2017-08-04 68 views
0

目前的窘境:我有我試圖分解爲基於列的部分字符串匹配較小的文件,一個巨大的數據幀。我已經做了一個偉大的腳本:創建許多排序的數據幀

df <- read.csv("file.csv", header = TRUE, sep = ",") 
newdf <- select(df, matches('threshold1',)) 
write.csv(newdf,"threshold1.file.csv", row.names = FALSE) 

問題是,我有幾百個門檻分解成單獨的文件。必須有一種方法,我可以循環這個腳本來創建所有的文件我而不是手動編輯腳本說閾值2,閾值3等

+0

'for(i in 1:N){m = paste0('threshold',i); newdf = select(df,matches(m,)); write.csv(newdf,paste0(米, 「.file.csv」),row.names = FALSE)}' – dww

+0

其中N =究竟? –

+0

閾值的數目 - 你必須自己拿到這個號碼,你沒有提供一個完整的例子 – dww

回答

0

你可以嘗試用lapply來解決它。

# Functions that splits and saves the data.frame 
split_df <- function(threshold, df){ 
    newdf <- select(df, matches(threshold,)) 
    write.csv(newdf, 
      paste(".file.csv", sep = ""), row.names = FALSE) 
    return(threshold) 
} 

df <- read.csv("file.csv", header = TRUE, sep = ",") 

# Number for thresholds 
N <- 100 
threshold_l <- paste("threshold", 1:N, sep = "") 

lapply(threshold_l, split_df, df = df)