1

I've got a list of dataframes and am trying to change the first colname using the lapply method

frames<-lapply(frames,function(x){ colnames(frames[[x]])[1]<-"date"})

is returning the error

Error in `*tmp*`[[x]] : invalid subscript type 'list'

I am not sure why it would produce this error as my understanding is that this should apply

colname[1]<-"date"

to every data frame in the list

If anyone can tell me the root of this error I would be very grateful!

user124123
  • 1,642
  • 7
  • 30
  • 50

1 Answers1

5

You do not need to reference the frames list inside of lapply. Your function treats x as an element in the list, frames. Try this:

frames <- lapply(frames, function(x) { colnames(x)[1] <- "date"; return(x) })

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
ialm
  • 8,510
  • 4
  • 36
  • 48
  • @RichardScriven I like to be explicit :) – ialm Oct 21 '14 at 16:56
  • Ok sorry, I rolled it back. `return` slows things down so I don't really use it unless necessary – Rich Scriven Oct 21 '14 at 16:57
  • @RichardScriven Oh, I didn't know that `return` slows down code performance. I may have to refrain from using `return` in my code... do you know why it slows things down? (Sorry - off topic, but I'm interested) – ialm Oct 21 '14 at 16:59
  • Here's a good read about that http://stackoverflow.com/questions/11738823/explicitly-calling-return-in-a-function-or-not. It's really just a matter of personal preference – Rich Scriven Oct 21 '14 at 17:04