1

I am applying the same function to multiple dataframes. For example, I want to merge the column2 and column3 in df1. After applying this function, the df1 will get a new column called col2_col3.

 df1 <- data.frame(x = rep(3, 5), y = seq(1, 5, 1), ID = letters[1:5])
 df2 <- data.frame(x = rep(5, 5), y = seq(2, 6, 1), ID = letters[6:10])
#I define a function:
PasteTwoColumn <- function(x) 
{
  x$col2_col3 <- paste(x[,2], x[,3], sep = "_")
  return(x)
}
#apply the function to the df1, it works.
df1 <- PasteTwoColumn(df1)
# but I failed by an lappy function, because it returns a list, not the dataframe
mylist <- list(df1, df2) 
result <- lapply(mylist, PasteTwoColumn)

I want to continue to apply this function to all my dataframes, eg. df1, df2, df3 ...df100. The output file should keep the same type of dataframe and the name.

The lapply function does not work, because it returns a list, not the separate data frame.

1 Answer 1

2

We can keep the datasets in a list and loop over the list with lapply

lst1 <- lapply(list(df1, df2), PasteTwoColumn)

If there are many datasets, use mget to get the values of the datasets into a list

lst1 <- lapply(mget(paste0('df', 1:100)), PasteTwoColumn)

Or instead of paste, we can also use ls

lst1 <- lapply(mget(ls(pattern = '^df\\d+$')), PasteTwoColumn)

If we need to update the original object, use list2env

list2env(lst1, .GlobalEnv) #not recommended though

If we need to use a for loop

for(obj in paste0("df", 1:100)) {
      assign(obj,  PasteTwoColumn(get(obj)))
  }
Sign up to request clarification or add additional context in comments.

1 Comment

The last one [for loop] is exactly what I want. thanks for showing me in many ways. I learn a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.