Need help editing data.frames within a list

I have a list of about 10 dataframes (a bunch of tabular data). I simply want to remove some rows and columns from all of the dataframes using my list. The name of my list is list_schooldata. How can I remove the first 50 rows from all the dataframes within list_schooldata

Thank you!

Here's one option: If we have a single data frame, we can do, for example, iris %>% slice(-(1:50)) or iris[-(1:50), ], to remove the first 50 rows. For data frames in a list, we can use map() to apply the same approach to every data frame in the list.

library(tidyverse)

list.school.data = map(list.school.data, ~ .x %>% slice(-(1:50))

or

list.school.data = map(list.school.data, ~ .x[-(1:50), ])

In base R, a similar approach would use lapply:

list.school.data = lapply(list.school.data, function(x) x[-(1:50), ])
1 Like

Thank you! That seemed to work. What if I wanted to now remove columns from the list, but specific columns (for example, columns 1, 5, 6, and 8)? Would I also use map()?

Yes, anything you would do to a single data frame you can do to each data frame in a list by using map. Example:

list(iris, mtcars) %>% 
  map(~ .x %>% slice(-(1:5)) %>% select(-c(1,3,5)))

It's probably safer to remove columns based on their name or name pattern, so you don't have to know their current column index position. For example:

list(iris, mtcars) %>% 
  map(~.x %>% slice(-(1:5)) %>% select(-matches("mpg|cyl|Petal")))
1 Like

Thank you for your help! I now understand the map() function and how it applies specifically to df's within a list.

slice is used to keep or remove rows. mtcars %>% slice(c(2,5,10)) returns rows 2, 5, and 10. mtcars %>% slice(-c(2,5,10)) removes rows 2, 5, and 10.

select is used to keep or remove columns.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.