R将逗号分隔的单元格分隔成行和笛卡尔积 [英] R separate comma separated cells into rows and Cartesian product
本文介绍了R将逗号分隔的单元格分隔成行和笛卡尔积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我下面有 mydf
数据框。我想拆分包含逗号分隔数据的任何单元格并将其放入行中。我正在寻找类似于以下 y
的数据框。我如何在几个步骤中有效地做到这一点?目前,我一次在同一列上使用 cSplit
函数。
I have mydf
data frame below. I want to split any cell that contains comma separated data and put it into rows. I am looking for a data frame similar to y
below. How could i do it efficiently in few steps? Currently i am using cSplit
function on one column at a time.
我尝试了 cSplit (mydf,c( name, new),,,direction = long)
,但这没用
library(splitstackshape)
mydf=data.frame(name = c("AB,BW","x,y,z"), AB = c('A','B'), new=c("1,2,3","4,5,6,7"))
mydf
x=cSplit(mydf, c("name"), ",", direction = "long")
x
y=cSplit(x, c("new"), ",", direction = "long")
y
推荐答案
有时, for
循环完全可以在R中使用。这是其中之一。尝试:
There are times when a for
loop is totally fine to work with in R. This is one of those times. Try:
library(splitstackshape)
cols <- c("name", "new")
for (i in cols) {
mydf <- cSplit(mydf, i, ",", "long")
}
mydf
## name AB new
## 1: AB A 1
## 2: AB A 2
## 3: AB A 3
## 4: BW A 1
## 5: BW A 2
## 6: BW A 3
## 7: x B 4
## 8: x B 5
## 9: x B 6
## 10: x B 7
## 11: y B 4
## 12: y B 5
## 13: y B 6
## 14: y B 7
## 15: z B 4
## 16: z B 5
## 17: z B 6
## 18: z B 7
这是一个使用稍大一点数据的小测试:
Here's a small test using slightly bigger data:
# concat.test = sample data from "splitstackshape"
test <- do.call(rbind, replicate(5000, concat.test, FALSE))
fun1 <- function() {
cols <- c("Likes", "Siblings")
for (i in cols) {
test <- cSplit(test, i, ",", "long")
}
test
}
fun2 <- function() {
test %>%
separate_rows("Likes") %>%
separate_rows("Siblings")
}
system.time(fun1())
# user system elapsed
# 3.205 0.056 3.261
system.time(fun2())
# user system elapsed
# 11.598 0.066 11.662
这篇关于R将逗号分隔的单元格分隔成行和笛卡尔积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文