Found 2003 Questions for R Programming

How to find the index of an element in a vector in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:15:42
There are three ways to find the index of an element in a vector.Example> x x [1] 8 10 9 6 2 1 4 7 5 3Using which> which(x == 6)[[1]] [1] 4Here we found the index of 6 in vector x.Using match> match(c(4,8),x) [1] 7 1Here we found the index of 4 and 8 in vector x.Using which with %in% > which(x %in% c(2,4)) [1] 5 7Here we found the index of 2 and 4 in vector x.

How to randomly select a fixed number of rows without replacement from a data frame in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:14:37
This can be done simply by using sample function.Example> df = data.frame(matrix(rnorm(20), nrow=5)) > df X1 X2 X3 X4 1 -0.3277833 -0.1810403 0.2844406 -2.9676440 2 0.8262923 0.4334449 0.4031084 -1.9278049 3 -0.1769219 -0.1583660 -0.2829540 -0.1962654 4 1.0357773 0.9326049 0.3250011 -1.8835882 5 -1.0682642 -0.6589731 -0.4783144 -0.2945062Let’s say we want to select 3 rows randomly then it can be done as follows −> df[sample(nrow(df), 3), ] X1 X2 X3 X4 2 0.8262923 0.4334449 0.4031084 -1.9278049 1 -0.3277833 -0.1810403 0.2844406 -2.9676440 5 -1.0682642 -0.6589731 -0.4783144 -0.2945062

How to rename a single column in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:11:56
We can do this by defining the newname as shown below −> Samp Samp sample.1.100..10. 1 47 2 63 3 57 4 16 5 53 6 7 7 54 8 2 9 13 10 14 > colnames(Samp) Samp Sampled Values 1 47 2 63 3 57 4 16 5 53 6 7 7 54 8 2 9 13 10 14 Since we only have one column in the data frame, so it is sufficient to use the object name.

How to add zeros before numbers in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:09:36
The easiest way to add zeros before numbers is by using paste0 functionExample> ID Gender Lens data data ID Gender Lens 1 25499 1 0.8 2 25500 2 1.2 3 25501 2 1.0 4 25502 1 2.0 5 25503 2 1.8 6 25504 1 1.4Let’s say we want to add 00 before every ID.It can be done by using paste0 function as follows −> IDs newdata newdata IDs Gender Lens 1 0025499 1 0.8 2 0025500 2 1.2 3 0025501 2 1.0 4 0025502 1 2.0 5 0025503 2 1.8 6 0025504 1 1.4

How to sum a variable by factor levels in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:08:10
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 Graduation 51 2 PhD 39 3 Post-Graduation 28 Using tapply > tapply(x$Frequency, x$Category, FUN=sum) Graduation PhD Post-Graduation 51 39 28

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

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:06:29
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 6.075883e-09 1.117262e-16 13 9.134720e-12 8.286361e-16 14 5.052271e-15 5.609229e-15 15 1.027977e-18 3.485735e-14 16 7.694599e-23 1.998488e-13 17 2.118819e-27 1.061697e-12 18 2.146384e-32 5.246031e-12 19 7.998828e-38 2.419003e-11 20 1.096607e-43 1.043991e-10 21 5.530710e-50 4.228163e-10Second> select(df, 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 ... Read More

How to concatenate strings in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 13:00:44
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
Updated on 06-Jul-2020 12:58:40
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
Updated on 06-Jul-2020 12:57:49
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
Updated on 06-Jul-2020 12:56:42
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)
Advertisements