[R] Subsetting data in a loop

hadley wickham h.wickham at gmail.com
Wed Oct 29 21:24:36 CET 2008


?split.

Hadley

On Wed, Oct 29, 2008 at 3:22 PM, t c <mudiver1200 at yahoo.com> wrote:
> I need some help with sub-setting my data.  I am trying to divide a data frame into multiple data frames based on the year collected, and stored in a list with each new data frame labeled with "year X" where X is the year the data was collected.  When I run my current code I get nine error messages stating
> "In one_year[name] <- myear :  number of items to replace is not a multiple of replacement length"What I have so far is:
> Below is my code, the data frame mydat, and what I would like the final product to look like.  I would appreciate any help or suggestions on what I am doing wrong.
> Thanks,
> Tim
>
>
> one_year<-list()
> for (i in min(mydat$Year):max(mydat$Year)) #Loops from first to last year in dataset
> {
> myear<-mydat[mydat$Year==i,]
> name<-paste("Year",i)
> one_year[name]<-myear
> }
>
>
> mydat:
> Year Month Day          number other   Ind1      Ind2       Ind3   Ind4
> 1985     8  13             1     1       0         0          0      0
> 1986     8   5             2     3       0         0          0      0
> 1991     4  24             3     0       0         0          0      0
> 1991     4  25             4     0       0         0          0      0
> 1991     8   2             5     0       0         0          0      0
> 1991     8   6             6     0       0         0          0      0
> 1991     9   4             7     2       0         0          0      0
> 1991     9   9             8     4       0         0          0      0
> 1991    12  16             9     0       0         0          0      0
> 1992     3  18            10     1       0         0          0      0
> 1992     5  13            11     0       0         0          0      0
> 1992     7  31            12     1       0         0          0      0
> 1992     8  19            13     0       0         0          0      0
> 1992    10  14            14     2       0         0          0      0
> 1992    10  30            15     4       0         0          0      0
> 1992    11  11            16     2       0         0          0      0
> 1992    11  20            17     0       0         0          0      0
> 1992    12   5            18     0       0         0          0      0
> 1992    12   9            19     3       0         0          0      0
> 1993     1  24            20     5       0         0          0      0
>
>
> What I would like is:
> Year1985
> 1985     8  13             1     1       0         0          0      0
>
> Year 1986
> 1986     8   5             2     3       0         0          0      0
>
> Year 1991
> 1991     4  24             3     0       0         0          0      0
> 1991     4  25             4     0       0         0          0      0
> 1991     8   2             5     0       0         0          0      0
> 1991     8   6             6     0       0         0          0      0
> 1991     9   4             7     2       0         0          0      0
> 1991     9   9             8     4       0         0          0      0
> 1991    12  16             9     0       0         0          0      0
> ect.
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>



-- 
http://had.co.nz/



More information about the R-help mailing list