Nizamuddin Siddiqui has Published 2307 Articles

How to sum a variable by factor levels in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:08:10

2K+ Views

We can do this by using aggregate function or with the help tapplyExample> x x Category Frequency 1 Graduation 12 2 Graduation 19 3 Post-Graduation 15 4 Graduation 20 5 PhD 25 6 Post-Graduation 13 7 PhD 14Using aggregate> aggregate(x$Frequency, by=list(Group=x$Category), FUN=sum) Group x 1 ... Read More

How to extract columns of a data frame in R using dplyr package?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:06:29

218 Views

Example> x y z df library(dplyr) > df %>% select(y, z) y z 1 1.486720e-06 7.888609e-31 2 1.338302e-04 7.888609e-29 3 4.431848e-03 3.904861e-27 4 5.399097e-02 1.275588e-25 5 2.419707e-01 3.093301e-24 6 3.989423e-01 5.939138e-23 7 2.419707e-01 9.403635e-22 8 5.399097e-02 1.262774e-20 9 4.431848e-03 1.467975e-19 10 1.338302e-04 1.500596e-18 11 1.486720e-06 1.365543e-17 12 ... Read More

How to concatenate strings in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 13:00:44

247 Views

Concatenation can be done by using paste function in R.Example> paste("I", "Love", "R", sep=" ") [1] "I Love R"If we want to specify characters between words then it can be done by using collapse argument as follows −> x x [1] "I" "Love" "R" > paste(x, collapse="-") [1] "I-Love-R"

How to find mode(s) of a vector in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:58:40

217 Views

Just like mean and median there is no in-built function in R to find the mode.We can make use of the following user created function for this purpose> Modes

How to create a frequency table of a vector that contains repeated values in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:57:49

583 Views

We can do this by using table function with as.data.frameExample> X as.data.frame(table(X)) X Freq 1 21 2 2 23 3 3 24 1 4 25 5 5 34 2 6 47 2 7 64 1 8 69 5 9 70 1

How to change the column names and row names of a data frame in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:56:42

448 Views

We can colnames function to change the column names and rownames function to change the row names.Example> df df ID Salry 1 1 10000 2 2 30000 3 3 22000 4 4 27000 5 5 18000 > colnames(df) df EmployeeID Salary 1 1 10000 2 2 30000 3 3 22000 4 4 27000 5 5 18000 > rownames(df)

How to check which packages are loaded in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:55:27

3K+ Views

We can do this by using sessionInfo().> sessionInfo() R version 3.6.1 (2019-07-05) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363) Matrix products: default Random number generation: RNG: Mersenne-Twister Normal: Inversion Sample: Rounding locale: [1] LC_COLLATE=English_India.1252 LC_CTYPE=English_India.1252 [3] LC_MONETARY=English_India.1252 LC_NUMERIC=C [5] LC_TIME=English_India.1252 attached base packages: [1] stats graphics grDevices ... Read More

How to create an empty data frame in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:53:45

198 Views

We can create an empty data frame as follows −> df str(df) 'data.frame': 0 obs. of 5 variables: $ Income : num $ Age : int $ EducationLevel: Factor w/ 0 levels: $ MaritalStatus : logi $ StateLiving : chrWe can edit this data frame by using −> edit(df)This ... Read More

How to convert a list to a data frame in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:50:28

160 Views

Consider the below list −> List df df X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 1 x c t g d l j y i q 2 s k u j i w x o p n 3 h p e c y m o ... Read More

How to check if a vector contains a given value in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:48:51

360 Views

We can use match %in% to check whether a vector contains a given value of notExample> x 1%in%x [1] TRUE > 10%in%x [1] TRUE > 99%in%x [1] TRUE > 1024%in%x [1] TRUE > 100%in%x [1] FALSEWe can also do this for checking the common values between two vectors.Example> x ... Read More

Advertisements