这已在各个地方询问过 SQL,但在 R 中找不到简单的方法
我有一个这样的数据集
Ids v1 v2 v3 v4 v5
548|14721,678|17604,716|18316,732|18505,745|18626,752|18716 9186 639 9045 316 28396
548|14721,678|17603,716|18316,732|18507,745|18626,752|18716 9041 598 8897 283 28054
548|14722,678|17603,716|18316,732|18507,745|18626,752|18716 8799 588 8669 246 27433
548|14721,678|17603,716|18316,732|18505,745|18626,752|18715 8914 614 8765 273 27347
716|18316,745|18626 4113 497 4050 270 27267
548|14722,678|17604,716|18316,732|18507,745|18626,752|18716 8829 589 8713 254 25270
我想根据逗号上的第一列进行拆分,并为每个条目创建一行,在其余行中重复这些条目。
我写的功能是
split.data <- function(data, split = ",") {
y <- NULL
for( i in 1:nrow(data)){
y <- rbind(y,cbind(data.frame(unlist(strsplit(as.character(data[i,1]), split = split))),data[i,-1]))
}
names(y) <- names(data)
y <- sapply(y,as.character)
return(as.data.frame(y))
}
这有效,但速度极慢。有没有办法对其进行矢量化并使其更快。我拥有的文件有超过 5000 行这样的行,需要一段时间才能运行。
我为 SQL 找到的相关文章:
Split values over multiple rows
Turning a Comma Separated string into individual rows
最佳答案
更新答案(2013 年 10 月 21 日)
如果您使用 concat.split.multiple
,则可以组合这些步骤。来自我的“splitstackshape”包。该函数使用 count.fields
自动,所以它不应该受到评论中提到的问题的影响:
library(splitstackshape)
out <- concat.split.multiple(mydf, "Ids", seps=",", "long")
head(out)
# v1 v2 v3 v4 v5 time Ids
# 1 9186 639 9045 316 28396 1 548|14721
# 2 9041 598 8897 283 28054 1 548|14721
# 3 8799 588 8669 246 27433 1 548|14722
# 4 8914 614 8765 273 27347 1 548|14721
# 5 4113 497 4050 270 27267 1 716|18316
# 6 8829 589 8713 254 25270 1 548|14722
tail(out)
# v1 v2 v3 v4 v5 time Ids
# 31 9186 639 9045 316 28396 6 752|18716
# 32 9041 598 8897 283 28054 6 752|18716
# 33 8799 588 8669 246 27433 6 752|18716
# 34 8914 614 8765 273 27347 6 752|18715
# 35 4113 497 4050 270 27267 6 <NA>
# 36 8829 589 8713 254 25270 6 752|18716
原始答案(2013 年 2 月 27 日)
您需要先拆分“Ids”字符串,然后再“ reshape ”您的数据(如果我正确理解您的目标)。
我没有像你那样 split ,而是继续利用
read.csv
和论点 fill = TRUE
.请注意,如果“Ids”列中的值是当前因子,则需要使用 text = as.character(mydf$Ids)
反而。以下是我将如何进行:
mydf2 <- cbind(read.csv(text = mydf$Ids, fill = TRUE, header = FALSE), mydf[-1])
mydf2
# V1 V2 V3 V4 V5 V6 v1 v2 v3 v4 v5
# 1 548|14721 678|17604 716|18316 732|18505 745|18626 752|18716 9186 639 9045 316 28396
# 2 548|14721 678|17603 716|18316 732|18507 745|18626 752|18716 9041 598 8897 283 28054
# 3 548|14722 678|17603 716|18316 732|18507 745|18626 752|18716 8799 588 8669 246 27433
# 4 548|14721 678|17603 716|18316 732|18505 745|18626 752|18715 8914 614 8765 273 27347
# 5 716|18316 745|18626 4113 497 4050 270 27267
# 6 548|14722 678|17604 716|18316 732|18507 745|18626 752|18716 8829 589 8713 254 25270
该数据目前采用“宽”格式。让我们把它变成一个“长”的格式。
reshape
函数需要一些关于如何进行的信息。特别是,它需要知道:idvar
的实际列。 s。 read.csv
时创建的新变量。 ,通过手动检查它们的索引,我们可以看到它们位于位置 1 到 6。显然,您需要从实际数据集中指定列号。 对于您提供的示例数据集,我们将使用
reshape
如下:mydf3 <- reshape(mydf2, direction = "long", idvar=7:ncol(mydf2),
varying=1:6, sep = "")
rownames(mydf3) <- NULL
这是
head
和 tail
结果data.frame
:> head(mydf3)
v1 v2 v3 v4 v5 time V
1 9186 639 9045 316 28396 1 548|14721
2 9041 598 8897 283 28054 1 548|14721
3 8799 588 8669 246 27433 1 548|14722
4 8914 614 8765 273 27347 1 548|14721
5 4113 497 4050 270 27267 1 716|18316
6 8829 589 8713 254 25270 1 548|14722
> tail(mydf3)
v1 v2 v3 v4 v5 time V
31 9186 639 9045 316 28396 6 752|18716
32 9041 598 8897 283 28054 6 752|18716
33 8799 588 8669 246 27433 6 752|18716
34 8914 614 8765 273 27347 6 752|18715
35 4113 497 4050 270 27267 6
36 8829 589 8713 254 25270 6 752|18716
这一切都假设我们从一个名为“mydf”的对象开始,如下所示:
mydf <- structure(list(Ids = c("548|14721,678|17604,716|18316,732|18505,745|18626,752|18716",
"548|14721,678|17603,716|18316,732|18507,745|18626,752|18716",
"548|14722,678|17603,716|18316,732|18507,745|18626,752|18716",
"548|14721,678|17603,716|18316,732|18505,745|18626,752|18715",
"716|18316,745|18626", "548|14722,678|17604,716|18316,732|18507,745|18626,752|18716"
), v1 = c(9186L, 9041L, 8799L, 8914L, 4113L, 8829L), v2 = c(639L,
598L, 588L, 614L, 497L, 589L), v3 = c(9045L, 8897L, 8669L, 8765L,
4050L, 8713L), v4 = c(316L, 283L, 246L, 273L, 270L, 254L), v5 = c(28396L,
28054L, 27433L, 27347L, 27267L, 25270L)), .Names = c("Ids", "v1",
"v2", "v3", "v4", "v5"), class = "data.frame", row.names = c(NA,
-6L))
关于string - 在R中将字符串拆分为多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15097162/