[R] (Fwd) Re: paired t-test. Need to rearrange data?
Petr Pikal
petr.pikal at precheza.cz
Tue Aug 8 16:32:28 CEST 2006
------- Forwarded message follows -------
From: Petr Pikal <petr.pikal at precheza.cz>
To: Henrik Parn <henrik.parn at bio.ntnu.no>
Subject: Re: [R] paired t-test. Need to rearrange data?
Date sent: Tue, 08 Aug 2006 16:13:47 +0200
Hi
Uff, it takes me a bit headache but this shall do it
?unstack
?table
?t
new.list<-unstack(test.data,y~id)
new.test.data<-t(as.data.frame(new.list[table(test.data$id)>1]))
# if you have more than 2 catches you need to modify this^^^
t.test(new.test.data[,1], new.test.data[,2], paired=T)
HTH
Petr
On 7 Aug 2006 at 16:43, Henrik Parn wrote:
Date sent: Mon, 07 Aug 2006 16:43:45 +0200
From: Henrik Parn <henrik.parn at bio.ntnu.no>
Send reply to: henrik.parn at bio.ntnu.no
Organization: NTNU
To: Petr Pikal <petr.pikal at precheza.cz>
Subject: Re: [R] paired t-test. Need to rearrange data?
> Dear Peter,
>
> Thanks a lot for your rapid answer! I am afraid that I presented an
> example data set in my previous question that was to nicely
> organised. In fact it more looked like the data I want to acheive
> rather than the data I recieved...
>
> So,may I bother you with a follow-up question:
>
> I have data on morphology on birds from several different years.
> Each year about 50-100 birds are captured, marked with a unique id
> and measured. The recapture rate is very low and on average 1-2
> birds are recaptured the subsequent year. Again a small example data
> set, which hopefully is more realistic:
>
> ####
> year <- as.factor(rep(1:4,each= 5)) # the four study years
> id <- c("a", "b", "c", "d", "e", "b", "f", "g", "h", "a", "i",
> "g", "j", "k", "l", "j", "m", "n", "l", "o") # id of the birds
> captured y <- rnorm(20) # the measure taken test.data <-
> data.frame(year, id, y)
> ####
>
>
> So, in this small example, bird "a" and "b" is captured on year 1
> and recaptured year 2, bird "g" captured on year 2 and 3, and so on.
> On birds that are captured more than once, I would like to do a
> paired t.test where I compare the measure taken at first capture
> with that taken on the second time.
>
> I suppose I need to add a vector indicating whether it is the first
> or second time a bird is captured:
>
> ####
> test.data$time <- ifelse(duplicated(test.data$id), 2, 1)
> ####
>
> But then I am stuck...Do you have a hint of how to proceed? How to
> select the y-values for which I have both a capture and a recapture?
>
> Thanks in advance for taking your time!
>
> Best regards,
> Henrik
>
> Petr Pikal wrote:
>
> >Hi
> >
> >one possibility is
> >
> >t(unstack(test.data, y~id))
> >
> >HTH
> >Petr
> >
> >On 6 Aug 2006 at 16:13, Henrik Parn wrote:
> >
> >Date sent: Sun, 06 Aug 2006 16:13:29 +0200
> >From: Henrik Parn <henrik.parn at bio.ntnu.no>
> >Organization: NTNU
> >To: R-help <r-help at stat.math.ethz.ch>
> >Subject: [R] paired t-test. Need to rearrange data?
> >Send reply to: henrik.parn at bio.ntnu.no
> > <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe>
> > <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>
> >
> >
> >
> >>Dear all,
> >>
> >>I have received some data arranged like this:
> >>
> >># original data
> >>id <- rep(letters[1:6], each=2)
> >>time <- as.factor(rep(1:2, 6))
> >>y <- as.vector(replicate(6, c(rnorm(n=1, mean=1), rnorm(n=1,
> >>mean=2)))) test.data <- data.frame(id, time, y) test.data
> >>
> >>I would like to perform a paired t-test of the y-values at time=1
> >>against those at time=2, with samples paired by their id. Is it
> >>necessary to arrange the data in a format like this:
> >>
> >># rearranged data
> >>id <- letters[1:6]
> >>y1 <- replicate(6, rnorm(n=1, mean=1)) # y-value at time = 1
> >>y2 <- replicate(6, rnorm(n=1, mean=2)) # y-value at time = 2
> >>test.data2 <- data.frame(id, y1, y2) test.data2
> >>
> >>...in order to perform a paired t-test?
> >>t.test(y1, y2, paired=T)
> >>
> >>If yes, which is the most convenient way to rearrange the data? Or
> >>is it possible to apply the paired t-test function to the original
> >>data set?
> >>
> >>And a side question: In my examples, I suppose can I use set.seed
> >>to reproduce the 'rnorm-values' created in the 'original data'
> >>also in my the 'rearranged data'. Can someone give me a hint of
> >>how to apply the same 'seed' to all the rnorms?
> >>
> >>
> >>Thanks a lot in advance!
> >>
> >>
> >>Henrik
> >>
> >>______________________________________________
> >>R-help at stat.math.ethz.ch 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.
> >>
> >>
> >
> >Petr Pikal
> >petr.pikal at precheza.cz
> >
> >
> >
>
> --
> ************************
> Henrik Pärn
> Department of Biology
> NTNU
> 7491 Trondheim
> Norway
>
> +47 735 96282 (office)
> +47 909 89 255 (mobile)
> +47 735 96100 (fax)
> ************************
>
------- End of forwarded message -------Petr Pikal
petr.pikal at precheza.cz
More information about the R-help
mailing list