0

I have a dataframe ("pollen) that I want to split into several smaller dataframes according to the values in the "Plant" column.

ID Bee Plant
1 Apis mellifera Salix caprea
2 Osmia cornuta Prunus cerasifera
3 Apis mellifera Salix caprea
4 Apis mellifera Rosa canina

I'm trying to do this:

splt <- split(pollen, pollen$Plant)
pflanzen <- allNames(splt)
for (species in pflanzen) {
      species <- pollen[pollen$Plant==species, ]}

I was hoping to get a bunch of different dataframes named "Salix caprea" and "Prunus cerasifera", etc, but I end up with just one, named "species", that contains the alphabetically last Plant. I thought the "species" in the function would get replaced by the actual species name, but apparently it isnt. What am I doing wrong? Or do you have a smarter way of doing it? Its a bunch of different plant and bee species that I would like to seperate out and I would rather not do it manually.

Thank you!

  • You are assigning to `species` literally in each iteration. You may need `assign(species, pollen[pollen$Plant==species, ])}` – akrun Nov 22 '22 at 18:07
  • 1
    Or use `split`: `split_data <- split(pollen, pollen$Plant)` to have a [very nice list of data frames](https://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames), or if you really want the separate objects `list2env(split_data, envir = .GlobalEnv)` – Gregor Thomas Nov 22 '22 at 18:12

0 Answers0