
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a lagged variable in R for groups?
Lagged variable is the type of variable that contains the previous value of the variable for which we want to create the lagged variable and the first value is neglected. Therefore, we will always have one missing value in each of the groups, if we are creating a lagged variable that depends on a grouping variable or factor variable.
Example
Consider the below data frame:
> set.seed(2) > Factor<-rep(c("F1","F2","F3","F4"),each=5) > Rate<-c(12,54,18,26,14,25,81,47,15,12,96,25,14,34,68,78,42,56,81,75) > df<-data.frame(Factor,Rate) > df Factor Rate 1 F1 12 2 F1 54 3 F1 18 4 F1 26 5 F1 14 6 F2 25 7 F2 81 8 F2 47 9 F2 15 10 F2 12 11 F3 96 12 F3 25 13 F3 14 14 F3 34 15 F3 68 16 F4 78 17 F4 42 18 F4 56 19 F4 81 20 F4 75
Creating a lagged variable for Rate treating Factor variable as group variable −
> df$Lagged_Variable <- c(NA, df$Rate[-nrow(df)]) > df$Lagged_Variable[which(!duplicated(df$Factor))] <- NA > df Factor Rate Lagged_Variable 1 F1 12 NA 2 F1 54 12 3 F1 18 54 4 F1 26 18 5 F1 14 26 6 F2 25 NA 7 F2 81 25 8 F2 47 81 9 F2 15 47 10 F2 12 15 11 F3 96 NA 12 F3 25 96 13 F3 14 25 14 F3 34 14 15 F3 68 34 16 F4 78 NA 17 F4 42 78 18 F4 56 42 19 F4 81 56 20 F4 75 81
- Related Questions & Answers
- How to create a lagged column in an R data frame?
- How to a split a continuous variable into multiple groups in R?
- How to create a point chart for categorical variable in R?
- How to create a dummy variable in R?
- How to create a frequency column for categorical variable in an R data frame?
- How to create an ordinal variable in R?
- How to create a predictive linear regression line for a range of independent variable in base R?
- How to create a boxplot using ggplot2 for single variable without X-axis labels in R?
- How to create vertical line for X variable at median in base R plot
- How to create horizontal line for Y variable at median in base R plot?
- How to create a scatterplot with log10 of dependent variable in R?
- How to create a binary random variable in R with given probability?
- How to find the correlation matrix of groups for a data.table object in R?
- How to create a categorical variable using a data frame column in R?
- How to create a scatterplot between a variable and an equation in R?
Advertisements