创建主数据框

时间:2016-02-18 18:42:50

标签: r

我在从文件列表创建主数据框时遇到问题,当我加载它时,它给我的是最后一个id号而不是其余的,帮助?

    options(max.print=10000000)
    pollutant<-function(id)
     {
     for (i in 1:id){
     filenames = sprintf("%03d.csv", i)
      masterframe<-read.csv(filenames)
     }
     realdata<-na.omit(masterframe)
      realdata
      }

我尝试将数据文件的主数据帧设置为id并使用此代码

              options(max.print=10000000)
               pollutant<-function(id,pollutant){
               for (i in 1:id) {
                filenames=list.files(pattern="csv")
               df.list = lapply(filenames, function(i) na.omit(read.csv(i)))
               master.df = do.call(rbind, df.list)}
               master.df}

但是我一直在为所有332个文件获取一个主数据框,它在运行时不会将自身限制为id 任何想法?

1 个答案:

答案 0 :(得分:0)

正如@rawr发布的链接所指出的,每次循环都会覆盖masterframe,因此realdata只是导入的最后一个文件。看起来你想要更像这样的东西:

realdata = list()

for (i in 1:id) {
  filenames = sprintf("%03d.csv", i)
  masterframe <- read.csv(filenames)
  realdata[[filenames]] = na.omit(masterframe)
}

但是,lapply可能是一种更清洁的方法:

# Get vector of filenames to read
filenames=list.files(pattern="csv")

# Read all the files into a list of data frames
df.list = lapply(filenames, function(x) na.omit(read.csv(x)))

# Combine into a single data frame
master.df = do.call(rbind, df.list)