如何将ddply与ifelse条件结合起来?拆分,如果满足条件则转换&结合数据?

时间:2017-05-05 08:31:40

标签: r if-statement split dplyr plyr

我想知道如何将ddply与ifelse条件结合使用,如下例所示,我尝试在某一周后将y_added添加到y。我希望这是针对每个主题和年份单独完成的,因此我认为ddply可能是一个不错的选择,但如果另一个功能更好,我会很高兴知道这一点。我已经看到similar question已经得到了答案,但在这种情况下我无法弄清楚如何做到这一点

library(plyr); library(dplyr)
# Example data
year      <- c(rep(2000, 20), rep(2001, 20))
week      <- c(1:10, 1:10, 1:10, 1:10)
subject   <- c(rep("A",10), rep("B", 10), rep("A",10), rep("B", 10))
y         <- c(1:10, 21:30, 11:20, 18:27)

d1 <- data.frame(year, week, subject, y)

d_addition <- data.frame(subject = c("A","B", "A"), y_added = c(5, 12, 7), 
                        week_added = c(5, 7, 8), year = c(2000, 2000, 2001)) 

d2 = full_join(d1, d_addition)

# Attempt to add y_added to y after the week the addition occured
d3 = ddply(d2,c("year", "subject"),transform,
          y2 <- ifelse(week >= y_added, y + y_added,y))

1 个答案:

答案 0 :(得分:1)

transform适用于=,而不适用于<-。因此,将其更改为=,它应该可以正常工作

res <- plyr::ddply(d2,c("year", "subject"),transform,
                y2 = ifelse(week >= y_added, y + y_added,y))

由于我们已经在使用dplyr,因此可以使用dplyr语法

完成整个过程
library(dplyr)
res2 <- full_join(d1, d_addition) %>% group_by(year, subject) %>% 
             dplyr::mutate(y2 = ifelse(week >= y_added, y+ y_added, y))

all.equal(res, res2, check.attributes = FALSE)
#[1] TRUE