R 创建包含空文件的列表和包含非空文件的列表

R 创建包含空文件的列表和包含非空文件的列表,r,dataframe,lapply,R,Dataframe,Lapply,我在一个文件夹里有很多文件,其中很多是空的,还有一些里面有数据 我想做的是: #Load all data in a list file_all <- list.files(file.path(getwd(), "testall"), pattern = "\\.txt$") #加载列表中的所有数据 file_all#在当前工作目录中创建文件列表 list.of.files为什么不使用apply函数将文件存储在两个不同的向量中。@EliSadoff这是个错误,对不起。我存储在两个不同的

我在一个文件夹里有很多文件,其中很多是空的,还有一些里面有数据

我想做的是:

#Load all data in a list
file_all <- list.files(file.path(getwd(), "testall"), pattern = "\\.txt$") 
#加载列表中的所有数据
file_all
#在当前工作目录中创建文件列表

list.of.files为什么不使用
apply
函数将文件存储在两个不同的向量中。@EliSadoff这是个错误,对不起。我存储在两个不同的向量中。执行此操作:list.of.files请显示当前和所需的结果。具体来说,请说明您当前的尝试不起作用的原因?错误?意外输出?用实际数据说明。
library(plyr)
df_list <- lapply(files, function(x) {
    if (!file.size(x) == 0) {
        list.files(x)
    }
})
    df_list2 <- lapply(files, function(x) {
    if (file.size(x) == 0) {
        list.files(x)
    }
})
# create a list of files in the current working directory
list.of.files <- file.info(dir())

# get the size for each file
sizes <- file.info(dir())$size

# subset the files that have non-zero size
list.of.non.empty.files <- rownames(list.of.files)[which(sizes != 0)]

# here you can further subset that list by file name, eg - if I only want all the files with extension .mp3
list.of.non.empty.files[grep( ".mp3", list.of.non.empty.files)]


# get the empty files
list.of.empty.files <- rownames(list.of.files)[which(sizes == 0)]