我正在尝试编写一个代码,其中(1)从目录中读取指定范围的文件,(2)计算每个文件中完整行的数量,(3)将我的答案作为2列数据框返回(具有指定的列名称。)
complete <- function(directory, ID){
filelist <- list.files(directory, full.names = TRUE)[ID]
x <- lapply(filelist, function(x){read.csv(x, header = TRUE)})
y <- complete.cases(data.frame(x))
z <- sum(y*1)
print(z)
}
它适用于1个文件但不适用于范围,如果我使用完整(&#34;目录&#34;,1:2)我得到:
错误(函数(...,row.names = NULL,check.rows = FALSE,check.names = TRUE,: 参数意味着不同的行数:1461,3652
一旦我可以获得完整值的数量,我想我可以找出如何返回数据框。
提前致谢,
玫瑰
答案 0 :(得分:0)
这可能有助于获得
后的内容complete <- function(directory){
# Get list of csv files in directory
filelist <- list.files(directory, pattern = ".csv", full.names = TRUE)
# Get a list of dataframes for all csv files in the directory
list.df <- lapply(filelist, function(x){read.csv(x, header = TRUE)})
# Get a list of complete rows for each dataframe in the list
complete.casesums <- lapply(list.df, function(x) sum(complete.cases(x)))
# Get a list of incomplete rows for each dataframe in the list
incomplete.casesums <- lapply(list.df, function(x) sum(!complete.cases(x)))
# Create a dataframe with the filename, number of complete rows, and number of incomplete rows
df <- data.frame(cbind(filelist, complete.casesums, incomplete.casesums), stringsAsFactors = FALSE)
# Return the dataframe
return(df)
}
# Call the function
(df <- complete("C:/Path/To/Folder"))