R Programming Articles

Page 24 of 174

How to calculate monthly average for time series object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To calculate monthly average for time series object, we can use tapply function with mean. For example, if we have a time series object called TimeData then the monthly average for this series can be found by using the command tapply(TimeData, cycle(TimeData), mean).Example1Consider the below time series object −> Data1 Data1Output   Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 1  988 695 867 211 915 348 729 518 592 447 448 880 2  551 410 427 134 133 572 637 800 630 878 642 940 3  603 335 638 639 595 512 671 863 752 568 ...

Read More

How to create stacked plot with density using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 489 Views

To create stacked plot with density using ggplot2, we can use geom_density function of ggplot2 package and position="stack". For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical then the stacked plot with density can be created by using the command −ggplot(df, aes(y, y=..density..))+geom_density(aes(fill=x), position="stack")ExampleConsider the below data frame −> x y df dfOutput   x y 1  C 3 2  C 5 3  B 4 4  A 7 5  B 1 6  A 6 7  D 4 8  C 3 9  C 7 10 ...

Read More

How to select rows of an R data frame that are non-NA?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 11K+ Views

To select rows of an R data frame that are non-Na, we can use complete.cases function with single square brackets. For example, if we have a data frame called that contains some missing values (NA) then the selection of rows that are non-NA can be done by using the command df[complete.cases(df), ].Example1Consider the below data frame −> x1 x2 x3 df1 df1Output   x1 x2 x3 1   1 NA NA 2  NA  5  3 3   1  5 NA 4   1 NA NA 5  NA  5 NA 6  NA  5  3 7  NA  5 NA 8   1 NA ...

Read More

How to add approximately equal sign in a plot using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 633 Views

To add approximately equal sign in a plot using ggplot2, we can use tilde sign twice as ~~ in geom_text function of ggplot2 package. For example, we can do this by using the following syntax geom_text(aes(label="NULL%~~%")). Check out the below example to understand how it works.ExampleConsider the below data frame −x

Read More

How to display the data frame summary in vertical order in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 482 Views

To display the data frame summary in vertical order, we can use lapply and cbind function along with the summary function. For example, if we have a data frame called df then the summary of df in vertical order can be found by using the below command −lapply(df, function(x) cbind(summary(x)))Example1Consider the below data frame −> x1 x2 x3 x4 x5 df1 df1Output   x1 x2 x3 x4 x5 1   4  0  2  2  6 2   7  2  4  1  7 3   7  2  3  3  6 4   4  0  4  5  2 5   5  2  2 ...

Read More

How to change the point size in geom_point conditionally in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To change the point size in geom_point conditionally, we can define the condition in geom_point with aes and the size using scale_size_manual function of ggplot2 package. For example, if we have a data frame called df that contains two columns say x and y then the scatterplot with different size of points for x values greater than 5 and less than equal to 5 can be drawn by using the below command −ggplot(df, aes(x, y))+geom_point(aes(size=x>5))+scale_size_manual(values=c(4, 7))ExampleConsider the below data frame −x6))+scale_size_manual(values=c(4, 7)) Output

Read More

How to create a list of regression models for predefined vectors in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To create a list of regression models for predefined vectors, we can create a blank list and then use the for loop to create the list of regression models. For example, if we have two vectors say x and y, and we want to create a list of regression model between x and y then we create a blank list using list() and perform for loop a particular number of times as shown in the below examples.Example1x

Read More

How to change default point size of geom_point in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 512 Views

To change the default point size of geom_point, we need to use update_geom_defaults function. Specifically, for the change of point size the syntax will be as follows −update_geom_defaults("point",list(size=”value”))Here, we can change the value according to our need.ExampleConsider the below data frame −x

Read More

How to find the proportion of each value for a cross tab obtained from a data frame in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

To find the proportion of each value for a cross tab obtained from a data frame, we can use prop.table function. Suppose we have a data frame called df that contains three columns, two categorical say C1 and C2 and one numerical say Y then the cross tab will be created by using the command xtabs(Y~.,df). Now the proportion of each value can be found by using prop.table(xtabs(Y~.,df),1).Example1Consider the below data frame −f1

Read More

How to create data frame using nested list elements in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 504 Views

To create data frame using nested list elements, we would need to unlist the list elements and store them in a matrix then read as a data frame using data.frame function. For example, if we have a nested called LIST then the data frame can be created by using the command −data.frame(matrix(unlist(LIST),ncol=”No of columns we want”,byrow=F))Check out the below example to understand how it works.ExamplenestedList

Read More
Showing 231–240 of 1,740 articles
« Prev 1 22 23 24 25 26 174 Next »
Advertisements