In R, I have several data frames with the same structure (3 columns and several rows). All data frames follow the same naming conventions and have the same data structure, so they can be listed with an ls(pattern="NamePrefix")
call.
I wanted to use a for loop to rename the second and third variable of all those data frames. Here is the code that I tried:
for (i in ls(pattern="NamePrefix"))
{
names(i)[[2]] <- "NewName"
names(i)[[3]] <- "OtherNewName"
}
I have also tried setting the names of the data frames as list first (by File_List <- ls(pattern="NamePrefix")
) and then using that list in the loop (for (i in File_List)
), but that doesn't work either.
Whatever I do, I get this error:
Error in names(i)[[2]] <- "NewName" :
'names' attribute [2] must be the same length as the vector [1]
However, if I simply do names(SomeDataset)[[2]] <- "NewName"
that works perfectly. Yet somehow the for loop appears incapable of running it.
Any ideas as to why does it happen and how to fix it? Note that I am well aware of several other alternatives to do the same loop renaming over several data frames without using names()
, but this was a far simpler, more intuitive alternative (I thought) and I can't see why the loop seems incapable of implementing it. So, now I want to understand why the solution that I thought was the simplest appears to be wrong (and if possible, how to fix it so that it works).
Thanks in advance!