Found 2038 Articles for R Programming

How to change the whisker line type in base R boxplot?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 06:58:19

1K+ Views

The default boxplot in R has dotted whisker lines and if we want to change it to something else, we can use the whisklty argument.For Example, if we have a vector called X then we can create the boxplot of X with different whisker line by using the command boxplot(X,whisklty=1). The argument can take different values.Check out the Examples given below to understand how it works.ExampleConsider the vector given below −x

Change the outline color for histogram bars using ggplot2 in R.

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:32:56

4K+ Views

To change the outlines color of histogram bars using ggplot2, we can use col argument inside geom_histogram function of ggplot2 package.For Example, if we have a data frame called df that contains a column say X then we can create the histogram of X with different outline color of bars using the below command −ggplot(df,aes(X))+geom_histogram(bins=30,col=I("red"))ExampleFollowing snippet creates a sample data frame −x

Find the standard deviation for every n number of observations in an R data frame column.

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:30:47

215 Views

To find the standard deviation for every n number of observations in an R data frame, we can use rollapply function of zoo package.For Example, if we have a data frame called df that contains a column say X containing 100 values then we can create a column with standard deviation of every 10 values by using the below command −df$SD_10

Create a number vector in R with initial values as zero by defining the maximum digits for each value.

Nizamuddin Siddiqui
Updated on 27-Oct-2021 12:24:04

46 Views

To create a number vector in R with initial values as zero by defining the maximum digits for each value, we can use sprintf function.For Example, if we want to create a number vector having values starting from 1 to 100 and we want to have 1 as 001 and so on then, we can use the below command −sprintf('%0.3d',1:100)ExampleFollowing snippet creates a sample data frame −x1

How to remove end lines from a boxplot in R?

Nizamuddin Siddiqui
Updated on 27-Oct-2021 12:19:07

511 Views

The default boxplot in R has straight lines that display end point(s) excluding outliers. To remove these end lines from a boxplot, we can use staplelty argument and set it to 0.For Example, if we have a vector called X then we can create the boxplot of X by using the command given below −boxplot(X,staplelty=0)ExampleFollowing snippet creates a sample data frame −x

How to check if a data frame has any missing value in R?

Nizamuddin Siddiqui
Updated on 27-Oct-2021 12:13:21

1K+ Views

To check if a data frame has any missing value in R, we can use any function along with is.na function. For Example, if we have a data frame called df then we can use the below command to check whether df contains any missing value or notany(is.na(df))Example 1Following snippet creates a sample data frame −x1

Create a subset of non-duplicate values without the first duplicate from a vector in R.

Nizamuddin Siddiqui
Updated on 27-Oct-2021 11:54:15

88 Views

Generally, the duplicate values are considered after first occurrence but the first occurrence of a value is also a duplicate of the remaining therefore, we might want to exclude that as well.The subsetting of non-duplicate values from a vector in R can be easily done with the help of duplicated function with negation operator as shown in the Examples given below.Example 1Following snippet creates a sample data frame −x1

Create scatterplot for two dependent variables and one independent variable in R.

Nizamuddin Siddiqui
Updated on 11-Nov-2021 04:40:13

2K+ Views

To create scatterplot for two dependent variables and one independent variable, we can use geom_point function and geom_smooth function of ggplot2 package. Both of these functions will be used twice, where we can define the aesthetics of the plot for each dependent variable as shown in the Example below.ExampleFollowing snippet creates a sample data frame −x

How to create pie chart in base R?

Nizamuddin Siddiqui
Updated on 27-Oct-2021 11:40:52

122 Views

A pie chart is a circular representation of data which is created for either nominal data or ordinal data. The slices in the pie chart depend on the magnitude of the data values. If we want to create a pie chart in base R then pie function can be used.For Example, if we have a vector called X then the pie chart for values in X can be created by using the below command −pie(X)ExampleFollowing snippet creates a sample data frame −x

How to find the row products for each row in an R data frame?

Nizamuddin Siddiqui
Updated on 27-Oct-2021 11:26:13

194 Views

To find the row products for each row in an R data frame, we can use rowProds function of matrixStats package but we need to read the data frame as a matrix.For Example, if we have a data frame called df then we can find the row products for each row in df by using the command given below −rowProds(as.matrix(df))Example 1Following snippet creates a sample data frame −x1

Advertisements