computing a subset using a loop

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

computing a subset using a loop

burton030
Dear all,

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.
       
I try to give you an overview about my data frame. I have a data frame named "Baumdaten" and it has one column named "transectID" with different IDs (A_SEF ,A_LEF, B_SEF etc.) there is another column named "Baumart" with different species like Abies alba, Betula pendula, etc. I want to build now subsets and the first subset should 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

A_2_SEF_Abies_alba<-subset(Baumdaten,Baumart=="Abies alba"&pointID=="A_2_SEF")

 The following step would be to replace Abies alba with Betula pendula and so on after doing this for A_SEF I have to start with A_LEF so a lot of time is needing thats why I want to ask if it is possible doing this by using a loop? Hope you can understand my problem...

Reply | Threaded
Open this post in threaded view
|

Re: computing a subset using a loop

burton030
Baumdaten_aufbereitet.csv

Here you have an overview about my data frame...
Reply | Threaded
Open this post in threaded view
|

Re: computing a subset using a loop

jholtman
In reply to this post by burton030
I looks like you want to use the 'split' function which would create a
list of dataframes with the various conditions:

result <- split(Baumdaten, list(Baumdaten$transectID,
Baumdaten$Baumart), drop = TRUE)

On Sun, Jul 15, 2012 at 11:31 AM, burton030 <[hidden email]> wrote:

> Dear all,
>
> 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.
>
> I try to give you an overview about my data frame. I have a data frame named
> "Baumdaten" and it has one column named "transectID" with different IDs
> (A_SEF ,A_LEF, B_SEF etc.) there is another column named "Baumart" with
> different species like Abies alba, Betula pendula, etc. I want to build now
> subsets and the first subset should 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
>
> A_2_SEF_Abies_alba<-subset(Baumdaten,Baumart=="Abies
> alba"&pointID=="A_2_SEF")
>
>  The following step would be to replace Abies alba with Betula pendula and
> so on after doing this for A_SEF I have to start with A_LEF so a lot of time
> is needing thats why I want to ask if it is possible doing this by using a
> loop? Hope you can understand my problem...
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/computing-a-subset-using-a-loop-tp4636564.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: computing a subset using a loop

burton030
Hi,

thanks for your reply but this code just gives me a list but no subsets but I need subsets because I want to do some calculations with these subsets and want do make some plots etc. Is there a solution for my problem? I ve posted an example for the first subset...

A_SEF_Abies_alba.csv
Reply | Threaded
Open this post in threaded view
|

Re: computing a subset using a loop

jholtman
Here is an example of using your data to split it into the subsets and
then computing a summary of each subset.  You have to remember that
what is returned from 'split' is a 'list' of 'data.frames' that as the
subsets that you want and then use use 'lapply' to process each of the
subsets in the list.

> df <- read.table("C:\\Documents and Settings\\kon9407\\My Documents\\Downloads\\Baumdaten_aufbereitet (1).csv"
+     , sep = ';'
+     , as.is = TRUE
+     , header = TRUE
+     )
> # split the data into a list of dataframe
> df.s <- split(df, list(df$Plot..ID., df$Baumart), drop = TRUE)
> head(names(df.s), 20)
 [1] "A_2_1.Abies alba" "A_2_2.Abies alba" "A_2_3.Abies alba" "A_2_4.Abies alba"
 [5] "A_2_5.Abies alba" "A_2_6.Abies alba" "A_3_1.Abies alba" "A_3_4.Abies alba"
 [9] "A_3_5.Abies alba" "A_3_6.Abies alba" "A_4_1.Abies alba" "A_4_3.Abies alba"
[13] "A_4_4.Abies alba" "A_4_5.Abies alba" "A_4_6.Abies alba" "B_1_2.Abies alba"
[17] "B_1_4.Abies alba" "B_1_5.Abies alba" "B_1_6.Abies alba" "B_2_4.Abies alba"
> df.s[1]
$`A_2_1.Abies alba`
  X Plot..ID. Alter.Neuer.Wald Hoehe..m. Radius..cm.          Familie
  Baumart    Deutsch
1 1     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
2 2     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
3 3     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
4 4     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
5 5     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
6 6     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
7 7     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
8 8     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
9 9     A_2_1                2       647        5,64 Kieferngewaechse
Abies alba Weisstanne
             Englisch Umfang..cm.    DBH..cm. Gehoelz Bemerkungen
Fotos Waldart pointID
1 European silver fir          38 12,09577569       0
NA     SEF A_2_SEF
2 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
3 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
4 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
5 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
6 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
7 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
8 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
9 European silver fir          NA        <NA>       1
NA     SEF A_2_SEF
  transectID         DBH_inch              age
1      A_SEF 4,76211641338583 35,7158731003937
2      A_SEF             <NA>             <NA>
3      A_SEF             <NA>             <NA>
4      A_SEF             <NA>             <NA>
5      A_SEF             <NA>             <NA>
6      A_SEF             <NA>             <NA>
7      A_SEF             <NA>             <NA>
8      A_SEF             <NA>             <NA>
9      A_SEF             <NA>             <NA>

>
> lapply(df.s, summary)  # notice the names of each of the subsets is printed
$`A_2_1.Abies alba`
       X      Plot..ID.         Alter.Neuer.Wald   Hoehe..m.
Radius..cm.
 Min.   :1   Length:9           Min.   :2        Min.   :647
Length:9
 1st Qu.:3   Class :character   1st Qu.:2        1st Qu.:647   Class
:character
 Median :5   Mode  :character   Median :2        Median :647   Mode
:character
 Mean   :5                      Mean   :2        Mean   :647
 3rd Qu.:7                      3rd Qu.:2        3rd Qu.:647
 Max.   :9                      Max.   :2        Max.   :647

   Familie            Baumart            Deutsch            Englisch
       Umfang..cm.
 Length:9           Length:9           Length:9           Length:9
      Min.   :38
 Class :character   Class :character   Class :character   Class
:character   1st Qu.:38
 Mode  :character   Mode  :character   Mode  :character   Mode
:character   Median :38

      Mean   :38

      3rd Qu.:38

      Max.   :38

      NA's   :8
   DBH..cm.            Gehoelz       Bemerkungen         Fotos
  Waldart
 Length:9           Min.   :0.0000   Length:9           Mode:logical
Length:9
 Class :character   1st Qu.:1.0000   Class :character   NA's:9
Class :character
 Mode  :character   Median :1.0000   Mode  :character
Mode  :character
                    Mean   :0.8889
                    3rd Qu.:1.0000
                    Max.   :1.0000

   pointID           transectID          DBH_inch             age
 Length:9           Length:9           Length:9           Length:9
 Class :character   Class :character   Class :character   Class :character
 Mode  :character   Mode  :character   Mode  :character   Mode  :character





$`A_2_2.Abies alba`
       X       Plot..ID.         Alter.Neuer.Wald   Hoehe..m.
Radius..cm.
 Min.   :12   Length:1           Min.   :2        Min.   :660
Length:1
 1st Qu.:12   Class :character   1st Qu.:2        1st Qu.:660   Class
:character
 Median :12   Mode  :character   Median :2        Median :660   Mode
:character
 Mean   :12                      Mean   :2        Mean   :660
 3rd Qu.:12                      3rd Qu.:2        3rd Qu.:660
 Max.   :12                      Max.   :2        Max.   :660
   Familie            Baumart            Deutsch            Englisch
       Umfang..cm.
 Length:1           Length:1           Length:1           Length:1
      Min.   :33
 Class :character   Class :character   Class :character   Class
:character   1st Qu.:33
 Mode  :character   Mode  :character   Mode  :character   Mode
:character   Median :33

      Mean   :33

      3rd Qu.:33

      Max.   :33
   DBH..cm.            Gehoelz  Bemerkungen         Fotos
Waldart
 Length:1           Min.   :0   Length:1           Mode:logical
Length:1
 Class :character   1st Qu.:0   Class :character   NA's:1
Class :character
 Mode  :character   Median :0   Mode  :character                  Mode
 :character
                    Mean   :0
                    3rd Qu.:0
                    Max.   :0
   pointID           transectID          DBH_inch             age
 Length:1           Length:1           Length:1           Length:1
 Class :character   Class :character   Class :character   Class :character
 Mode  :character   Mode  :character   Mode  :character   Mode  :character




$`A_2_3.Abies alba`
       X          Plot..ID.         Alter.Neuer.Wald   Hoehe..m.
Radius..cm.
 Min.   :21.00   Length:10          Min.   :2        Min.   :662
Length:10
 1st Qu.:23.25   Class :character   1st Qu.:2        1st Qu.:662
Class :character
 Median :25.50   Mode  :character   Median :2        Median :662
Mode  :character
 Mean   :25.50                      Mean   :2        Mean   :662
 3rd Qu.:27.75                      3rd Qu.:2        3rd Qu.:662
 Max.   :30.00                      Max.   :2        Max.   :662
   Familie            Baumart            Deutsch            Englisch
       Umfang..cm.
 Length:10          Length:10          Length:10          Length:10
      Min.   :14.0
 Class :character   Class :character   Class :character   Class
:character   1st Qu.:22.0
 Mode  :character   Mode  :character   Mode  :character   Mode
:character   Median :30.5

      Mean   :31.3

      3rd Qu.:35.0

      Max.   :56.0
   DBH..cm.            Gehoelz  Bemerkungen         Fotos
Waldart
 Length:10          Min.   :0   Length:10          Mode:logical
Length:10
 Class :character   1st Qu.:0   Class :character   NA's:10
Class :character
 Mode  :character   Median :0   Mode  :character                  Mode
 :character
                    Mean   :0
                    3rd Qu.:0
                    Max.   :0
   pointID           transectID          DBH_inch             age
 Length:10          Length:10          Length:10          Length:10
 Class :character   Class :character   Class :character   Class :character
 Mode  :character   Mode  :character   Mode  :character   Mode  :character



On Sun, Jul 15, 2012 at 3:01 PM, burton030 <[hidden email]> wrote:

> Hi,
>
> thanks for your reply but this code just gives me a list but no subsets but
> I need subsets because I want to do some calculations with these subsets and
> want do make some plots etc. Is there a solution for my problem? I ve posted
> an example for the first subset...
>
> http://r.789695.n4.nabble.com/file/n4636591/A_SEF_Abies_alba.csv
> A_SEF_Abies_alba.csv
>
> --
> View this message in context: http://r.789695.n4.nabble.com/computing-a-subset-using-a-loop-tp4636564p4636591.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: computing a subset using a loop

burton030
Hi,

thanks I think now I understand how it works...

I m using now this code for the first specie in all my transect IDs.

df.s <- split(Baumdaten, list(Baumdaten$transectID, Baumdaten$Baumart), drop = TRUE)
head(names(df.s), 10)
"A_SEF_Abies alba" "A_LEF_Abies alba" "B_SEF_Abies alba" "B_LEF_Abies alba"
"C_SEF_Abies alba" "C_LEF_Abies alba" "D_SEF_Abies alba" "D_LEF_Abies alba"
"E_SEF_Abies alba" "E_LEF_Abies alba"


 But can you tell me how, I can make subsets out of one of the subsets. For example I want boxplot for the variable age.

Is it something like this? It dosent work, I know...

lapply(df.s,boxplot(A_SEF_Abies alba$Baumart))

Thanks in advanced
Reply | Threaded
Open this post in threaded view
|

Re: computing a subset using a loop

jholtman
If you just wanted to do a boxplot of one of the subset, you would do
something like:

boxplot(df.s[["A_SEF_Abies alba"]]$age)

If you wanted to do it for all the subset, then something like:

lapply(df.s, function(.sub) boxplot(.sub$age))

On Mon, Jul 16, 2012 at 11:14 AM, burton030 <[hidden email]> wrote:

> Hi,
>
> thanks I think now I understand how it works...
>
> I m using now this code for the first specie in all my transect IDs.
>
> df.s <- split(Baumdaten, list(Baumdaten$transectID, Baumdaten$Baumart), drop
> = TRUE)
> head(names(df.s), 10)
> "A_SEF_Abies alba" "A_LEF_Abies alba" "B_SEF_Abies alba" "B_LEF_Abies alba"
> "C_SEF_Abies alba" "C_LEF_Abies alba" "D_SEF_Abies alba" "D_LEF_Abies alba"
> "E_SEF_Abies alba" "E_LEF_Abies alba"
>
>
>  But can you tell me how, I can make subsets out of one of the subsets. For
> example I want boxplot for the variable age.
>
> Is it something like this? It dosent work, I know...
>
> lapply(df.s,boxplot(A_SEF_Abies alba$Baumart))
>
> Thanks in advanced
>
> --
> View this message in context: http://r.789695.n4.nabble.com/computing-a-subset-using-a-loop-tp4636564p4636653.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.