So I have a bunch of data frames in a list object. Frames are organised such as
ID Category Value
2323 Friend 23.40
3434 Foe -4.00
And I got them into a list by following this topic. I can also run simple functions on them as shown in this topic.
Now I am trying to run a conditional function with lapply, and I’m running into trouble. In some tables the ‘ID’ column has a different name (say, ‘recnum’), and I need to tell lapply to go through each data frame, check if there is a column named ‘recnum’, and change its name to ‘ID’, as in
colnr <- which(names(x) == "recnum"
if (length(colnr > 0)) {names(x)[colnr] <- "ID"}
But I’m running into trouble with local scope and who knows what. Any ideas?
If you look at the second part of mnel’s answer, you can see that the function
fooevaluatesxas its last expression. Without that, if you try to change the names of the data.frames in your list directly from within the anonymous function passed tolapply, it will likely not work.Just as an alternative, you could use
gsuband avoid loading an additional package (althoughplyris a nice package):