I want to classify my data based on the difference in 2 years.
I wanted to use the diff function but it does not work with negative numbers.
I get this error
'lag' and 'differences' must be integers >= 1
df$new_var <- fcase(
df$year1== df$year2, '4',
diff(df$year1, df$year2) <= 5, '3',
diff(df$ipodate1, df$ipodate2) <= 10, '2',
default = '0'
)
Is there a way I can calculate differences in absolute values
df <- data.frame(
year1 = c('1997','2008','2004','2010','2005','2007','2008'),
year2 = c('1997','2018','1988','1929','2023','2012','2009'))
So the end should look sth like this:
| year1 | year2 | new_var |
|---|---|---|
| 1997 | 1997 | 4 |
| 2008 | 20018 | 2 |
| 2004 | 1988 | 0 |
...