Found 2038 Articles for R Programming

How to create a time series plot in R without time vector?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 10:03:49

332 Views

To create a time series plot in R without time vector, we can use ts.plot function.For Example, if we have a vector called X then we can create the time series plot of X by using the command ts.plot(X), the Output of this command will have a time axis in place of X-axis. Check out the below Examples to understand how it works.Example 1Consider the following vector −x

How to plot matrix elements using corrplot in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:41:52

3K+ Views

To create a plot of matrix elements with the help of corrplot function, we would need to set is.corr argument to FALSE so that the matrix values will be plotted in the diagram, otherwise, the corrplot function requires correlation matrix instead of a matrix hence there will be an error as shown in the Example below.ExampleConsider the matrix given below −M

How to draw concentric circles in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:36:05

480 Views

To draw concentric circles, we can use draw.circle function of plotrix package but firstly, we would need to create a blank graph with plot function as shown below.For Example, we can create three concentric circles at position X=5 and Y=5 having radius of 1, 2, and 3 by using the below command −draw.circle(5, 5, c(3, 2, 1), col=c("blue", "red", "green"))ExampleTo create three concentric circles at position X=5 and Y=5 having radius of 1, 2, and 3 add the following code to the above snippet −plot(1:10, type="n")OutputIf you execute all the above given snippets as a single program, it generates the ... Read More

How to create vertical line in xyplot in R?

Nizamuddin Siddiqui
Updated on 28-Oct-2021 09:37:20

334 Views

To create vertical line in xyplot, we can use abline function.For Example, if we have a data frame called df that contains two columns say X and Y and we want to create a scatterplot between X and Y using xyplot with a vertical line at X = 2 then we can use the below command −xyplot(y~x,df,abline=c(v=2))ExampleConsider the data frame given below −x

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

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

739 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

5K+ 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

1K+ 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

2K+ 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

637 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

Advertisements