0

I want my code to accept the dataframes in a list one by one, so one by one I want to make the changes to them (add a variable sect), then save the dataframe, and move on to the next dataframe in the list. I tried to do this in the manner below, but I don't think it works!

setwd("C:\\Users\\datasets")

files = list.files(pattern="*.dta") 
dflist <- list()

for (i in 1:length(files)){
  dflist[[i]] <- read.dta13(files[i], nonint.factors = TRUE)

  names(dflist) <- gsub("\\.dta$", "", list)

  #This creates "sect" variable if it doesn't exist already.
  if(!("sect" %in% colnames(dflist[[i]]))){  
    dflist[[i]]$sect <- "total"}
  #Saving dataset
  save.dta13(dflist[[i]], paste0(i, ".dta"))  
}

Edit: It gives this error:

    Error in as.character(x) : 
  cannot coerce type 'builtin' to vector of type 'character'`
DuEllier
  • 15
  • 4
  • What are your results? – jackiezhu Jul 12 '16 at 15:18
  • @jackiezhu It gives an error, I included it in my post now! – DuEllier Jul 12 '16 at 16:25
  • 1
    You never really have a list. When you say `dflist <- read.dta13(...)`, you make `dflist` a single data frame, not a list containing a data frame. Before your loop you should initialize `dflist = list()`, then in your loop you should assign to individual elements of `dflist`, e.g., `dflist[[i]] <- read.dat13(...)`. (Unless I'm mistaken and a single call to `read.dta13` returns a list of data frames.) – Gregor Thomas Jul 12 '16 at 16:29
  • @Gregor do I not initialize `dflist = list()` by `list = list.files(pattern="*.dta")` or is this different? – DuEllier Jul 12 '16 at 16:37
  • 1
    You don't have the code `dflist = list()`, so you didn't initialize it. All `list = list.files(...)` does is create a variable named `list`. (And if you look at it, `str(list)` or `class(list)`, you'll see it's not even a `list` class variable. Making it a little extra confusing that you name it `list`. ) – Gregor Thomas Jul 12 '16 at 16:55
  • @Gregor Okay... I was told that setting your workdirectory to the map with your files, and then using list.files works fine to make a list. Anyway, if I wanted to use list() to make a list of around 100 dataframes, how would I do that? – DuEllier Jul 12 '16 at 20:35
  • You would do it exactly as I said in my first comment: First initialize the variable that you want to be a list as a list: `dflist = list()`. Then assign to individual elements of it, e.g., `dflist[[i]] <- ...` For more details, see [How do I make a list of data frames](http://stackoverflow.com/a/24376207/903061). – Gregor Thomas Jul 12 '16 at 20:42
  • @Gregor Okay thank you, I followed the instructions (I edited my original post) in the post you linked me to. However, now I get the error `Error in as.character(x) : cannot coerce type 'builtin' to vector of type 'character'` – DuEllier Jul 12 '16 at 21:04
  • You probably need to edit this line `names(dflist) <- gsub("\\.dta$", "", list)`. You no longer have a variable named `list` (which is good!) and since you're inside the loop you need to set a single name, not all the names at once. Try `names(dflist)[i] <- gsub("\\.dta$", "", files[i])`. You could also try moving it after the loop and setting all the names at once: `names(dflist) <- gsub("\\.dta$", "", files)`. – Gregor Thomas Jul 12 '16 at 21:07

0 Answers0