我有一个由 168 个文件名组成的数据框 DF1:
DF1$FileName <- c("File1.csv", "File2.csv",..... "File168.csv")
使用:
filez <- NULL
for (i in 1:168){
filez[i] <- paste0("file", i, ".csv", sep="")
}
filesz <- as.data.frame(filez)
我有另一个数据框DF2,如下所示:
DF2$RowNumber <- as.data.frame(rep(c(1:512000), times = 168, length.out = NA, each = 1))
这意味着 DF2 有一个“RowNumber”列,其中数字 1 到 512000 重复了 168 次(即总共 86016000 行)。
我想做的是:
选择文件名(一次一个)-> DF1$FileName[i]
然后在 DF2$FileName 中重复粘贴 1 到 512000
重复上述操作,直到填满所有 86016000 行
最终结果应如下所示:
DF2
RowNumber FileName
1 File1.txt
2 File1.txt
3 File1.txt
. .
. .
. .
. .
512000 File1.txt
1 File2.txt
2 File2.txt
3 File2.txt
. .
. .
512000 File2.txt
1 File3.txt
2 File3.txt
3 File3.txt
. .
. .
512000 File3.txt
. .
. .
512000 File167.txt
1 File168.txt
2 File168.txt
3 File168.txt
. .
. .
512000 File168.txt
我尝试了这个,但我知道存在逻辑错误导致系统挂起:
for (i in 1:nrow(m)){
while(m$RowNumber[i] != 512000) {m$FileName[i] <- filez[[i]]}
}
有人可以建议我更好、更简单的方法来解决我的问题吗?
我确信 R 会有一些包来执行此类操作,但我不知道是哪一个。
最佳答案
在这种情况下不需要 for
循环。您可以为此使用专门设计的函数,例如:
1) 来自基础 R 的 expand.grid
:
filenames <- paste0("file", 1:168, ".csv")
rownumbers <- 1:512000
d <- expand.grid(rownumbers = rownumbers, filenames = filenames)
给出:
> head(d)
rownumbers filenames
1 1 file1.csv
2 2 file1.csv
3 3 file1.csv
4 4 file1.csv
5 5 file1.csv
6 6 file1.csv
2) data.table 包中的 CJ
(交叉连接)函数:
library(data.table)
d <- CJ(rownumbers = rownumbers, filenames = filenames)
这会给你相同的结果。
3) 来自 tidyr 包的 crossing
函数:
library(tidyr)
d <- crossing(rownumbers = rownumbers, filenames = filenames)
这也会给你相同的结果。
关于R 中数据帧中固定行数的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667069/