[英]converting different rows of a data frame to one single row in R
I have a dataset that looks like this: 我有一个看起来像这样的数据集:
CATA 1 10101
CATA 2 11101
CATA 3 10011
CATB 1 10100
CATB 2 11100
CATB 3 10011
etc. 等等
and I want to combine these different rows into a single, long row like this: 我想将这些不同的行合并为一个较长的行,如下所示:
CATA 101011110110011
CATB 101001110010011
I've tried doing this with melt() and then dcast(), but it doesn't seem to work. 我试过使用melt()然后是dcast()来执行此操作,但是它似乎不起作用。 Does anyone have some simple pieces of code to do this?
有人有一些简单的代码可以做到这一点吗?
Look at the paste
command and specifically the collapse
argument. 查看
paste
命令,特别是collapse
参数。 It's not clear what should happen if/when you have different values for the first column, so I won't venture to guess. 目前尚不清楚,如果/当您在第一列中使用不同的值时会发生什么,所以我不会冒险猜测。 Update your question if you get stuck.
如果遇到问题,请更新您的问题。
dat <- data.frame(V1 = "CATA", V2 = 1:3, V3 = c(10101, 11101, 10011))
paste(dat$V3, collapse= "")
[1] "101011110110011"
Note that you may want to convert the data to character first to prevent leading zeros from being trimmed. 请注意,您可能需要先将数据转换为字符,以防止修剪前导零。
EDIT: to address multiple values for the first column 编辑:解决第一列的多个值
Use plyr
's ddply
function which expects a data.frame as an input and a grouping variable(s). 使用
plyr
的ddply
函数,该函数期望将data.frame作为输入和分组变量。 We then use the same paste()
trick as before along with summarize()
. 然后,我们将使用与之前相同的
paste()
技巧以及summarize()
。
library(plyr)
dat <- data.frame(V1 = sample(c("CATA", "CATB"), 10, TRUE)
, V2 = 1:10
, V3 = sample(0:100, 10, TRUE)
)
ddply(dat, "V1", summarize, newCol = paste(V3, collapse = ""))
V1 newCol
1 CATA 16110
2 CATB 19308974715042
Assuming all possible elements in V1
of dat
are known, 假设
dat
V1
中所有可能的元素都是已知的,
elements <- c("CATA","CATB","CATC")
i <- 1
final_list <- c()
while (i <= length(elements)){
k <- grep(elements[i], dat$V1, ignore.case = FALSE, fixed = TRUE, value = FALSE)
m <- paste(dat$V1[k[1]], " ", paste(dat[k,3], collapse=""), sep="")
final_list <- c(final_list,m)
i=i+1
}
@Chase answer is much better ! @追的答案好多了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.