0
我正嘗試將特定值從一行移動到列中。R:字符串中的逗號會干擾新列
數據集:
table<- structure(list(V1 = c(1931432L, 1931432L, 1931434L, 1931434L, 1931434L), V2 = c(9548326L, 9548327L, 9551339L, 9551338L, 9551337L), V3 = c("D", "N", "N", "D", "D"), V4 = c(1L, 1L, 1L, 1L, 1L), V5 = c(NA, NA, NA, NA, NA), V6 = c("This belongs in column 1", "This belongs in column 2", "This belongs in column 1", "This belongs in, column 2", "This belongs in column 3")), .Names = c("V1", "V2", "V3", "V4", "V5", "V6"), class = "data.frame", row.names = c(NA, -5L))
目前我的代碼:
tableNEW <-subset(table, select = c(1,2,6))
tableEND <- aggregate(.~ V1, data=tableNEW, FUN=function(x) paste(sort(x), collapse="|"))
是生產:
V1 | V2 | V3
1|1931432 |9548326, 9548327 | "This belongs in column 1", "This belongs in column 2"
2|1931434 |9551339, 9551338, 9551337 | "This belongs in column 1", "This belongs in, column 2", "This belongs in column 3"
理想情況下,我在尋找:
V1 | V2 | V3 |V4 |V5
1|1931432 |9548326, 9548327 | "This belongs in column 1"| "This belongs in column 2"|
2|1931434 |9551339, 9551338, 9551337 | "This belongs in column 1"| "This belongs in, column 2"| "This belongs in column 3"
我嘗試過其他方法,試圖在產生什麼之後工作,但[2,V3]中的血腥逗號奇蹟般地顯示出混亂的分裂。我需要在決賽桌中保留文本內的逗號。
是否有一種有效的方法,將摺疊步驟與列創建結合起來?
+1我喜歡這個'seq_along'和'ave'一起使用,但也使用'setNames'。我從未使用過的東西。太讚了。謝謝你提出這個建議。 – 2014-10-09 12:15:16
謝謝@akrun。我仍然有很多東西需要學習。 :-) – Aaron 2014-10-09 12:59:30
@Aaron很高興幫助。 – akrun 2014-10-09 15:02:01