如何根据其他两列的值在 R 中创建新变量?

How to create a new variable in R based on values from two other columns?

我有一个如下所示的数据集:

每个患者在两个时间点的双耳有四行。我想创建一个新变量,它取自化疗剂量 1 的第一行和化疗剂量 2 的第二行。我想要的输出是这样的:

如何在 R 中创建这样的变量?

你能简单地mutate(),使用if_else()吗?

library(dplyr)

df %>% mutate(NEW_VARIABLE = if_else(Time_Point=="C1", Chemo_Dose1,Chemo_Dose2))

输出:

        Ear Study_ID Chemo_Dose1 Chemo_Dose2 Time_Point NEW_VARIABLE
1  Left Ear  CF41853        1200         300         C1         1200
2  Left Ear  CF41853        1200         300       Post          300
3 Right Ear  CF41854        1200         300         C1         1200
4 Right Ear  CF41854        1200         300       Post          300

输入:

structure(list(Ear = c("Left Ear", "Left Ear", "Right Ear", "Right Ear"
), Study_ID = c("CF41853", "CF41853", "CF41854", "CF41854"), 
    Chemo_Dose1 = c(1200, 1200, 1200, 1200), Chemo_Dose2 = c(300, 
    300, 300, 300), Time_Point = c("C1", "Post", "C1", "Post"
    )), class = "data.frame", row.names = c(NA, -4L))

这个回答能解决你的问题吗?

### 1- data simulation
df <- data.frame(dose1=rep(1200, 4), dose2=rep(300, 4), time=c("C1", "Post", "C1", "Post"))

### 2- computing new variable based on time endpoint
df$newVariable <- ifelse(df$time=="C1", df$dose1, df$dose2)

data.table 选项使用 fifelse(感谢@langtang 创建数据):

library(data.table)
setDT(df)[, new := fifelse(Time_Point == "C1", Chemo_Dose1, Chemo_Dose2)]
df

输出:

         Ear Study_ID Chemo_Dose1 Chemo_Dose2 Time_Point  new
1:  Left Ear  CF41853        1200         300         C1 1200
2:  Left Ear  CF41853        1200         300       Post  300
3: Right Ear  CF41854        1200         300         C1 1200
4: Right Ear  CF41854        1200         300       Post  300