R Programming Articles

Page 142 of 174

How to create a horizontal boxplot in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 3K+ Views

To create a horizontal boxplot in base R, we can set the horizontal argument inside boxplot function to TRUE. For example, if we have a vector called x then the horizontal histogram of that vector can be created by using the command boxplot(x,horizontal=TRUE).Example1x

Read More

How to create normal random variables with specific correlation between them in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 700 Views

To create normal random variables with specific correlation between them, we can use mvrnorm function of MASS package. For example, if we want to create two variables of size 10 with means equal to 2 and 4 and standard deviation of 0.5 then it can be done by using the command −mvrnorm(10,mu=c(2,4),Sigma=matrix(c(1,0.5,0.5,1),ncol=2),empirical=TRUE)Example1library(MASS) X

Read More

How to find prime factors of a number in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 1K+ Views

A prime number is the number that is only divisible by itself and one. These prime numbers can also divide other numbers hence they become a factor of those numbers. For example, 5 is a prime number and it also divides 20. To find the prime factors of a number, we can use primeFactors function of numbers package.Exampleslibrary(numbers)primeFactors(100)[1] 2 2 5 5primeFactors(1000)[1] 2 2 2 5 5 5 primeFactors(32547)[1] 3 19 571primeFactors(12354767)[1] 17 726751 primeFactors(21457)[1] 43 499primeFactors(99)[1] 3 3 11 primeFactors(365748)[1] 2 2 3 29 1051primeFactors(214687)[1] 11 29 673 primeFactors(3587497)[1] 3587497primeFactors(35874)[1] 2 3 3 1993 primeFactors(268713)[1] 3 3 73 409primeFactors(298473)[1] ...

Read More

How to subset a data.table object using a range of values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 3K+ Views

To subset a data.table object using a range of values, we can use single square brackets and choose the range using %between%. For example, if we have a data.table object DT that contains a column x and the values in x ranges from 1 to 10 then we can subset DT for values between 3 to 8 by using the command DT[DT$x %between% c(3,8)].Example1Loading data.table package and creating a data.table object −library(data.table) x1

Read More

How to create empty bar plot in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 438 Views

To create a bar plot in base R, we can use the function barplot and pass the vector or column of the data frame for which we want to create the bar plot but the bars created by using barplot by default has grey color. Therefore, if we want to create an empty bar plot then setting the color of bars to NA will make the plot an empty bar plot.Example1x

Read More

How to create a histogram without bins in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Mar-2021 406 Views

We can set type argument to s in plot function to create a histogram without bins but first we need to create the histogram and store it in an object. For example, if we have a vector say x then the histogram of x can be stored in an object called p then we can use the command plot(c(p$counts,0),type="s") to create the histogram without bins as shown in the below example.Example> x p plot(c(p$counts,0),type="s")Output

Read More

How to create bar plot in base R with different limits for Y-axis?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Mar-2021 2K+ Views

To create a bar plot in base R with different limits for Y-axis, we can use ylim argument but generally that behaves badly, such as extending the bars below X-axis. Therefore, we need to fix those things. Check out the below example to understand how it can be done.Example> x barplot(x)OutputExample> barplot(x,ylim=c(300,600))OutputExample> barplot(x,ylim=c(300,600),xpd=FALSE)OutputExample> box(bty="l") Output

Read More

How to extract the row for groupwise maximum in another column of an R data.table object?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Mar-2021 491 Views

To extract the row for groupwise maximum in another column of an R data.table object, can make use of which.max function by defining the grouping column. It means that if we have a categorical/grouping column and a numerical column then we groupwise maximum will be the maximum for each grouping level in the numerical column and we can extract the row based on these two columns. Check out the examples to understand how it works.Example1Loading data.table package and creating a data.table object −> library(data.table) > x1 x2 x3 DT1 DT1Output   x1 x2 x31:  B  3  2 2:  C  6  0 ...

Read More

How to display raise to the power on X-axis in base R plot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Mar-2021 450 Views

To display anything different than the vector or column names on the axes, we need to use xlab for X-axis and ylab for Y-axis. Therefore, if we want to display raise to the power on X-axis then xlab argument will be along with the plot function. For example, if we have a vector called x and we want to create a point chart for x -square with X-axis showing x^2 then it can be done as plot(x^2,xlab="x^2").Example> x y plot(x,y)OutputExample> plot(x/1000,y,xlab="x/10^3")Output

Read More

How to remove rows in a data.table object with NA's in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 04-Mar-2021 2K+ Views

If a row contains missing values then their sum will not finite, therefore, we can use is.finite function with the data.table object to remove the rows with NA’s. For example, if we have a data.table object called DT that contains some rows with NA’s then the removal of those rows can be done by using DT[is.finite(rowSums(DT))].Example1Loading data.table package and creating a data.table object −> library(data.table) > x1 x2 DT1 DT1Output   x1  x2 1:  1   2 2:  NA  4 3:  1   2 4:  NA  5 5:  1   6 6:  1   8 7:  NA  3 8:  1   ...

Read More
Showing 1411–1420 of 1,740 articles
« Prev 1 140 141 142 143 144 174 Next »
Advertisements