应用函数来访问不同列的先前值

时间:2017-06-26 17:48:28

标签: r

我想添加一个“数字滞后”的新列,但如果组发生更改,我不想将之前的组作为滞后数字。这使用ifelse显示如下,但是如何使用apply()?

执行此操作
mydata=data.frame(groups = c("A","A","B","B"), numbers= c(1,2,3,4))
mydata$numbers_lagged = lag(mydata$numbers, k=1)  
mydata$groups_lagged= lag(mydata$groups, k=1) 
mydata$numbers_lagged= ifelse(mydata$groups != mydata$groups_lagged,NA, mydata$numbers_lagged) #if the group does not equal the previous group then set to NA
mydata

1 个答案:

答案 0 :(得分:1)

apply比较我建议在group_by

中使用dplyr
library(dplyr)
mydata%>%group_by(groups)%>%dplyr::mutate(numbers_lagged=lag(numbers))%>%
   ungroup()%>%
   arrange(groups)%>%
   mutate(groups_lagged=lag(groups))

  groups numbers numbers_lagged groups_lagged
  <fctr>   <dbl>          <dbl>        <fctr>
1      A       1             NA            NA
2      A       2              1             A
3      B       3             NA             A
4      B       4              3             B