2016-11-03 2 views
0

폴더에 많은 파일이 있는데 그 중 많은 파일이 비어 있고 다른 파일은 데이터가 들어 있습니다.빈 파일 목록과 비어있는 파일 목록 만들기

#Load all data in a list 
file_all <- list.files(file.path(getwd(), "testall"), pattern = "\\.txt$") 

, 내가 @nrussell How to skip empty files when importing text files in R?

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) 
    } 
}) 

@nusel과 내 사이의 차이점은 내가 원하는 것입니다. 비어있는 파일 목록과 비어있는 파일 목록을 다시 나열하십시오. 얼마나 많은 파일이 비어 있고 얼마나 많은 파일이 비어 있는지 알고 싶습니다.

+0

파일을 '적용'기능을 사용하여 두 가지 다른 벡터에 저장하지 않는 이유는 무엇입니까? –

+0

@EliSadoff 그건 실수 였어, 미안. 나는 두 개의 다른 벡터로 저장하고있다. – Enrique

+0

다음을 수행하십시오. list.of.files <- file.info (dir()) 크기 <- file.info (dir()) $ 크기 list.of.non.empty.files <- rownames (list. of.files) [which (sizes! = 0)] 그리고 비어 있지 않은 파일 목록에서 반드시 읽어야합니다. list.of.empty.files <- rownames (list.of.files) [which (sizes == 0)] –

답변

2
# 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)] 
+0

감사합니다! 그건 완벽하게 작동합니다. – Enrique