Reputation: 2426
Basically I'm looking for a diff() function that works on dataframes and that can substract one element in row n from another element in row n+1 but that is also in another column.
So, with the dataframe :
> df = data.frame(X=c("x1","x2","x3"), Y=c("y1","y2","y3"))
X Y
1 x1 y1
2 x2 y2
3 x3 y3
I would like to do df$Z=df$Y-df$Z
but with a "lag" so I get :
> df$Z
[1] NULL y2-x1 y3-x2
Without using a loop of course, I've never seen such a slow thing such as this in R...
Upvotes: 0
Views: 88
Reputation: 887241
Try (You showed some factor columns instead of numeric values). If the columns are numeric,
df$Z <- c(NA,df$Y[-1]- df$X[-nrow(df)])
Or using with
df$Z <- with(df, c(NA,Y[-1]- X[-length(X)]))
For the data provided
df$Y[-1] #removes the first observation
#[1] y2 y3
#Levels: y1 y2 y3
df$X[-nrow(df)] #removes the last observation
#[1] x1 x2
#Levels: x1 x2 x3
so that, when we take the difference, it will be y2-x1
, y3-x2
Upvotes: 0
Reputation: 206263
Now sure why you had to make things difficult in your example by using character values but
c(NA, with(df, paste(tail(Y,-1), head(X,-1), sep="-")))
show the subtractions you want to perform. if the data were actually numeric
df$Z <- c(NA, with(df, tail(Y,-1) - head(X,-1)))
should do the trick
Upvotes: 1