R:根据数据框的另一列中的不同信息组合列

时间:2014-10-06 20:50:31

标签: r data-manipulation

我试图为数据操作的以下目的找到一种更简单的方法。数据框如下:

"object"    "Date_In"   "Date_out"  "label" "room"  "test"
"1" "LEU_A" 6   9   "Up"    "11z"   "c"
"2" "LEU_A" 1   10  "Down"  "14x"   "c"
"3" "LEU_B" 6   8   "Up"    "11z"   "a1"
"4" "LEU_B" 10  13  "Down"  "14x"   "a1"
"5" "ALL_A" 7   8   "Up"    "11z"   "c"
"6" "ALL_A" 1   26  "Down"  "1g"    "c"
"7" "CLMIA_A"   5   15  "Up"    "11z"   "a2"
"8" "CLMIA_A"   10  10  "Down"  "14x"   "a2"
"9" "CLMIA_A"   10  12  "Down"  "13w"   "a2"

对于" Up"的所有行label(在"标签" col中),我将它与每个行组合在一起,这些行具有相同的对象名称,但是使用" Down"标签。生成的新行(最好在新表中)应该有一个包含" Date In"从标有标记的行和" Room"从下面标记的行。示例结果数据框可能如下所示:

"object"    "Date_In_Room"
"1" "LEU_A" "6_14x"
"2" "LEU_B" "6_14x" 
"3" "ALL_A" "7_1g"
"4" "CLMIA_A"   "5_14x"
"5" "CLMIA_A"   "5_13w"

请注意,标有" up"有一个以上的相关" down"标记的行,我想包括所有这些行。我可以通过首先分开上下行,然后在数据帧中逐行循环来实现,但这可能非常耗时,尤其是当我有数百个对象时。如果你有一些更简单的方法,请告诉我:))

非常感谢阅读。感谢您的帮助。

海伦

1 个答案:

答案 0 :(得分:2)

library(data.table)
DT <- as.data.table(your_data_frame)

RESULTS <- DT[, list(Date_In[label=="Up"], room[label=="Down"]), keyby=object]


RESULTS
    object V1  V2
1:   ALL_A  7  1g
2: CLMIA_A  5 14x
3: CLMIA_A  5 13w
4:   LEU_A  6 14x
5:   LEU_B  6 14x