[R] using for loop for data frame

Rui Barradas ruipbarradas at sapo.pt
Tue Apr 15 23:35:15 CEST 2014



Em 15-04-2014 18:02, Monaly Mistry escreveu:
> Hi,
> I have a data frame with 15 different years and each year has a different
> sample size.  I'm having trouble writing a for loop to have a t-test done
> for each year for two of the columns.  Thus far I have been able to loop
> over the years but the output is the same t-test 15 times.  I'm new to
> writing loops and functions, and am trying to figure out a way to use them
> together (if there are any general rules to follow, I would be great-full
> to know)
> for(year in 1998:2012){
>    b <-t.test(tr$adj_ind_pop_dif, tr$ind_nei_dif, paired=TRUE)
>    print(b)
> }

The problem with this loop is that you don't use the variable 'year' in 
it. It just repeats the same test 15 times.
> the other way I did it was to take subsets of the dataframe and then do a
> separate t-test for each
> ab<-subset(tr, BroedJaar =="1999")
> t.test(ab$ind_nei_dif, ab$adj_ind_pop_dif, paired=TRUE)

So BroedJaar is a character variable? Try

for(year in 1998:2012){
	ab <- subset(tr, BroedJaar == as.character(year))
	b <- t.test(ab$adj_ind_pop_dif, ab$ind_nei_dif, paired=TRUE)

Note however that this won't store the results of the t-tests, all but 
the last one will be lost. (The last one is stored in 'b'.)
If you want the results of the 15 tests you can do

result <- lapply(1998:2012, function(year){
		ab <- subset(tr, BroedJaar == as.character(year))
		t.test(ab$adj_ind_pop_dif, ab$ind_nei_dif, paired=TRUE)
names(result) <- 1998:2012

Then you can access the results with something like

result[[1]]  # first test
result[["1998"]]  # equivalent

Hope this helps,

Rui Barradas

> Best,
> Monaly.
> 	[[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.

More information about the R-help mailing list