[英]distributing data.frame data across multiple rows in r
I'm using R 3.5.1 我正在使用R 3.5.1
I think this is a simple issue, but I'm not super familiar with R. 我认为这是一个简单的问题,但是我对R并不是很熟悉。
I have a data.frame object that looks like this 我有一个看起来像这样的data.frame对象
COL1 COL2 COL3
A blah 3
A abc 4
A def 42
B xyz 10
B aaa 3
C pdq 19
I want to transform the data.table to look like this 我想转换data.table看起来像这样
COLA COLACount COLB COLBCount COLC COLCCount
blah 3 xyz 10 pdq 19
abc 4 aaa 3
def 42
I'm not sure where to begin with this (or what to call it). 我不确定从哪里开始(或称呼它)。 I have considered doing the following:
我已经考虑过执行以下操作:
But I have a feeling that there might be an r package/method that simplifies this procedure. 但是我感觉可能存在简化程序的r包/方法。
Thank you for any suggestions. 感谢您的任何建议。
cbind.fill <- function(...){
nm <- list(...)
nm <- lapply(nm, as.matrix)
n <- max(sapply(nm, nrow))
do.call(cbind, lapply(nm, function (x)
rbind(x, matrix(, n-nrow(x), ncol(x)))))
} #code from package rowr
do.call(cbind.fill, split(dt, dt$COL1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.