Plot Two Histograms Together in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:41:01

374 Views

Consider the below data frames −> glucose fructose glucose$sweetener fructose$sweetener sweeteners library(ggplot2) > ggplot(sweeteners, aes(length, fill = sweetener)) + geom_density(alpha = 0.2)

Delete Rows in an R Data Frame

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:38:44

430 Views

This can be done by using square brackets.Example> data data    X1       X2       X3       X4       X5 1 4.371434 6.631030 5.585681 3.951680 5.174490 2 4.735757 4.376903 4.100580 4.512687 4.085132 3 4.656816 5.326476 6.188766 4.824059 5.401279 4 3.487443 4.253042 5.277751 6.121441 4.925158 5 5.174943 3.704238 5.813336 5.224412 4.990136 6 3.461819 5.102038 6.094579 5.536754 6.311731 7 4.772712 6.445479 5.254032 4.430560 7.183776 8 5.366510 5.232044 5.422526 3.746559 4.810256 9 4.786759 4.665812 4.634238 6.511210 4.959757 10 6.731195 5.083179 4.969842 4.976357 4.939117Let’s say we want to remove rows 4, 7, and 9. We will do it as follows −> data data       X1    X2       X3       X4       X5 1 4.371434 6.631030 5.585681 3.951680 5.174490 2 4.735757 4.376903 4.100580 4.512687 4.085132 3 4.656816 5.326476 6.188766 4.824059 5.401279 5 5.174943 3.704238 5.813336 5.224412 4.990136 6 3.461819 5.102038 6.094579 5.536754 6.311731 8 5.366510 5.232044 5.422526 3.746559 4.810256 10 6.731195 5.083179 4.969842 4.976357 4.939117

Split a Vector into Chunks in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:36:43

329 Views

This can be done with the help of seq_along, split, and ceiling.Example> x x [1] 6 5 6 3 11 4 5 6 7 4 6 4 3 4 7 1 8 4 2 4 5 7 8 5 9 [26] 3 5 5 5 1 7 5 8 1 8 3 8 4 5 7 5 8 4 4 2 5 6 3 9 4 [51] 6 3 3 2 5 6 5 4 5 5 2 3 3 12 11 6 4 5 6 7 5 2 2 5 8 [76] 3 8 8 7 3 7 6 6 4 1 6 8 3 6 6 6 6 4 8 6 4 5 4 2 5 > max y chunks chunks $`1` [1] 6 5 6 3 11 4 5 6 7 4 6 4 3 4 7 1 8 4 2 4 $`2` [1] 5 7 8 5 9 3 5 5 5 1 7 5 8 1 8 3 8 4 5 7 $`3` [1] 5 8 4 4 2 5 6 3 9 4 6 3 3 2 5 6 5 4 5 5 $`4` [1] 2 3 3 12 11 6 4 5 6 7 5 2 2 5 8 3 8 8 7 3 $`5` [1] 7 6 6 4 1 6 8 3 6 6 6 6 4 8 6 4 5 4 2 5

Split String Column into Multiple Columns in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:35:39

472 Views

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

Remove Unique Characters Within Strings in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:33:32

173 Views

We can achieve this by using gsub functionExample> x x [1] "14870e" "16578e" "302e47" "e95748" > gsub("e", "", x) [1] "14870" "16578" "30247" "95748"

Rearrange Data from Long Format to Wide Format in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:32:43

164 Views

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

Remove a Column from an R Data Frame

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:30:39

9K+ Views

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

Extract Characters from a String in R

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

683 Views

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"

Check If a String is a Subset of Another String in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:27:52

1K+ Views

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.

Access the Last Value in a Vector in R

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:25:47

2K+ Views

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

Advertisements