将重复的行转移到R中的列 [英] Transpose duplicated rows to column in R
问题描述
我有一个大的data.frame(20000+条目)这种格式:
id D1 D2
/ pre>
1 0.40 0.21
1 0.00 0.00
1 0.53 0.20
2 0.17 0.17
2 0.25 0.25
2 0.55 0.43
其中每个ID可能会重复3-20次。我想将重复的行合并到新列中,因此我的新数据框架如下所示:
id D1 D2 D3 D4 D5 D6
1 0.40 0.21 0.00 0.00 0.53 0.20
2 0.17 0.17 0.25 0.25 0.55 0.43
我已经用plyr操纵了data.frames,但是我不知道如何解决这个问题。任何帮助将不胜感激。感谢。
解决方案最好的选择是使用
code>和
dcast
从reshape2。但是在我们跳到这个选项之前,我们来看看我们还有什么可以使用的:
每个id的行是不平衡的。这将使它有点难以放入一个整齐的矩形
data.frame
。
以下是一些示例。
平衡数据:每个id有三行
mydf< - structure(list(id = c(1,1,1,2,2,2),
D1 = c(0.4,0,0.53,0.17,0.25,0.55),
D2 = c(0.21,0,0.2,0.17,0.25,0.43)),
.Names = c(id D1,D2),row.names = c(NA,6L),
class =data.frame)
mydf
#id D1 D2
# 1 1 0.40 0.21
#2 1 0.00 0.00
#3 1 0.53 0.20
#4 2 0.17 0.17
#5 2 0.25 0.25
#6 2 0.55 0.43
使用这些数据,您只需使用
aggregate
:do.call(data.frame,aggregate(。〜id,mydf,as.vector))
#id D1.1 D1.2 D1.3 D2.1 D2.2 D2.3
#1 1 0.40 0.00 0.53 0.21 0.00 0.20
#2 2 0.17 0.25 0.55 0.17 0.25 0.43
不平衡数据:某些解决方法
如果您ve为id = 2添加了第四个值,
aggregate
将不会在这里工作:mydf [7,]< - c(2,.44,.33)
do.call(data.frame,aggregate 〜id,mydf,as.vector))
#data.frame('0` = c(0.4,0,053))中的错误,'1'= c(0.17,0.25,0.55,0.44:
#参数意味着不同的行数:3,4
最好只有一个
列表
c code code code code code $ >lapply(split(mydf [-1],mydf [[1]]),function(x)unlist(x,use.names = FALSE))
#$`1`
#[1] 0.40 0.00 0.53 0.21 0.00 0.20
#
#$`2`
#[1] 0.17 0.25 0.55 0.44 0.17 0.25 0.43 0.33
#
或者,如果您坚持使用矩形 data.frame
,探索 rbind
不平衡数据的几种工具之一,例如,plyr中的 rbind.fill
:
library(plyr)
rbind.fill(lapply(split(mydf [-1],mydf [[1 ]]),
function(x)data.frame(t(unlist(x,use.names = FALSE))))
#X1 X2 X3 X4 X5 X6 X7 X8
# 1 0.40 0.00 0.53 0.21 0.00 0.20 N A NA
#2 0.17 0.25 0.55 0.44 0.17 0.25 0.43 0.33
不平衡数据:更直接的方法
或者,您可以使用 fusion
和 dcast
从reshape2如下:
library(reshape2)
x< - melt(mydf,id.vars =id)
## ^^这个没有足够的信息`dcast`
##我们还需要一个时间变量,所以使用` ave`
##根据每个ID的
##值创建一个。
x $ time< - ave(x $ id,x $ id,FUN = seq_along)
## ^^我可能会在这一点上停止。
##具有正确的ID和时间值的长数据
##往往更容易使用,并且许多
##其他功能在R中更好地使用
# #这个长数据格式。
dcast(x,id〜time,value.var =value)
#id 1 2 3 4 5 6 7 8
#1 1 0.40 0.00 0.53 0.21 0.00 0.20 NA NA
#2 2 0.17 0.25 0.55 0.44 0.17 0.25 0.43 0.33
I have a large data.frame (20000+ entries) in this format:
id D1 D2
1 0.40 0.21
1 0.00 0.00
1 0.53 0.20
2 0.17 0.17
2 0.25 0.25
2 0.55 0.43
Where each id may be duplicated 3-20 times. I would like to merge the duplicated rows into new columns, so my new data.frame looks like:
id D1 D2 D3 D4 D5 D6
1 0.40 0.21 0.00 0.00 0.53 0.20
2 0.17 0.17 0.25 0.25 0.55 0.43
I've manipulated data.frames before with plyr, but I'm not sure how to approach this problem. Any help would be appreciated.Thanks.
The best option would be to just use melt
and dcast
from "reshape2". But before we jump to that option, let's see what else we have available to us:
You mention that the number of rows per "id" is unbalanced. That would make it somewhat difficult to put into a tidy rectangular data.frame
.
Here are a few examples.
Balanced data: Three rows per "id"
mydf <- structure(list(id = c(1, 1, 1, 2, 2, 2),
D1 = c(0.4, 0, 0.53, 0.17, 0.25, 0.55),
D2 = c(0.21, 0, 0.2, 0.17, 0.25, 0.43)),
.Names = c("id", "D1", "D2"), row.names = c(NA, 6L),
class = "data.frame")
mydf
# id D1 D2
# 1 1 0.40 0.21
# 2 1 0.00 0.00
# 3 1 0.53 0.20
# 4 2 0.17 0.17
# 5 2 0.25 0.25
# 6 2 0.55 0.43
With such data, you can just use aggregate
:
do.call(data.frame, aggregate(. ~ id, mydf, as.vector))
# id D1.1 D1.2 D1.3 D2.1 D2.2 D2.3
# 1 1 0.40 0.00 0.53 0.21 0.00 0.20
# 2 2 0.17 0.25 0.55 0.17 0.25 0.43
Unbalanced data: Some workarounds
If you've added a fourth value for "id = 2", aggregate
won't work here:
mydf[7, ] <- c(2, .44, .33)
do.call(data.frame, aggregate(. ~ id, mydf, as.vector))
# Error in data.frame(`0` = c(0.4, 0, 0.53), `1` = c(0.17, 0.25, 0.55, 0.44 :
# arguments imply differing number of rows: 3, 4
It might be best to just have a list
of the resulting vector
s:
lapply(split(mydf[-1], mydf[[1]]), function(x) unlist(x, use.names=FALSE))
# $`1`
# [1] 0.40 0.00 0.53 0.21 0.00 0.20
#
# $`2`
# [1] 0.17 0.25 0.55 0.44 0.17 0.25 0.43 0.33
#
Or, if you insist on a rectangular data.frame
, explore one of the several tools to rbind
unbalanced data, for example, rbind.fill
from "plyr":
library(plyr)
rbind.fill(lapply(split(mydf[-1], mydf[[1]]),
function(x) data.frame(t(unlist(x, use.names=FALSE)))))
# X1 X2 X3 X4 X5 X6 X7 X8
# 1 0.40 0.00 0.53 0.21 0.00 0.20 NA NA
# 2 0.17 0.25 0.55 0.44 0.17 0.25 0.43 0.33
Unbalanced data: A more direct approach
Alternatively, you can use melt
and dcast
from "reshape2" as follows:
library(reshape2)
x <- melt(mydf, id.vars = "id")
## ^^ That's not enough information for `dcast`
## We need a "time" variable too, so use `ave`
## to create one according to the number of
## values per ID.
x$time <- ave(x$id, x$id, FUN = seq_along)
## ^^ I would probably actually stop at this point.
## Long data with proper ID and "time" values
## tend to be easier to work with and many
## other functions in R work more nicely with
## this long data format.
dcast(x, id ~ time, value.var = "value")
# id 1 2 3 4 5 6 7 8
# 1 1 0.40 0.00 0.53 0.21 0.00 0.20 NA NA
# 2 2 0.17 0.25 0.55 0.44 0.17 0.25 0.43 0.33
这篇关于将重复的行转移到R中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!