Found 2003 Questions for R Programming

How to change the orientation and font size of x-axis labels using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:53:37
This can be done by using theme argument in ggplot2Example> df df x y 1 long text label a -0.8080940 2 long text label b 0.2164785 3 long text label c 0.4694148 4 long text label d 0.7878956 5 long text label e -0.1836776 6 long text label f 0.7916155 7 long text label g 1.3170755 8 long text label h 0.4002917 9 long text label i 0.6890988 10 long text label j 0.6077572Plot is created as follows −> library(ggplot2) > ggplot(df, aes(x=x, y=y)) + geom_point() + theme(text = element_text(size=20), axis.text.x = element_text(angle=90, hjust=1))

How to select only numeric columns from an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:51:29
The easiest way to do it is by using select_if function of dplyr package but we can also do it through lapply.Using dplyr> df df X1 X2 X3 X4 X5 1 1 11 21 a k 2 2 12 22 b l 3 3 13 23 c m 4 4 14 24 d n 5 5 15 25 e o 6 6 16 26 f p 7 7 17 27 g q 8 8 18 28 h r 9 9 19 29 i s 10 10 20 30 j t >library("dplyr") > select_if(df, is.numeric) X1 X2 X3 1 1 11 21 2 2 12 22 3 3 13 23 4 4 14 24 5 5 15 25 6 6 16 26 7 7 17 27 8 8 18 28 9 9 19 29 10 10 20 30Using lapply> numeric_only df[ , numeric_only] X1 X2 X3 1 1 11 21 2 2 12 22 3 3 13 23 4 4 14 24 5 5 15 25 6 6 16 26 7 7 17 27 8 8 18 28 9 9 19 29 10 10 20 30

How to delete columns by their name in data.table in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:50:15
We can do this by setting the column to NULLExample> library(data.table) > df data_table data_table[, x:=NULL] > data_table numbers 1: 1 2: 2 3: 3 4: 4 5: 5 6: 6 7: 7 8: 8 9: 9 10: 10To delete two columns> df Data_table Data_table numbers 1: 0 2: 1 3: 2 4: 3 5: 4 6: 5 7: 6 8: 7 9: 8 10: 9

How to simulate discrete uniform random variable in R?

Nizamuddin Siddiqui
Updated on 11-Jul-2020 12:54:09
There is no function in base R to simulate discrete uniform random variable like we have for other random variables such as Normal, Poisson, Exponential etc. but we can simulate it using rdunif function of purrr package.The rdunif function has the following syntax −> rdunif(n, b , a)Here, n = Number of random values to returnb = Maximum value of the distribution, it needs to be an integer because the distribution is discretea = Minimum value of the distribution, it needs to be an integer because the distribution is discreteExampleLet’s say you want to simulate 10 ages between 21 to ... Read More

How to standardize columns in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:46:24
This can be done by using scale function.Example> data data x y 1 49.57542 2.940931 2 49.51565 2.264866 3 50.70819 2.918803 4 49.09796 2.416676 5 49.90089 2.349696 6 49.03445 3.883145 7 51.29564 4.072614 8 49.11014 3.526852 9 49.41255 3.320530 10 49.42131 3.033730 > standardized_data standardized_data x y [1,] -0.1774447 -0.20927607 [2,] -0.2579076 -1.28232321 [3,] 1.3476023 -0.24439768 [4,] -0.8202493 -1.04137095 [5,] 0.2607412 -1.14768085 [6,] -0.9057468 1.28619932 [7,] 2.1384776 1.58692277 [8,] -0.8038439 0.72069363 [9,] -0.3967165 0.39321942 [10,] -0.3849124 -0.06198639 attr(,"scaled:center") x y 49.707220 3.072784 attr(,"scaled:scale") x y 0.7427788 0.6300430

How to find the day of a week from a data frame in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:43:52
It can be done by using weekdays function.Example< df = data.frame(date=c("2020-07-01", "2020-08-10", "2020-11-15")) < df$day

How to remove all objects except one or few in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:42:16
We can use rm remove all or few objects.Example< x>-rnorm(100,0.5) < y>-1:100 < z>-rpois(100,5) < a>-rep(1:5,20)To remove all objects> rm(list=ls()) ls() character(0)To remove all except a> rm(list=setdiff(ls(), "a")) > ls() [1] "a"To remove all except x and a> rm(list=ls()[! ls() %in% c("x","a")]) ls() [1] "a" "x"

How to plot two histograms together in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:41:01
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)

How to delete rows in an R data frame?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:38:44
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

How to split a vector into chunks in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:36:43
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
Advertisements