简体   繁体   中英

Creating a new variable from values in existing rows and columns

I would like to create a new variable that is labelled change_index. This variable is outcome1 at time 3 - outcome 1 at time 1 / outcome1 at time 1.

How do I go about doing this? I tried doing the following

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

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

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

but I get the following error

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

I realize this happens because the new data frame is smaller since it contains less rows. Should I just create a new data frame with change index? How do I go about doing this?

I have to calculate this change index for many variables in columns (many outcomes). Is there a way to automate this process?

Thanks for reading.

   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

EDIT 1

Tried doing the following as suggested below, I changed the names according to my data

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

I get the following error

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

EDIT 2

Tried the new suggestion, still the same error

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

Instead of doing the filter , we create new variables

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM