0

I have a data frame with different variables and I want to build different subsets out of this data frame using some conditions and I want to use a loop because there will be a lot of subsets and this would be saving a lot of time.

This are the conditions:

Variable A has an ID for an area, variable B has different species (1,2,3, etc.) and I want to compute different subsets with these columns. The name of every subset should be the the ID of a point and the content should be all individuals of a certain specie in this point.

For a better understanding:

This would be the code for the one subset and I want to use a loop

A_2_NGF_Abies_alba <- subset(A_2_NGF, subset = Baumart %in% c("Abies alba"))

Is this possible doing in R

Thanks

johannes
  • 14,043
  • 5
  • 40
  • 51
burton030
  • 405
  • 4
  • 8
  • 23
  • Can you provide a small [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) example? – johannes Jul 12 '12 at 12:36
  • 2
    If they are disjoint subsets you are better off using `split`. – James Jul 12 '12 at 12:36
  • You need to word your question differently so we know what is holding you up. Do you want to know how to build the string `A_2_NGF_Abies_alba` from the name of your dataframe plus some variable names? Do you want to know how to loop over dataframe ID names? Etc. – Carl Witthoft Jul 12 '12 at 12:52
  • Ok i try to give you an overview about my data frame. I cant post an image because I dont have enough reputation... I have a data frame named "Baumdaten" and it has one column named "pointID" (A_2_SEF,A_2_LEF,A_3_LEF etc.) there is another column named "Baumart" with different species (Abies alba, Betula pendula, etc) with different species, like abies alba, Betula pendula, etc.) and I want to build now subsets and the first subset shoul be named A_2_SEF_Abies_alba and should contain all Abies alba that are living in A_2_SEF. So the normal code would be: to be continue – burton030 Jul 12 '12 at 13:21
  • A_2_SEF_Abies_alba<-subset(Baumdaten,Baumart=="Abies alba"&pointID=="A_2_SEF") and then I have to replace Abies alba with Betula pedula and so on after doing this for A_2_SEF I have to start with A_2_LEF so a lot of time is needing thats why I want to ask if it is possible doing that by using a loop? Hope you can understand my problem... – burton030 Jul 12 '12 at 13:22

1 Answers1

1

Does this help you?

Baumdaten <- data.frame(pointID=sample(c("A_2_SEF","A_2_LEF","A_3_LEF"), 10, T), Baumart=sample(c("Abies alba", "Betula pendula", "Fagus sylvatica"), 10, T))

split(Baumdaten, Baumdaten[, 1:2])
johannes
  • 14,043
  • 5
  • 40
  • 51