2016-01-28 70 views
1

我試圖從許多csv文件中提取相同的前16列數據,這些csv文件位於不同的子目錄中,並將csv文件名添加到最終的每行CSV。我的代碼:選擇特定的列並將csv名稱添加到最終的csv文件

getwd() 
root<-list.dirs(".", recursive=TRUE) 
# get list of files ending in csv in directory root 
dir(root, pattern='csv$', recursive = TRUE, full.names = TRUE) %>% 
# read files into data frames 
lapply(FUN = read.csv) %>% 
# bind all data frames into a single data frame 
rbind_all %>% 
# write into a single csv file 
write.csv("all.csv") 

我想知道在哪裏放置選擇列和添加文件名的代碼。

答:

getwd() 
root<-list.dirs(".", recursive=TRUE) 
# get list of files ending in csv in directory root 
dir(root, pattern='csv$', recursive = TRUE, full.names = TRUE) %>% 
# read files into data frames, select first 16 columns and add filename 
lapply(FUN = function(p) read.csv(p) %>% select(1:16) %>%  

mutate(file_name=p)) %>%  
# bind all data frames into a single data frame 
rbind_all %>% 
# write into a single csv file 
write.csv("all.csv") 
+1

我會做在'lapply'步驟,這是您最後一次訪問文件名/路徑。可能是這樣的:'lapply(FUN = function(p)read.csv(p)%>%select(1:16)%>%mutate(file_name = p))%>%' – scoa

+0

謝謝scoa!我修改了回答 – EJrandom

回答

2

你應該在您使用lapply的時候做,因爲這是最後一步,您可以訪問文件名/路徑:

dir(root, pattern='csv$', recursive = TRUE, full.names = TRUE) %>% 
    lapply(FUN = function(p) read.csv(p) %>% select(1:16) %>% mutate(file_name=p)) %>% 
    bind_rows() %>% 
    write.csv("all.csv") 
+0

使用'bind_rows'而不是'rbind_all',看到這個http://rpackages.ianhowson.com/cran/dplyr/man/bind.html –

相關問題