在R中重塑数据帧

亚尔鲍尔

我有一个数据框,需要重塑形状,将单列中的重复值转换为具有多个数据列的单行。我知道这应该很简单,但是我不知道该怎么做,以及我需要使用的许多重塑/铸造功能中的哪一个。

我的部分数据如下所示:

 Source       ID info
1     In   842701    1
2    Out   842701    1
3     In 21846591    2
4    Out 21846591    2
5     In 22181760    3
6     In 39338740    4
7    Out     9428    5

我想使它看起来像这样:

        ID In Out info
1   842701  1   1    1
2 21846591  1   1    2
3 22181760  1   0    3
4 39338740  1   0    4
5     9428  0   1    5

依此类推,同时保留所有其余列(对于给定条目而言相同)。

我真的很感谢您的帮助。TIA。

阿克伦

这是一种使用方法 reshape2

library(reshape2)
res <- dcast(transform(df, indx=1, ID=factor(ID, levels=unique(ID))),
                                   ID~Source, value.var="indx", fill=0)


res
#        ID In Out
#1   842701  1   1
#2 21846591  1   1
#3 22181760  1   0
#4 39338740  1   0
#5     9428  0   1

或者

res1 <- as.data.frame.matrix(table(transform(df,
                 ID=factor(ID, levels=unique(ID)))[,2:1]))

更新

dcast(transform(df1, indx=1, ID=factor(ID, levels=unique(ID))),
                                 ...~Source, value.var="indx", fill=0)

 #        ID info In Out
 #1   842701    1  1   1
 #2 21846591    2  1   1
 #3 22181760    3  1   0
 #4 39338740    4  1   0
 #5     9428    5  0   1

你也可以使用reshapebase R

 res2 <- reshape(transform(df1, indx=1), idvar=c("ID", "info"),
                              timevar="Source", direction="wide")

 res2[,3:4][is.na(res2)[,3:4]] <- 0
 res2
 #        ID info indx.In indx.Out
 #1   842701    1       1        1
 #3 21846591    2       1        1
 #5 22181760    3       1        0
 #6 39338740    4       1        0
 #7     9428    5       0        1

数据

df <- structure(list(Source = c("In", "Out", "In", "Out", "In", "In", 
"Out"), ID = c(842701L, 842701L, 21846591L, 21846591L, 22181760L, 
39338740L, 9428L)), .Names = c("Source", "ID"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7"))


df1 <- structure(list(Source = c("In", "Out", "In", "Out", "In", "In", 
 "Out"), ID = c(842701L, 842701L, 21846591L, 21846591L, 22181760L, 
 39338740L, 9428L), info = c(1L, 1L, 2L, 2L, 3L, 4L, 5L)), .Names = c("Source", 
 "ID", "info"), class = "data.frame", row.names = c("1", "2", 
 "3", "4", "5", "6", "7"))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章