粘贴,由和data.table在r [英] paste, by and data.table in r
本文介绍了粘贴,由和data.table在r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从重新形状的数据集中粘贴字符串。我使用data.table包如下:
I'm trying to paste strings from a reshaped dataset. I'm using the data.table package as follows:
m<-data.frame(x=rep(c("a","b"),20),y=factor(sample(letters,40,replace=T)))
DT<-data.table(m)
setkey(DT,x)
DT[,paste(y,sep=","),by=x]
然而,这只给出一个新的框架,除了变量名之外,与原始框架完全相同。我想要的输出是两个连接的向量,其中的变量粘贴在一起。
However, this only gives a new frame quite identical to the original one except for the variable name. I'd like the output to be two concatenated vectors where the variables are pasted together. How can I do this?
推荐答案
为了完整起见,官方回答:
For completeness' sake, an official answer:
如果你使用粘贴(y,collapse =,)
,它应该工作。
If you use paste(y,collapse=",")
instead, it should work.
这篇关于粘贴,由和data.table在r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文