-2

I have a data frame with id column

Whats the best way to split it into list of data frames based on id column and remove the id column in each one of them

  • Please read about [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and update your question accordingly. Include a sample of your data by pasting the output of `dput()` into your post or `dput(head())` if you have a large data frame. Also include code you have tried, any relevant errors, and expected output. If you cannot post your data, then post code for creating representative data. [Do not post images of code and/or data.](https://meta.stackoverflow.com/a/285557/6382434). – LMc Aug 30 '23 at 21:28

1 Answers1

0

Using base R:

data <- split(data, data$id)
data <- lapply(data, function(df) df[, -which(names(df) == "id")])

Using the tidyverse:

data <- data %>%
  split(data$id) %>%
  map(~ select(.x, -id))
weakCoder
  • 11
  • 3
  • You could avoid the `lapply` by `split`ting on the dataset with the column removed - e.g.: `split(mtcars[-which(names(mtcars) == "cyl")], mtcars$cyl)` – thelatemail Aug 30 '23 at 21:47