Found 2003 Questions for R Programming

How to check which packages are loaded in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:55:27
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
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
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
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
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

How to create two line charts in the same plot in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:45:16
We can do this by using lines function after plotting the first chart.Example> x X1 X2 plot(x, X1, type="l",col="red", ylab="X") > lines(x, X2, col="green")

How to convert a factor that is represented by numeric values to integer or numeric variable in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:43:51
We can convert a factor to integer or numeric variable by using as.numeric function with defining the levels of the factor or by defining the characters of the factorExample> f f [1] 0.323049098020419 0.916131897130981 0.271536672720686 0.462429489241913 [5] 0.657008627429605 0.462429489241913 0.462429489241913 0.212830029195175 [9] 0.271536672720686 0.497305172728375 7 Levels: 0.212830029195175 0.271536672720686 ... 0.916131897130981Using as.numeric> as.numeric(levels(f))[f] [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052 > Using as.numeric(as.character( )) > as.numeric(as.character(f)) [1] 0.3230491 0.9161319 0.2715367 0.4624295 0.6570086 0.4624295 0.4624295 [8] 0.2128300 0.2715367 0.4973052

How to replace NA values with zeros in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:40:55
We can replace all NA values by using is.na functionExample> Data df df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 NA 5 4 7 3 1 2 6 NA 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 NA NA 0 2 4 2 NA 8 6 9 9 9 4 0 6 1 7 NA 9 5 5 NA 8 1 NA 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1Replacing NA’s by 0’s> df[is.na(df)] df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 0 5 4 7 3 1 2 6 0 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 0 0 0 2 4 2 0 8 6 9 9 9 4 0 6 1 7 0 9 5 5 0 8 1 0 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1

How to drop data frame columns in R by using column name?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:39:01
Columns of a data frame can be dropped by creating an object of columns that we want to drop or an object of columns that we want to keep.Example> df keeps df[keeps] Var3 Var4 1 21 31 2 22 32 3 23 33 4 24 34 5 25 35 6 26 36 7 27 37 8 28 38 9 29 39 10 30 40

How to extract p-value and R-squared from a linear regression in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 12:36:36
We can use regression model object name with $r.squared to find the R-squared and a user defined function to extract the p-value.ExampleExtracting R-Squared> x y LinearRegression summary(LinearRegression)$r.squared [1] 0.2814271Extracting p-value> Regressionp
Advertisements