在数据帧的特定列之间按行连接
我有一个包含列的数据框,当将其连接(按行)作为字符串时,我可以将数据框分区为所需的形式。
> str(data)
'data.frame': 680420 obs. of 10 variables:
$ A : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ B : chr "2011-01-26" "2011-01-27" "2011-02-09" "2011-02-10" ...
$ C : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ D : chr "AAA" "AAA" "BCB" "CCC" ...
$ E : chr "A00001" "A00002" "B00002" "B00001" ...
$ F : int 9 9 37 37 37 37 191 191 191 191 ...
$ G : int NA NA NA NA NA NA NA NA NA NA ...
$ H : int 4 4 4 4 4 4 4 4 4 4 ...
对于每一行,我想将 F、E、D 和 C 列中的数据连接成一个字符串(使用下划线字符作为分隔符)。以下是我对此的不成功尝试:
data$id <- sapply(as.data.frame(cbind(data$F,data$E,data$D,data$C)), paste, sep="_")
以下是不期望的结果:
> str(data)
'data.frame': 680420 obs. of 10 variables:
$ A : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ B : chr "2011-01-26" "2011-01-27" "2011-02-09" "2011-02-10" ...
$ C : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ D : chr "AAA" "AAA" "BCB" "CCC" ...
$ E : chr "A00001" "A00002" "B00002" "B00001" ...
$ F : int 9 9 37 37 37 37 191 191 191 191 ...
$ G : int NA NA NA NA NA NA NA NA NA NA ...
$ H : int 4 4 4 4 4 4 4 4 4 4 ...
$ id : chr [1:680420, 1:4] "9" "9" "37" "37" ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "V1" "V2" "V3" "V4"
任何帮助将不胜感激。
I have a data frame with columns that, when concatenated (row-wise) as a string, would allow me to partition the data frame into a desired form.
> str(data)
'data.frame': 680420 obs. of 10 variables:
$ A : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ B : chr "2011-01-26" "2011-01-27" "2011-02-09" "2011-02-10" ...
$ C : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ D : chr "AAA" "AAA" "BCB" "CCC" ...
$ E : chr "A00001" "A00002" "B00002" "B00001" ...
$ F : int 9 9 37 37 37 37 191 191 191 191 ...
$ G : int NA NA NA NA NA NA NA NA NA NA ...
$ H : int 4 4 4 4 4 4 4 4 4 4 ...
For each row, I would like to concatenate the data in columns F, E, D, and C into a string (with the underscore character as separator). Below is my unsuccessful attempt at this:
data$id <- sapply(as.data.frame(cbind(data$F,data$E,data$D,data$C)), paste, sep="_")
And below is the undesired result:
> str(data)
'data.frame': 680420 obs. of 10 variables:
$ A : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ B : chr "2011-01-26" "2011-01-27" "2011-02-09" "2011-02-10" ...
$ C : chr "2011-01-26" "2011-01-26" "2011-02-09" "2011-02-09" ...
$ D : chr "AAA" "AAA" "BCB" "CCC" ...
$ E : chr "A00001" "A00002" "B00002" "B00001" ...
$ F : int 9 9 37 37 37 37 191 191 191 191 ...
$ G : int NA NA NA NA NA NA NA NA NA NA ...
$ H : int 4 4 4 4 4 4 4 4 4 4 ...
$ id : chr [1:680420, 1:4] "9" "9" "37" "37" ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "V1" "V2" "V3" "V4"
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
一下。矢量化代码的优点在于您不需要逐行循环或循环等效的 *apply 函数。
编辑更好的是
Try
instead. The beauty of vectorized code is that you do not need row-by-row loops, or loop-equivalent *apply functions.
Edit Even better is
使用
tidyr
包的unite
:第一个参数是所需的名称,接下来的所有参数都是
sep
- 要连接的列。Use
unite
oftidyr
package:First parameter is the desired name, all next up to
sep
- columns to concatenate.stringr::str_c()
或paste()
都可以。否则
(
stringr
在大型数据集上性能更好)Either
stringr::str_c()
orpaste()
will work.or else
(
stringr
is better performance on large datasets)