Home > Software engineering >  Converting column names to upper case in a list of data frames using lapply explanation
Converting column names to upper case in a list of data frames using lapply explanation

Time:11-30

This is probably a very simple problem but I have been struggling to search for this issue. Basically, I am using lapply to convert the column names to upper in a list of dataframes. My first attempt did not work, however adding ;x works. What exactly is going on?

This does not work:

 df.list <- lapply(df.list,function(x) colnames(x) <- toupper(colnames(x)))

This does:

df.list <- lapply(df.list,function(x) {colnames(x) <- toupper(colnames(x));x})

CodePudding user response:

Since you are modifying the object x (or in this case only the colnames of x) inside the function definition, you have to return the modified object x. This is happening by using ;x which can be read as a new line only returning the object x

  • Related