简体   繁体   中英

mutate new continuous variable in dplyr

I have a dataset over time of patients with declining concentration of biomarker x. I am trying to find the first occasion that the percentage of peak x falls below 25%, or where the absolute value of x falls below 34 for men or 16 for women. I have done the % part:

df25 <- df %>% group_by(id) %>% summarise(x_25 = which(x_frac <=25)[1])

but I was wondering how you could combine the absolute values into this? I've tried the following, but it doesn't work:

df25a <- df %>% group_by(id) %>% 
  mutate(x_25a = ifelse(x_frac>=25 & x<=34, day, NA))

I would be very grateful for your expertise, bw Annemarie

id  day x   x_frac  sex    "hoped for x_25"
1   0   1935    100 0                 2
1   1   1039    54  0                 2
1   2   308     16  0                 2
1   3   112     6   0                 2  
2   0   31      100 1                 1  
2   1   11      35  1                 1   
3   0   204     100 0                 NA   
3   1   178     87  0                 NA    

Your code seems to make sense, maybe something like this? (this does not match your hoped col but matches your description, I think)

df25a <- df %>% group_by(id) %>% 
  mutate(x_25a = ifelse(sex==0,ifelse(x_frac<=25 | x<=34, day, NA),
                               ifelse(x_frac<=25 | x<=16, day, NA)))

And the summary version:

df25 <- df %>% group_by(id) %>% summarise(x_25 = ifelse(sex[1]==0,which(x_frac <=25 | x<=34)[1],
                                                                  which(x_frac <=25 | x<=16)[1]))

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