use a pathname from a row in a dataframe in a for loop

风流意气都作罢 提交于 2020-05-30 07:56:56

问题


In an R script, I'd like to use use a pathname from a row in a dataframe in a for loop. For example, I'd like to get the directory size for each of the paths in column one in the following dataframe, by substituting "." in the code: sum(file.info(list.files(".", all.files = TRUE, recursive = TRUE))$size) with a value from the 'pathname' column.

                         pathname size
1     F:/Asus/C_drive/WEPP/bitmap    0
2       F:/Asus/C_drive/WEPP/Data    0
3       F:/Asus/C_drive/WEPP/misc    0
4     F:/Asus/C_drive/WEPP/output    0
5       F:/Asus/C_drive/WEPP/runs    0
6      F:/Asus/C_drive/WEPP/tools    0
7 F:/Asus/C_drive/WEPP/watersheds    0
8       F:/Asus/C_drive/WEPP/wepp    0
9    F:/Asus/C_drive/WEPP/weppwin    0

回答1:


Here is a solution using Base R, using the directory structure from my R Working directory. We'll build a data frame with directory, file name and file size, then aggregate to the directory level.

theDirectories <- list.dirs(getwd()) # start with current wd
# for example answer, only use first 2 directories in vector
fileList <- lapply(theDirectories[1:2],function(x){
     files <- list.files(path=x,full.names = TRUE)
     fileName <- list.files(path=x,full.names = FALSE)
     size <- unlist(lapply(files,file.size))
     pathName <- rep(x,length(size))
     data.frame(pathName,fileName,size,stringsAsFactors = FALSE)
})
fileSizes <- do.call(rbind,fileList)
aggregate(size ~ pathName,data = fileSizes,sum)

...and the output:

> aggregate(sizes ~ pathName,data = fileSizes,sum)
                                  pathName    size
1      /Users/lgreski/gitrepos/datascience 3461684
2 /Users/lgreski/gitrepos/datascience/.git  137811
> 


来源:https://stackoverflow.com/questions/61311014/use-a-pathname-from-a-row-in-a-dataframe-in-a-for-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!