1

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.

Now how do I recursively run a function in each data frame? For example, how would I use tolower(colnames(x)) to change column names within data frames to lowercase?

Community
  • 1
  • 1
dmvianna
  • 15,088
  • 18
  • 77
  • 106

1 Answers1

2

Here is a sample data.frame and a list with that data.frame repeated twice.

test <- read.table(header=TRUE, text="ID     Category    Value
 2323   Friend      23.40
 3434   Foe         -4.00")
temp <- list(A = test, B = test)

If you just wanted to change the names of the original data.frame, try:

names(test) <- tolower(names(test))
test
#     id category value
# 1 2323   Friend  23.4
# 2 3434      Foe  -4.0

If you wanted to change the names of all the data.frames in the list, try:

lapply(temp, function(x) { names(x) = tolower(names(x)); x })
# $A
#     id category value
# 1 2323   Friend  23.4
# 2 3434      Foe  -4.0
# 
# $B
#     id category value
# 1 2323   Friend  23.4
# 2 3434      Foe  -4.0
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485