2016-05-06 49 views
0

我在R有一個data.frame我需要比較兩行數據,如果它們是相同的,我需要合併行並將數據合併到一列中。我覺得這是一個共同的需要,當使用R所以使用ddply或其他包應該能夠完成這項任務。下面是數據原樣,dat,它在一些代碼後應該是什麼樣的,foo.我是新來的,所以任何幫助都非常感謝。如何在R中重塑一個data.frame而沒有循環?

前:

dat <- structure(list(detected_id = c(11, 11, 4), reviewer_name = c("mike", 
"mike", "john"), created_at = c("2016-05-04 10:02:45", "2016-05-04 10:02:45", 
"2016-05-04 10:02:45"), stage = c(2L, 2L, 1L), V7 = c("Detected Organism: Staphylococcus Aureus, Comment: Looks good", 
"Detected Organism: Staphylococcus Aureus, Comment: Note 1", 
"Detected Organism: Human Adenovirus 7, Comment: test")), .Names = c("detected_id", 
"reviewer_name", "created_at", "stage", "V7"), row.names = c(NA, 
-3L), class = "data.frame") 

後:

foo <- structure(list(detected_id = c(11L, 4L), reviewer_name = c("mike", 
"john"), created_at = structure(c(1L, 1L), .Label = "5/4/16 10:02", class = "factor"), 
    stage = c(2L, 1L), V7 = structure(c(2L, 1L), .Label = c("Detected Organism: Human Adenovirus 7, Comment: test", 
    "Detected Organism: Staphylococcus Aureus, Comment: Looks good; Detected Organism: Staphylococcus Aureus, Comment: Note 1" 
    ), class = "factor")), .Names = c("detected_id", "reviewer_name", 
"created_at", "stage", "V7"), row.names = c(NA, -2L), class = "data.frame") 

quick look

編輯:

下面我提供的數據集工作的解決方案,但我發現的情況下,這些解決方案實際上並不像預期的那樣工作。這是失敗的data.frame的一個例子。請注意,detected_id列對我來說已經過時了。

dat <- structure(list(detected_id = c(11, 11, 11, 11, 12, 4), reviewer_name = c("Mike", 
"Mike", "Mike", "Mike", "John", "John"), created_at = c("2016-05-04 10:02:45", 
"2016-05-04 10:02:45", "2016-05-04 10:02:45", "2016-05-04 10:02:45", 
"2016-05-04 10:02:45", "2016-05-04 10:02:45"), stage = c(2L, 
3L, 2L, 3L, 1L, 1L), V7 = c("Detected Organism: Staphylococcus Aureus, Comment: Looks good", 
"Detected Organism: Staphylococcus Aureus, Comment: Looks good", 
"Detected Organism: Staphylococcus Aureus, Comment: Note 1", 
"Detected Organism: Staphylococcus Aureus, Comment: Note 1", 
"Detected Organism: Stenotrophomonas Maltophilia, Comment: new note", 
"Detected Organism: Human Adenovirus 7, Comment: test")), .Names = c("detected_id", 
"reviewer_name", "created_at", "stage", "V7"), row.names = c(NA, 
-6L), class = "data.frame") 

SOLUTION:重塑data.frame之前刪除detected_id柱,由於使用@eddi

回答

3
library(data.table) 

setDT(dat)[, paste(V7, collapse = "; ") 
      , by = .(detected_id, reviewer_name, created_at, stage)] 
# detected_id reviewer_name   created_at stage 
#1:   11   mike 2016-05-04 10:02:45  2 
#2:   4   john 2016-05-04 10:02:45  1 
#                               V1 
#1: Detected Organism: Staphylococcus Aureus, Comment: Looks good; Detected Organism: Staphylococcus Aureus, Comment: Note 1 
#2:                  Detected Organism: Human Adenovirus 7, Comment: test 
+0

良好的解決方案,按預期工作。謝謝! – webDevleoper101

+0

查看我編輯的 – webDevleoper101

+1

@ webDevleoper101我不確定「失敗」對您意味着什麼。它完全按預期工作。有一點不清楚你所希望的 - 也許你想從''by'中取出'detected_id'。 – eddi

0

基礎R

with(dat, aggregate(V7,list(detected_id=detected_id, reviewer_name=reviewer_name, created_at=created_at, stage=stage),paste,collapse=' ')) 
+0

我更喜歡你的解決方案,因爲它只是基礎R,請檢查我剛剛編輯的編輯 – webDevleoper101