[R] computing a subset using a loop

jim holtman jholtman at gmail.com
Mon Jul 16 01:30:23 CEST 2012


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 <burton69 at hotmail.de> 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.
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list