Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lapply over a list and adding a column with data frame name

Tags:

r

I have a list containing two data frames:

sample_list <- list("tables" = data.frame(weight = sample(1:50, 20, replace = T)),
                    "chairs" = data.frame(height = sample(1:50, 20, replace = T)))

I would like to use lapply to run a function over all the data frames in this list. In the output of each function, I need to create another column with the name of the source data frame (see mutate):

lapply(sample_list, function(x) {
  x %>% 
    filter(x >= 20) %>% 
    mutate(groupName = names(x))
})

For some reason, I can't figure out how to make this work. How do I pass the name of the data frame into mutate? Right now it is returning the name of the first column in that data frame, rather than the name of the data frame itself.

Thanks!

like image 279
DJC Avatar asked Oct 09 '19 18:10

DJC


People also ask

Can you use Lapply on a DataFrame?

In R Programming Language to apply a function to every integer type value in a data frame, we can use lapply function from dplyr package. And if the datatype of values is string then we can use paste() with lapply.

Can you use Lapply on a DataFrame in R?

The output of lapply() is a list. lapply() can be used for other objects like data frames and lists. lapply() function does not need MARGIN. A very easy example can be to change the string value of a matrix to lower case with tolower function.

How do you add a list to a DataFrame as a column in R?

Now to add a list as a column, create a list with required values. Then, use the name of the data frame and the new column separated by $ and assign this to the list so created. This will assign the list to the column name given and then add it to the dataframe.

Does Lapply return a list?

lapply returns a list as it's output. In the output list there is one component for each component of the input list and it's value is the result of applying the function to the input component.


1 Answers

We can loop through names of sample_list instead of looping through the list

lapply(names(sample_list), function(x) {
    sample_list[[x]] %>% 
        filter_at(vars(1),~. >= 20) %>% 
        mutate(groupName = x)
})

Update Sep-2021

cleaner way using purrr::map

purrr::map(names(sample_list), ~sample_list[[.x]] %>% 
             filter_at(vars(1),~. >= 20) %>% 
             mutate(groupName = .x)
)
like image 69
A. Suliman Avatar answered Nov 15 '22 01:11

A. Suliman