2013-01-03 75 views
-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 
+0

你能指定你的問題嗎? –

+0

這甚至不是一個問題。 –

+0

問題是我無法正確加載文件,並且出現錯誤消息 (df1 [idx,2:4]> df1 [idx,1]):找不到對象'df1'的錯誤 – Gongon

回答

3

它是一種非常簡單的錯誤。將行which(df1[idx, 2:4] > df1[idx, 1]))更改爲which(in_df[idx, 2:4] > in_df[idx, 1]))

相關問題