Import a number of csv files and replace NA by Zeros

Posted by tao.hong on Stack Overflow See other posts from Stack Overflow or by tao.hong
Published on 2012-09-15T20:37:21Z Indexed on 2012/09/15 21:38 UTC
Read the original article Hit count: 197

Filed under:

I know how to do this individually. However, I have more than 1000 files. I decided to use a for loop. However, it seems like I did not find the correct way to evaluate my variables.

Here is my code

setwd('C:/data')
filenames=dir() #find file names

for (i in filenames){
  adt = substr(x = i, start = 1, stop = nchar(i)-4)
  name=paste("data_", adt, sep="")
  assign(name, read.csv(i,header=T,sep=",")) #read each file and assign a variable name starting with data_ to it
  func=paste('name[is.na(name)] <- 0',sep="") # here is the place I have problem. R will not consider name is a parameter whose values change in each iteration
  eval((text=func))

  }

© Stack Overflow or respective owner

Related posts about r