'How can I automate this division of my data set in R?

How do I get a loop which performs these actions in R?

Map 33 doesn't exist.

map1 <- dat %>%
  filter(map == 1)
map2 <- dat %>%
  filter(map == 2)
map3 <- dat %>%
  filter(map ==3)
...


Solution 1:[1]

We don't need to use multiple steps, just use split to return a list of data.frames

lst1 <- split(dat, dat$map)
names(lst1) <- paste0("map", names(lst1))

It is better not to create multiple objects. But if we need, use list2env

list2env(lst1, .GlobalEnv)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1