[R] Doing a Task Without Using a For Loop

Erik Iverson iverson at biostat.wisc.edu
Tue Oct 14 16:07:46 CEST 2008


table(data1$ID, data1$Year)

See ?table and other functions referenced in ?table.



Tom La Bone wrote:
> Assume that I have the dataframe "data1", which is listed at the end of this
> message. I want count the number of lines that each person has for each
> year. For example, the person with ID=213 has 15 entries (NinYear) for 1953.
> The following bit of code calculates NinYear:
> 
> for (i in 1:length(data1$ID)) {
>   data1$NinYear[i] <- length(data1[data1$Year==data1$Year[i] &
>     data1$ID==data1$ID[i],1]) }
> 
> This seems to work but is horribly slow (some files I am working with have
> over 500,000 lines). Can anyone suggest a faster way of doing this, perhaps
> a way that does not use a for loop? Thanks.
> 
> Tom
> 
> ID	Year	NinYear
> 209	1971	0
> 209	1971	0
> 213	1951	0
> 213	1951	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1953	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1954	0
> 213	1955	0
> 213	1955	0
> 234	1953	0
> 234	1953	0
> 234	1953	0
> 234	1953	0
> 234	1953	0
> 234	1958	0
> 234	1958	0
> 234	1965	0
> 234	1965	0
> 234	1965	0
> 249	1952	0
> 249	1952	0
> 
> 
>



More information about the R-help mailing list