简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM