根据现有行和列中的值创建新变量

时间:2018-09-21 15:29:39

标签: r dplyr mutate

我想创建一个标记为change_index的新变量。此变量是时间3的结果1-时间1的结果1 /时间1的结果1。

我该怎么做?我尝试执行以下

outcome1t0 <- data %>%
filter(time == "1") %>%
select(outcome1)

outcome1t12 <- data %>%
filter(time == "3") %>%
select(outcome1)

data$newvariable <- (outcome1t0 - outcome1t12) / outcome1t0

但出现以下错误

Error in `$<-.data.frame`(`*tmp*`, bicind, value = list(bicep = c(13.3591525423729,  : 
replacement has 20 rows, data has 60

我意识到这是因为新数据帧较小,因为它包含的行较少。我是否应该仅创建具有更改索引的新数据框?我该怎么做?

我必须为列中的许多变量(很多结果)计算此变化指数。有没有办法使这个过程自动化?

感谢您的阅读。

   subject treatment time outcome1 outcome2
1       1         a    1       80       15
2       1         a    2       75       14
3       1         a    3       74       12
4       2         b    1       90       16
5       2         b    2       81       15
6       2         b    3       76       15

编辑1

尝试按照以下建议进行以下操作,我根据数据更改了名称

ancestral1 %>%
group_by(subject) %>% 
mutate(bicep0 = bicep[time == 0],
     bicep12 = bicep[time == 12], 
     bicepind = (bicep12 - bicep0) / bicep12)

我收到以下错误

Error in mutate_impl(.data, dots) : 
Column `bicep0` must be length 1 (the group size), not 0

编辑2

尝试了新建议,仍然是相同的错误

ancestral1 %>% 
group_by(subject) %>% 
mutate(bicep0 = if(any(time == 5)) bicep[time == 5] else NA, 
     bicep12 = bicep[time == 3], 
     bicepind = (bicep0 - bicep12) / bicep0)

Error in mutate_impl(.data, dots) : 
Column `bicep12` must be length 1 (the group size), not 0

1 个答案:

答案 0 :(得分:1)

我们创建新变量而不是进行filter

data %>%
  group_by(subject) %>% 
  mutate(outcome1t0 = outcome1[time == 1],
       outcome1t2 = outcome1[time == 3], 
       newvariable = (outcome1t0 - outcome1t2) / outcome1t0) %>%
  select(-outcome1t0, -outcome1t2)
# A tibble: 6 x 6
# Groups:   subject [2]
#  subject treatment  time outcome1 outcome2 newvariable
#    <int> <chr>     <int>    <int>    <int>       <dbl>
#1       1 a             1       80       15       0.075
#2       1 a             2       75       14       0.075
#3       1 a             3       74       12       0.075
#4       2 b             1       90       16       0.156
#5       2 b             2       81       15       0.156
#6       2 b             3       76       15       0.156