根据ID

时间:2017-02-07 13:05:51

标签: r

我在临床试验中列出了具有唯一ID(P1-35)的参与者。一些是控制(P15,P16,P29-P35)和一些非控制(所有其余的)。

我有一个带有患者生理反应的data.frame,例如SkinTemp和HeartRate。我一直在绘制所有数据,但希望将其子集化为Control vs Non-control,以便能够单独查看并单独绘制。

是否有办法根据参与者是控制还是非控制来添加额外的Ns和Cs列?

编辑:新数据

    dput(head(data.frame(lp2),10))
structure(list(id = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), .Label = c("1", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "2", "20", "21", "22", "23", "24", "25", 
"26", "27", "28", "29", "3", "30", "31", "32", "33", "34", "4", 
"5", "6", "7", "8", "9"), class = "factor"), Time = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0), SkinTemp = c(27.781, 27.78, 27.779, 27.779, 
27.778, 27.777, 27.776, 27.775, 27.775, 27.774), HeartRate = c(70, 
70, 70, 70, 70, 70, 70, 70, 70, 70), RespirationRate = c(10, 
10, 10, 10, 10, 10, 10, 10, 10, 10), HeartRateZero = c(39.764, 
39.764, 39.764, 39.764, 39.764, 39.764, 39.764, 39.764, 39.764, 
39.764), HeartRateZeroNorm = c(0.273998277347115, 0.273998277347115, 
0.273998277347115, 0.273998277347115, 0.273998277347115, 0.273998277347115, 
0.273998277347115, 0.273998277347115, 0.273998277347115, 0.273998277347115
), RespirationRateZero = c(6.404, 6.404, 6.404, 6.404, 6.404, 
6.404, 6.404, 6.404, 6.404, 6.404), RespirationRateZeroNorm = c(0.158766362554542, 
0.158766362554542, 0.158766362554542, 0.158766362554542, 0.158766362554542, 
0.158766362554542, 0.158766362554542, 0.158766362554542, 0.158766362554542, 
0.158766362554542), SkinTempZero = c(0.43, 0.429000000000002, 
0.428000000000001, 0.428000000000001, 0.427, 0.426000000000002, 
0.425000000000001, 0.423999999999999, 0.423999999999999, 0.423000000000002
), SkinTempZeroNorm = c(0.0600307133882451, 0.0598911070780402, 
0.0597515007678348, 0.0597515007678348, 0.0596118944576294, 0.0594722881474245, 
0.0593326818372191, 0.0591930755270137, 0.0591930755270137, 0.0590534692168088
), TimeZero = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), TimeZeroNorm = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), Segment = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L), TimeLower = c(-Inf, -Inf, -Inf, -Inf, -Inf, 
-Inf, -Inf, -Inf, -Inf, -Inf), TimeUpper = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0)), .Names = c("id", "Time", "SkinTemp", "HeartRate", 
"RespirationRate", "HeartRateZero", "HeartRateZeroNorm", "RespirationRateZero", 
"RespirationRateZeroNorm", "SkinTempZero", "SkinTempZeroNorm", 
"TimeZero", "TimeZeroNorm", "Segment", "TimeLower", "TimeUpper"
), row.names = c(NA, 10L), class = "data.frame")

enter image description here

1 个答案:

答案 0 :(得分:1)

将@Sotos的注释转换为具有可重现的单列数据帧的答案。

df <- data.frame(id = c(1:35))
df$Group <- ifelse(df$id %in% c(15:16, 29:35), "C", "N")

或者,dplyr::mutate

library(dplyr)
df %>% mutate(Group = if_else(id %in% c(15:16, 29:35), "C", "N"))

只需将df替换为您自己的数据框。