关于将总和值更改为特定条件的信息

时间:2019-03-02 20:53:22

标签: r

data <- data.frame(id=c(rep(1, 4), rep(2, 3), rep(3, 3), rep(4,4)), 
           condition=c(1, 1, 0, 0, 1, 1, 0, 1, 1, 0,1,1,0,0),
           count=c(1, 2, 0, 0, 1, 2, 0, 1, 2, 0,1,2,0,0), 
           firstexosure=c(1, 0, 0, 0, 1, 0, 0, 1, 0, 0,1,0,0,0), 
           outcome=c(0, 0, 0, 1, 0, 0, 1, 0, 0, 1,0,0,1,0), 
           time=c(100, 250, 220, 300, 240, 380, 150, 200, 320, 360,100,210,220,235) ) 

使用后

data<-data%>%group_by(id,condition)%>%mutate(sumtime= sum(time))

我将每个id组中的sumtime值更改为condiotion == 1的值

data <- data.frame(id=c(rep(1, 4), rep(2, 3), rep(3, 3), rep(4,4)), 
               condition=c(1, 1, 0, 0, 1, 1, 0, 1, 1, 0,1,1,0,0),
               count=c(1, 2, 0, 0, 1, 2, 0, 1, 2, 0,1,2,0,0), 
               firstexosure=c(1, 0, 0, 0, 1, 0, 0, 1, 0, 0,1,0,0,0), 
               outcome=c(0, 0, 0, 1, 0, 0, 1, 0, 0, 1,0,0,1,0), 
               time=c(350, 350, 350, 350, 620, 620, 620, 520, 520, 520,310,310,310,310) )

我如何用R书写?

0 个答案:

没有答案