基于作为另一个数据帧中的值存储的列名称的一个数据帧中的查找值

时间:2014-01-27 19:10:24

标签: r replace match data.table lookup

请参阅下面的可重复(剪切+粘贴)示例。实际数据集对11000人进行了超过4000次连续观察。我需要创建列A,B,C等,显示“药物”变量X,Y,Z等的NUMBER,其对应于“疾病”变量的特定值的第一次出现。这些数字指的是使用特定药物(开始,停止,增加剂量等)采取的行动。“疾病”变量是指疾病是否在包含耀斑和缓解等多个阶段的疾病中爆发。

例如:

Animal <- c("aardvark", "1", "cheetah", "dromedary", "eel", "1", "bison", "cheetah", "dromedary",     
"eel")
Plant <- c("apple_tree", "blossom", "cactus", "1", "bronze", "apple_tree", "bronze", "cactus",     
"dragonplant", "1")
Mineral <- c("amber", "bronze", "1", "bronze", "emerald", "1", "bronze", "bronze", "diamond",     
"emerald")
Bacteria <- c("acinetobacter", "1", "1", "d-strep", "bronze", "acinetobacter", "bacillus", 
"chlamydia", "bronze", "enterobacter" )
AnimalDrugA <- c(1, 11, 12, 13, 14, 15, 16, 17, 18, 19)
AnimalDrugB <- c(20, 1, 22, 23, 24, 25, 26, 27, 28, 29)
PlantDrugA <- c(301, 302, 1, 304, 305, 306, 307, 308, 309, 310)
PlantDrugB <- c(401, 402, 1, 404, 405, 406, 407, 408, 409, 410)
MineralDrugA <- c(1, 2, 3, 4, 1, 6, 7, 8, 9, 10)
MineralDrugB <- c(11, 12, 13, 1, 15, 16, 17, 18, 19, 20)
BacteriaDrugA <- c(1, 2, 3, 4, 5, 6 , 7, 8, 9, 1)
BacteriaDrugB <- c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
dummy_id <- c(1001, 2002, 3003, 4004, 5005, 6006, 7007, 8008, 9009, 10101)


Elements <- data.frame(dummy_id, Animal, Plant, Mineral, Bacteria, AnimalDrugA, AnimalDrugB,          
PlantDrugA, PlantDrugB, MineralDrugA, MineralDrugB, BacteriaDrugA, BacteriaDrugB)
ds <- Elements[,order(names(Elements))]
ds  #Got it in alphabetical order... The real data set will be re-ordered chronologically


#Now I want the first occurrence of the word "bronze" for each id
# for each subject 1 through 10.  (That is, "bronze" corresponds to start of disease flare.)
first.bronze <- colnames(ds)[apply(ds,1,match,x="bronze")]
first.bronze

#Now, I want to find the number in the DrugA, DrugB variable that corresponds to the first            
#occurrence of bronze.
#Using the alphabetically ordered data set, the answer should be:
#dummy_id  DrugA  DrugB
#1...      NA   NA
#2...      2    12
#3...     NA    NA
#4...     4     1
#5...     5     6
#6...    NA    NA
#7...    7     17
#8...    8     18
#9...    9     2
#10...    NA    NA
#Note that all first occurrences of "bronze"
# are in Mineral or Bacteria.
#As a first step, join first.bronze to the ds
ds$first.bronze <- first.bronze 
ds

#Make a new ds where those who have an NA for first.bronze are excluded:
ds2 <- ds[complete.cases(ds$first.bronze),]
ds2


# Create a template data frame
out <- data.frame(matrix(nr = 1, nc = 3))
colnames(out) <- c("Form Number", "DrugA", "DrugB")  # Gives correct column names
out

#Then grow the data frame...yes I realize potential slowness of computation
test <- for(i in ds2$first.bronze){
    data <- rbind(colnames(ds2)[grep(i, names(ds2), ignore.case = FALSE, fixed = TRUE)])
    colnames(data) <- c("Form Number", "DrugA", "DrugB")  # Gives correct column names
    out <- rbind(out, data)
}
out

#Then delete the first row of NAs
out <- na.omit(out)
out

#Then add the appropriate dummy_ids
dummy_id <- ds2$dummy_id
out_with_ids <- as.data.frame(cbind(dummy_id, out))
out_with_ids

现在我被卡住了。我将来自ds2的列的名称列为out_with_ids数据集中的药物A,药物B的值。我已经彻底搜索了Stack Overflow,但基于匹配,合并,替换和data.table包的解决方案似乎不起作用。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为这里的问题是数据格式。我建议你把它存放在“长”表中,如下所示:

library(data.table)
dt <- data.table(dummy_id = rep(dummy_id, 4),
                 type = rep(c("Animal", "Bacteria", "Mineral", "Plant"), each = 10),
                 name = c(Animal, Bacteria, Mineral, Plant),
                 drugA = c(AnimalDrugA, BacteriaDrugA, MineralDrugA, PlantDrugA),
                 drugB = c(AnimalDrugB, BacteriaDrugB, MineralDrugB, PlantDrugB))

然后过滤和执行其他操作要容易得多。例如,

dt[name == "bronze"][order(dummy_id)]

坦率地说,我不确定我到底想要达到什么目的。