Concatenate Strings in R

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

339 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"

Find Modes of a Vector in R

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

360 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

Create Frequency Table of a Vector with Repeated Values in R

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

726 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

Change Column and Row Names of a Data Frame in R

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

571 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)

Check Loaded Packages in R

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

4K+ 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

Create an Empty Data Frame in R

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

281 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.

Convert List to Data Frame in R

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

250 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

Check if a Vector Contains a Given Value in R

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

494 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

Drop Factor Levels in Subset of a Data Frame in R

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

779 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

Create Two Line Charts in the Same Plot in R

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

199 Views

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")

Advertisements