Posts

Count number of items and sum of the items in a column in R

Image
Code: mydf <- data.frame(Key=c('a','b','c','d','a','a','a','a','b','c','d','c','c','d','b','b','a'), Values=c(4,6,3,4,5,6,2,1,3,4,5,6,4,2,4,5,6))                 mydf_keys <- unique(mydf$Key) mydf_vals <- c(nrow(mydf_keys)) for (i in mydf_keys) {     mydf_vals <- c(mydf_vals, nrow(mydf[mydf$Key %in% i, ])) } #mydf_count gives count of items i.e. how many times they appear mydf_count <- data.frame(item=mydf_keys, count=mydf_vals) print(mydf_count) #mydf_sum gives sum of values of items in the column you specify mydf_sum <- setNames(aggregate(mydf$Values, by = list(item=mydf$Key), FUN = sum, na.rm = TRUE), c('item', 'sum')) print(mydf_sum) Output: If you have a better approach for this then please share in the comments below or email me. :) This R example groups rows by a key column and comput...

Run Microsoft R Open Script From Command Prompt

Image
Microsoft R Open is Multi-core environment developed on Core R. Microsoft R Open (MRO), formerly known as Revolution R Open (RRO), is the enhanced distribution of R from Microsoft Corporation. It is a complete open source platform for statistical analysis and data science. Microsoft R Open 3.4.1 is coming September 11th. MRO comes with Microsoft R Open GUI and you can also use it in RStudio IDE. But in case you want to run it from command prompt, it is pretty easy. First you need to set Path in environment variable. For 32-bit version path would be like: C:\Program Files\Microsoft\R Open\R-3.4.0\bin For 64-bit version path would be like: C:\Program Files\Microsoft\R Open\R-3.4.0\bin\x64 Now save it and restart command prompt if already open or open a new command prompt window. Type command "where rscript" and check whether it returns a correct path. Now you can type command "rscript your_script.r" and it will get executed. No...

Xiaomi Mi A1 vs Motorola Moto G5S Plus Price and Comparison

Image
Xiaomi Mi A1 The Xiaomi Mi A1 is a dual-SIM (Nano-SIM) smartphone that runs Android 7.1.2 Nougat. It features a 5.5-inch full-HD (1080x1920 pixels) display. It is powered by an octa-core Qualcomm Snapdragon 625 SoC coupled with 4GB of RAM. In terms of optics, the dual rear camera setup of the Xiaomi Mi A1 bears two 12-megapixel sensors, with one featuring a wide-angle lens with a 1.25-micron pixel sensor and f/2.2 aperture, and the other sporting a telephoto lens, 1-micron pixel sensor, an f/2.6 aperture, and capable of delivering 2x optical zoom. The smartphone bears a 5-megapixel front camera with a real-time beautification mode. The Xiaomi Mi A1 comes with 64GB of inbuilt storage, which is expandable via microSD card (up to 128GB) in a hybrid dual-SIM configuration. It houses a 3080mAh battery. Motorola Moto G5S Plus The dual-SIM (Nano) Moto G5S Plus runs Android 7.1 Nougat out of the box, and it sports a 5.5-inch full-HD (1080x1920 pixels) IPS LCD display. It is...

Perl Subroutine Arguments

Image
Subroutines:- 1. Retrieving arguments to a subroutine with shift: A subroutine's arguments come in via the special @_ array. The shift without an argument defaults to @_ .     sub volume {         my $height = shift ;         my $width = shift ;         my $depth = shift ;         return $height * $width * $depth ;     }

C/C++ Parameter Passing

Image
C parameter passing:- 1. Pass by Value: - The value of a variable is sent to function. - The actual parameter cannot be changed by function. - Duplicate Copy of Original Parameter is Passed. - No effect on Original Parameter after modifying parameter in function. # include < stdio.h > void interchange ( int number1 , int number2 ) { int temp ; temp = number1 ; number1 = number2 ; number2 = temp ; } int main ( ) { int num1 = 50 , num2 = 70 ; interchange ( num1 , num2 ) ; printf ( " \n Number 1 : %d " , num1 ) ; printf ( " \n Number 2 : %d " , num2 ) ; return ( 0 ) ; }