0

Essentially I am trying to take a data frame with strings and numbers and split it into separate new data frames based on the value in a specific column. I found functions that do this with matrices, but have not been successful in getting this to work.

I would like to take a data frame looking something like this example below (but with many more rows and columns) and sort them all based on their value under "TaxonID", which is a number 1-6.

Species TaxonId sulfur nitrogen ammonia nitrite
Species1    1   718 75  90  63
Species2    1   718 75  90  63
Species3    6   46  22  2   1
Species4    6   67  26  0   0
Species5    5   9   5   0   0

To create:

df1:

Species TaxonId sulfur nitrogen ammonia nitrite
Species1    1   718 75  90  63
Species2    1   718 75  90  63

df5:

Species TaxonId sulfur nitrogen ammonia nitrite
Species5    5   9   5   0   0

df6:

Species TaxonId sulfur nitrogen ammonia nitrite
Species3    6   46  22  2   1
Species4    6   67  26  0   0

And advice would be much appreciated, thank you for taking time to help a novice :)

1 Answers1

0

You can use split to do this.

Say your dataframe is called dat, you can do

split_dat <- split(dat, dat$TaxonID)

You can then retrieve each dataframe from the list split_dat

df1 <- split_dat["1"]
df6 <- split_dat["6"]

Haven't tested this - you may have to convert TaxonID to a character first with as.character

Conor Neilson
  • 1,026
  • 1
  • 11
  • 27