Found 2038 Articles for R Programming

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

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

221 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 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

249 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
Updated on 06-Jul-2020 12:58:40

222 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
Updated on 06-Jul-2020 12:57:49

588 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
Updated on 06-Jul-2020 12:56:42

450 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
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 utils datasets methods base other attached packages: [1] ggplot2_3.2.1 loaded via a namespace (and not attached): [1] Rcpp_1.0.2 withr_2.1.2 crayon_1.3.4 dplyr_0.8.3 [5] assertthat_0.2.1 grid_3.6.1 R6_2.4.0 gtable_0.3.0 [9] magrittr_1.5 scales_1.0.0 pillar_1.4.2 rlang_0.4.0 [13] lazyeval_0.2.2 glue_1.3.1 purrr_0.3.2 munsell_0.5.0 [17] compiler_3.6.1 pkgconfig_2.0.2 colorspace_1.4-1 tidyselect_0.2.5 [21] tibble_2.1.3So here we have base packages and ggplot2 ... Read More

How to create an empty data frame in R?

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

199 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 edit command will open a spreadsheet and we can enter the data in the data frame.

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

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

163 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 d z s 4 c f e d n w v b k h 5 u y g j i m l x k s 6 q k f v y b c i x z 7 x i v m f j h t g b 8 c z ... Read More

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

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

363 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 y x%in%y [1] FALSE TRUE FALSE FALSE

How to drop factor levels in subset of a data frame in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:47:10

514 Views

There are two ways to do drop the factor levels in a subset of a data frame, first one is by using factor function and another is by using lapply.Example> df levels(df$alphabets) [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" > subdf levels(subdf$alphabets) [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"Although we have created a subset but the level of factor variable alphabets still showing 10 levels. If we want to drop the factor levels then it can be done byUsing factor function> subdf$alphabets levels(subdf$alphabets) [1] "a" "b" "c" "d" "e" ... Read More

Advertisements