r - Creating a "Lagged" Data Frame -
i have following data frame , want shift first 2 columns down. basically, want see 'lagged' data frame. there seems lot on how single column (see here), nothing select of columns.
my data = d1 <- data.frame(month = c("jan", "feb", "mar", "apr", "may", "june"), conv = c(1, 3, 6, 2, 3, 8), month = c("jan", "feb", "mar", "apr", "may", "june"), visit = c( 1, 2, 4, 8, 16, 32), click = c(64, 62, 36, 5, 6, 3)) d1 desired output = d2 <- data.frame(month = c("jan", "feb", "mar", "apr", "may", "june"), conv = c(1, 3, 6, 2, 3, 8), month = c(na, "jan", "feb", "mar", "apr", "may"), visit = c( na, 1, 2, 4, 8, 16), click = c(na, 64, 62, 36, 5, 6)) d2
help?!
a cheap method:
cbind(d1[,1:2],head(rbind(na,d1),-1)[,-(1:2)])
result:
month conv month.1 visit click 1 jan 1 <na> na na 2 feb 3 jan 1 64 3 mar 6 feb 2 62 4 apr 2 mar 4 36 5 may 3 apr 8 5 6 june 8 may 16 6
Comments
Post a Comment