Found 2038 Articles for R Programming

Change the decimal point of every value in an R data frame column.

Nizamuddin Siddiqui
Updated on 05-Nov-2021 05:54:26

3K+ Views

To change the decimal point of every value in an R data frame column, we can use round function.For Example, if we have a data frame called df that contains a column say X and we want to have each value with 3 decimal places then we can use the below command −df$X

Why Output of mean of normal random variable created using rnorm equals to 10 is not 10 in manual calculation with R?

Nizamuddin Siddiqui
Updated on 05-Nov-2021 05:49:19

49 Views

When we find the mean of normal random variable which is created with rnorm(“sample_size”, 10) is not 10 because rnorm will create a random variable hence mean will be changed but as we increase the sample size the mean will become closer to 10.Check out the Examples given below to understand the variation in the Outputs as the sample size increases.ExampleThe variation in the Outputs of mean of normal random variable created by using rnorm as the sample size increases is explained below −mean(rnorm(100, mean=10)) mean(rnorm(100, mean=10)) mean(rnorm(100, mean=10)) mean(rnorm(100, mean=10)) mean(rnorm(100, mean=10)) mean(rnorm(100, mean=10)) mean(rnorm(100, mean=10)) mean(rnorm(1000, mean=10)) mean(rnorm(1000, ... Read More

Find the missing numbers in a sequence in R data frame column.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 10:13:49

803 Views

To find the missing numbers in a sequence in R data frame column, we can use setdiff function.For Example, if we have a data frame called df that contains a column say X and we want to check which values between 1 to 20 are missing in this column then we can use the below command −setdiff(1:20,df$X)Example 1Following snippet creates a sample data frame −x

How to extract columns having at least one non-duplicate in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 10:07:38

72 Views

To extract columns from an R data frame having at least one non-duplicate, we can use Filter function.For Example, if we have a data frame called df that contains some columns having duplicate values and columns that do not contains at least one non-duplicate then we can extract columns having at least one non-duplicate by using the below command −Filter(var,df)Example 1Following snippet creates a sample data frame −x1

Create bar plot for grouped data of two columns in base R.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 10:02:16

3K+ Views

To create bar plot for grouped data in base R, we can create the table for both the columns and then use beside argument of barplot function to create the bar plot. To differentiate between the bars, we need to set legend argument to TRUE as well. To understand how it can be done check out the below Example.ExampleFollowing snippet creates a sample data frame −G

Get a matrix as Output if a condition for a single value is met in R.

Nizamuddin Siddiqui
Updated on 03-Nov-2021 09:58:55

198 Views

If we want to check a condition and the Output based on that condition needs to be a matrix then we can use if else function in R.For Example, if we have a value say V = 5 and we want to get matrix M1 if V is equal to 5 and matrix M2 if V is not equal to 5 then we can use the below command −if (V == 5) M1 else M2ExampleFollowing snippet creates a sample matrices −M1

How to create normal probability plot in R with confidence interval bands?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:46:41

2K+ Views

To create normal probability plot in R with confidence interval bands, we can use qqPlot function of QTLRel package. We simply need to pass the vector name that contains normal distribution values inside qqPlot function or directly introduce the vector inside the function as shown in the below given examples.Example 1Use the following code to create normal probability plot −library("QTLRel") qqPlot(rnorm(10))OutputIf you execute the above given snippet, it generates the following Output −Example 2Add the following code to the above snippet to create normal probability plot −qqPlot(rnorm(500)) OutputIf you execute the above given snippet, it generates the following Output −Example ... Read More

How to create an ID column in R based on categories?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:44:43

2K+ Views

If we have a categorical column in an R data frame then it can be used to create an ID column where each category will have its own ID defined on the basis of categories in the categorical column.For this purpose, we would need to read the categorical column with as.factor and as.numeric function as shown in the below examples.Example 1Following snippet creates a sample data frame −Group

How to create a scatterplot with larger distance between facets in R?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:40:48

69 Views

By default, the distance/space between facets created by using ggplot2 is very less and it becomes a little uneasy for viewers to separately read the facets. Therefore, to deal with this problem we can increase the space between facets and it can be done with the help of theme function as shown in the example below.ExampleFollowing snippet creates a sample data frame &miuns;x

How to find the column means for each matrix stored in an R list?

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:38:22

401 Views

To find the column mean of all matrices stored in an R list, we can use sapply function along with colMeans function.For example, if we have a list called LIST that contains some matrices then the col means for each matrix can be found by using the commandsapply(LIST,colMeans)Check out the below example to understand how it works.ExampleFollowing snippet creates list of matrices −M1

Advertisements