-3
这是后here:的后续。循环目录中的所有文件
以下是由@Arun提供的代码: 此代码运行在不同目录的文件上以比较不同的数据帧。
f1 <- list.files("<path1>", full.names = TRUE)
f2 <- list.files("<path2>", full.names = TRUE)
f3 <- list.files("<path3>", full.names = TRUE)
get_idx <- function(in_df) {
sapply(1:nrow(in_df), function(idx)
which(df1[idx, 2:4] > df1[idx, 1]))
}
get_result <- function(idx.v, in_df2, in_df3) {
sapply(1:length(idx.v), function(ix) {
col.idx <- idx.v[[ix]]
len.idx <- length(col.idx)
if (len.idx > 0) {
res <- sum(in_df2[ix, col.idx] - in_df3[ix, col.idx])
} else {
res <- NA
}
})
}
然后最后部分是:
out <- lapply(1:length(f1), function(f.idx) {
df1 <- read.table(f1[f.idx], header = T)
df2 <- read.table(f2[f.idx], header = T)
df3 <- read.table(f3[f.idx], header = T)
idx.v <- get_idx(df1)
result <- get_result(idx.v, df2, df3)
transform(df3, result = result)
})
运行此代码产生的错误消息:
Error in which(df1[idx, 2:4] > df1[idx, 1]) : object 'df1' not found
你能指定你的问题吗? –
这甚至不是一个问题。 –
问题是我无法正确加载文件,并且出现错误消息 (df1 [idx,2:4]> df1 [idx,1]):找不到对象'df1'的错误 – Gongon