Reputation: 795
I have a list of data frames:
mylist<-list(df1=data.frame(Var1=c("A","b","c"), var2=
c("a","b","c")), df2= data.frame(var1 = c("a","B","c"),
VAr2=c("A","B","C")))
I would like to change all cases within the column headings and each element of a string to lowercase. (This way when I merge data frames all variables merge correctly and 'cat' vs "Cat' are not read as different entries).
The output would look like:
mylist<-list(df1=data.frame(var1=c("a","b","c"), var2=
c("a","b","c")), df2= data.frame(var1 = c("a","b","c"),
var2=c("a","b","c")))
I have tried the following:
cleandf <- lapply(mylist, function(x) tolower(mylist[x])
Upvotes: 2
Views: 92
Reputation: 26343
Here is a similar approach to Masoud's answer
lapply(mylist, function(x) {
names(x) <- tolower(names(x))
x[] <- lapply(x, tolower)
x
})
#$df1
# var1 var2
#1 a a
#2 b b
#3 c c
#$df2
# var1 var2
#1 a a
#2 b b
#3 c c
The first lapply
iterates over your list. For each data frame - represented by the x
- we first change its column names. The second lapply
then applies tolower
to each column of your data frames.
Upvotes: 1