Found 2003 Questions for R Programming

How to split a string column into multiple columns in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:35:39
This can be done with the help of tidyr package.Example> library(tidyr) > data = data.frame(attr = c(1,5,12,17), type=c('class_and_memory','class_and_memory_2')) > data %>% + separate(type, c("class", "memory"), "_and_") attr class memory 1 1 class memory 2 5 class memory_2 3 12 class memory 4 17 class memory_2

How to remove unique characters within strings in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:33:32
We can achieve this by using gsub functionExample> x x [1] "14870e" "16578e" "302e47" "e95748" > gsub("e", "", x) [1] "14870" "16578" "30247" "95748"

How to re-arrange data from long format to wide format in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:32:43
This can be done by using reshape function.Example> data dataname salarygroup Errors1 firstName 1 58 2 firstName 2 50 3 firstName 3 47 4 firstName 4 29 5 firstName 5 36 6 LastName 1 34 7 LastName 2 40 8 LastName 3 54 9 LastName 4 38 10 LastName 5 41 > reshape(data, idvar = "name", timevar = "salarygroup", direction = "wide")name Errors.1 Errors.2 Errors.3 Errors.4 Errors.51 firstName 58 50 47 29 36 6 LastName 34 40 54 38 41

How to remove a column from an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:30:39
This can be easily done by using subset function.Example> df df x y z a 1 1 6 11 16 2 2 7 12 17 3 3 8 13 18 4 4 9 14 19 5 5 10 15 20To remove only one column> df df y z a 1 6 11 16 2 7 12 17 3 8 13 18 4 9 14 19 5 10 15 20To remove two columns> df df df z a 1 11 16 2 12 17 3 13 18 4 14 19 5 15 20To remove a range of columns> df df df a 1 16 2 17 3 18 4 19 5 20To remove separate columns> df df df y 1 6 2 7 3 8 4 9 5 10

How to extract characters from a string in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:29:06
We can use stri_sub function in stringi package.Example> x x [1] "TutorialsPoint is the largest online library for best tutorials" > library(stringi) > stri_sub(x,1,9) [1] "Tutorials" > stri_sub(x,1,-20) [1] "TutorialsPoint is the largest online library" > stri_sub(x,-14,-1) [1] "best tutorials" > stri_sub(x,-41,-1) [1] "largest online library for best tutorials"

How to check if a string is a subset of another string in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:27:52
To check whether a string is a subset of another string we can use grepl function.Example> Company Job grepl(Job, Company, fixed = TRUE) [1] TRUEHere we are getting TRUE because Tutor is a subset of TutorialsPoint.> grepl(Company, Job, fixed = TRUE) [1] FALSEHere we are getting FALSE because TutorialsPoint is not a subset of Tutor.

How to access the last value in a vector in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:25:47
This can be done by using tail function.Example> x tail(x,n=1) [1] 1095 > data tail(data,n=1) Class 10 PhD df = data.frame(matrix(rnorm(20), nrow=5)) > tail(df,n=1) X1 X2 X3 X4 5 -0.3595053 0.9943738 0.959761 -0.6565688 > tail(df$X4,n=1) [1] -0.6565688

How to deactivate scientific notation of numbers in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:23:19
We can use options(scipen=999) to do this.Example> x t.test(x, mu=2000)One Sample t-testdata: x t = -14.212, df = 9, p-value = 1.801e-07 alternative hypothesis: true mean is not equal to 200095 percent confidence interval −151.3501 659.0499sample estimates −mean of x 405.2Here p-value is in scientific notation. Now we can deactivate it as follows −> options(scipen=999) > t.test(x, mu=2000)One Sample t-testdata: x t = -14.212, df = 9, p-value = 0.0000001801 alternative hypothesis: true mean is not equal to 200095 percent confidence interval −151.3501 659.0499sample estimates −mean of x 405.2If we want to activate scientific notation again then it be ... Read More

How to change the order of bars in bar chart in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:21:14
This can be done by setting the levels of the variable in the order we want.Example> data data ggplot(data, aes(x = Class)) + geom_bar()Setting the levels in increasing order> data

How to reorder the columns in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:19:01
Reordering of columns can be done by using square brackets.Example> df = data.frame(matrix(rnorm(20), nrow=5)) > df       X1       X2       X3       X4 1 -0.3637644 2.0770246 0.48763128 -0.09019256 2 -3.1758515 2.3173075 0.86846761 0.38396459 3 1.1844641 0.3412267 1.90986295 -1.03493074 4 -0.5953466 1.7211738 -0.90686896 -0.71215313 5 -0.8732530 0.3256303 0.02312328 -0.36993899Let’s say we want to change the order of columns as X3, X2, X4, and X1 then it can be done as shown below −> df[,c(3,2,4,1)]       X3       X2       X4          X1 1  0.48763128 2.0770246 -0.09019256 -0.3637644 2  0.86846761 2.3173075  0.38396459 -3.1758515 3  1.90986295 0.3412267 -1.03493074  1.1844641 4 -0.90686896 1.7211738 -0.71215313 -0.5953466 5  0.02312328 0.3256303 -0.36993899 -0.8732530
Advertisements