通过给定的序列/语句

时间:2017-01-19 15:31:31

标签: r for-loop

我想为分析创建一个样本数据集:

set.seed(123)

d <- data.frame(ID=rep(1:10, each = 8),
                AGE=rep(sample(20:40, size=10),each=8),
                YEAR=rep(2011:2014, 10, each = 2),
                HAND=rep(c("LEFT","RIGHT"), 40),
                OUTCOME=(rnorm(80)) )

> d[1:8,]
  ID AGE YEAR  HAND    OUTCOME
1  1  26 2011  LEFT  1.7150650
2  1  26 2011 RIGHT  0.4609162
3  1  26 2012  LEFT -1.2650612
4  1  26 2012 RIGHT -0.6868529
5  1  26 2013  LEFT -0.4456620
6  1  26 2013 RIGHT  1.2240818
7  1  26 2014  LEFT  0.3598138
8  1  26 2014 RIGHT  0.4007715

ID将成为主题,AGE是主题的年龄,YEAR是衡量的年份,HAND是左手或右手,{{ 1}}是衡量结果的一部分。

现在我意识到每个受试者的OUTCOME理论上应该增加一年AGE这个受试者被测量,即:

YEAR

我提出了这个解决方案:

26,26,27,27,28,28,29,29

问题:我想知道是否有更有效的方法来做到这一点?例如,是否可以添加&#34;更正&#34;在上面的age <- unique(d$AGE) AGE2=c() for(i in 1:10){ a <- rep(age[i]+0:3, each=2) AGE2 <- c(AGE2,a) } d$AGE2 <- AGE2 d[1:8,] > d[1:8,] ID AGE YEAR HAND OUTCOME AGE2 1 1 26 2011 LEFT 1.7150650 26 2 1 26 2011 RIGHT 0.4609162 26 3 1 26 2012 LEFT -1.2650612 27 4 1 26 2012 RIGHT -0.6868529 27 5 1 26 2013 LEFT -0.4456620 28 6 1 26 2013 RIGHT 1.2240818 28 7 1 26 2014 LEFT 0.3598138 29 8 1 26 2014 RIGHT 0.4007715 29 函数中马上年龄?

3 个答案:

答案 0 :(得分:1)

我们可以使用duplicated

执行此操作
library(dplyr)
res <-  d %>% 
           group_by(ID) %>%
           mutate(AGE2 =  AGE + cumsum(!duplicated(YEAR))-1)
head(res)

#     ID   AGE  YEAR   HAND    OUTCOME  AGE2
#  <int> <int> <int> <fctr>      <dbl> <dbl>
#1     1    26  2011   LEFT  1.7150650    26
#2     1    26  2011  RIGHT  0.4609162    26
#3     1    26  2012   LEFT -1.2650612    27
#4     1    26  2012  RIGHT -0.6868529    27
#5     1    26  2013   LEFT -0.4456620    28
#6     1    26  2013  RIGHT  1.2240818    28

答案 1 :(得分:1)

使用dplyr,您只需按IDHAND进行分组:

d %>% group_by(ID, HAND) %>% mutate(AGE2 = AGE + (0:(length(AGE)-1)))
Source: local data frame [80 x 6]
Groups: ID, HAND [20]

      ID   AGE  YEAR   HAND    OUTCOME  AGE2
   <int> <int> <int> <fctr>      <dbl> <int>
1      1    26  2011   LEFT  1.7150650    26
2      1    26  2011  RIGHT  0.4609162    26
3      1    26  2012   LEFT -1.2650612    27
4      1    26  2012  RIGHT -0.6868529    27
5      1    26  2013   LEFT -0.4456620    28
6      1    26  2013  RIGHT  1.2240818    28
7      1    26  2014   LEFT  0.3598138    29
8      1    26  2014  RIGHT  0.4007715    29
9      2    35  2011   LEFT  0.1106827    35
10     2    35  2011  RIGHT -0.5558411    35
# ... with 70 more rows

答案 2 :(得分:1)

使用data.table,您可以使用rep和一点代数。

library(data.table)
setDT(d)
d[, AGE2 := AGE + rep(0L:((.N-1)/2L), each=2), by=ID][]
    ID AGE YEAR  HAND      OUTCOME AGE2
 1:  1  26 2011  LEFT  1.715064987   26
 2:  1  26 2011 RIGHT  0.460916206   26
 3:  1  26 2012  LEFT -1.265061235   27
 4:  1  26 2012 RIGHT -0.686852852   27
 5:  1  26 2013  LEFT -0.445661970   28
 6:  1  26 2013 RIGHT  1.224081797   28
 7:  1  26 2014  LEFT  0.359813827   29
 8:  1  26 2014 RIGHT  0.400771451   29
 9:  2  35 2011  LEFT  0.110682716   35
10:  2  35 2011 RIGHT -0.555841135   35
11:  2  35 2012  LEFT  1.786913137   36
12:  2  35 2012 RIGHT  0.497850478   36
...

这里,AGE2是通过将AGE添加到rep(0L:((.N-1)/2L), each=2)来构造的,UISwipeGestureRecognizer通过观察数减去1,除以2计算0。对于每个ID,by语句重复此操作。