如何在没有循环的情况下重塑R中的data.frame?

时间:2016-05-06 17:39:33

标签: r reshape

我在R中有一个var audioBufferListPtr = UnsafeMutableAudioBufferListPointer(ioData).unsafeMutablePointer.memory for i in 0 ..< Int(inBufferNumber) { var buffer: AudioBuffer = audioBufferListPtr.mBuffers } 。我需要比较两行数据,如果它们是相同的,我需要合并行并将数据合并到一列中。我觉得这是使用R时的常见需求,因此使用data.frame或其他一些软件包应该能够完成此任务。下面是数据,ddply,以及在某些代码之后它应该是什么,dat我是R的新手,所以非常感谢任何帮助。

之前:

foo.

之后:

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

quick look

编辑:

下面的解决方案适用于我提供的数据集,但是我发现这些解决方案实际上并没有按预期工作的情况。这是一个失败的data.frame的示例。请注意,detected_id列对我来说已经过时了。

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

解决方案:在重新整形data.frame之前删除detected_id列,谢谢@eddi

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

使用基础R

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