R: Combine columns based on different information in another column of a dataframe

放肆的年华 提交于 2020-01-17 04:44:06

问题


I'm trying to find an easier way for the following purpose of data manipulation. The dataframe is like this:

"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"

For all rows with "Up" label (in the "label" col), I will combine it with each of the rows, which has same object name, but with "Down" label. The resulting new row (preferably in a new table) should have a column containing both "Date In" from the up-labeled row, and "Room" from the down-labeled row. A sample result dataframe can look like:

"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"

Note that it is possible for one sample labeled with "up" to have more than one related "down" labeled rows, and I'd like to include all of them. I could do it by first separate up and down rows, and then looping row by row within the dataframe, but that can be time-consuming, especially when I have hundreds of objects. Please let me know if you have some easier methods:)

Thanks a lot for reading. I appreciate your help.

Helene


回答1:


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


来源:https://stackoverflow.com/questions/26224654/r-combine-columns-based-on-different-information-in-another-column-of-a-datafra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!