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")
我會做在'lapply'步驟,這是您最後一次訪問文件名/路徑。可能是這樣的:'lapply(FUN = function(p)read.csv(p)%>%select(1:16)%>%mutate(file_name = p))%>%' – scoa
謝謝scoa!我修改了回答 – EJrandom