Programming Articles - Page 931 of 3363

Combine values of two columns separated with hyphen in an R data frame.

Nizamuddin Siddiqui
Updated on 28-Oct-2021 09:29:59

962 Views

To combine values of two columns separated with hyphen in an R data frame, we can use apply function.For Example, if we have a data frame called df that contains only two columns say X and Y then we can combine the values in X and Y by using the below command given below −df$X_Y

How to find the class of columns of an R data frame?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 08:48:14

6K+ Views

To find the class of columns of an R data frame, we can use class function along with sapply and as.data.frame function.For Example, if we have a data frame called DF then we can find the class of columns in DF by using the command as follows −data.frame(sapply(DF, class))Example 1Consider the below data frame −head(mtcars, 20) The following dataframe is created                      mpg  cyl   disp   hp  drat     wt   qsec  vs am gear carb Mazda RX4           21.0    6  160.0  110  3.90 ... Read More

How to check which list element contains a particular value in R?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 08:16:42

1K+ Views

A list in R can have large number of elements and also of different types. If we have a list that contain vectors and we want to check which list element contains a particular value then we can use which function along with sapply function.For Example, if we have a list called LIST then we can find which element of LIST contains 5 then we can use the command given below −which(sapply(LIST, FUN=function(X) 5 %in% X))Example 1Consider the List given below −List1

How to convert strings in R data frame to unique integers?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 07:55:11

2K+ Views

To convert strings in R data frame to unique integers, we first need to extract the unique strings in the data frame and then read them inside data.frame function with as.numeric along with factor function.Check out the below Examples to understand how it works.Example 1Consider the below data frame −x1

Create a date column in R using a date vector excluding weekends.

Nizamuddin Siddiqui
Updated on 28-Oct-2021 07:33:11

3K+ Views

If we have a vector of dates that contains all days in a week then we can use that vector to create a date column by excluding weekends with the help of subsetting the vector as shown in the below Examples. After subsetting the vector, we will just need to pass the vector in data.frame function.Example 1Consider the below vector of dates −x

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

Nizamuddin Siddiqui
Updated on 28-Oct-2021 07:23:02

825 Views

The default boxplot in R has straight border line type that display end point(s) excluding outliers. To change these border lines from a boxplot, we can use staplelty argument.For Example, if we have a vector called X then we can create the boxplot of X with different border line type by using the command boxplot(X,staplelty=15). The argument can take different values. Check out the below Examples to understand how it works.ExampleConsider the below vector −x

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

390 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

98 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

Advertisements