我有一个如下所示的数据集A.如何将数据集A转换为数据集B.数据集A在我的文件中包含超过10,000个观察值。有没有简单的方法呢?
Dataset A:
Line 1:AB 12 23
Line 2:AB 34 56
Line 3:CD 78 90
Line 4:EF 13 45
Dataset B:
Line 1:AB 12 23 34 56
Line 2:CD 78 90 NA NA
Line 3:EF 13 45 NA NA
答案 0 :(得分:2)
使用cSplit
library(splitstackshape)
library(dplyr)
DatA['new']=apply(DatA[,-1], 1, paste, collapse=",")
DatA=DatA%>%group_by(Alphabet)%>%summarise(new=paste(new,collapse=','))
cSplit(DatA, 2, drop = TRUE,sep=',')
Alphabet new_1 new_2 new_3 new_4
1: AB 12 23 34 56
2: CD 78 90 NA NA
3: EF 13 45 NA NA
数据输入
DatA <- data.frame(Alphabet = c("AB", "AB", "CD","EF"),
Value1 = c(12,34,78,13),Value2 = c(23,56,90,45),stringsAsFactors = F)