[R] Sorting Data?

Jonathan Baron baron at psych.upenn.edu
Thu May 27 22:43:02 CEST 2004


On 05/27/04 21:58, Martin Klaffenboeck wrote:
>Hello,
>
>Im reading through some manuals, but I cannot find my answer.
>
>I have a file containing many data:
>
>Vpn	Code	Family	Age	F1	F2	...	F17
>1	1	M	46	1	2	...	1
>2	1	D	18	3	2	...	4
>3	2	M	50	3	3	...	3
>...
>and so on.
>
>Now I can read it by:
>
>F = read.table("file", header=T)
>
>but now I want to seperate the mothers (M) and daugthers (D) of the
>family with all the data in all other fields.  How can I do that?
>
>The 'Code' Tells me which mother belongs to which dougther.  I want to
>make a matrix where I have the mothers on one and the daugthers on the
>other axis and compair the distance of every question (F1...F17) and
>the distance of the sum of this questions.  The questions are semantic
>differencials, 5 values.  F4, and F7 must have reverse polarity in this
>case.

The following is not tested and probably contains at least one error.

Lets assume that there is one mother per daughter and one
daughter per mother, and your file is Myfile, and the Codes are
in order.  One way is this:

Myfile$F4 <- -Myfile$F4 # reverse polarity
Myfile$F7 <- -Myfile$F7

Mothers <- Myfile[Family="M",]
Daughters <- Myfile[Family="D",]

Itemdiffs <- Mothers[,-(1:4)]-Daughters[,-(1:4)] # the -(1:4)
                                                 # removes cols 1:4
or
Itemdiffs <- (Mothers-Daughters)[,-(1:4)]
or
Itemdiffs <- abs(Mothers[,-(1:4)]-Daughters[,-(1:4)])
or maybe
Itemdiffs <- rowMeans(abs(Mothers[,-(1:4)]-Daughters[,-(1:4)]))

and for the difference of the sums

Sumdiffs <- rowMeans(Mothers[,-(1:4)]-Daughters[,-(1:4)])

If my assumptions are wrong, then more work is needed.  You might
have to sort by Code, e.g.,

Myfile <- Myfile[sort(Myfile$Code),]

And you might have to match by Code, assuming each daughter has
one mother but one mother can have 2 daughters.  Here is a VERY
CRUDE way (which may not work):

Itemdiff <- matrix(NA,nrow(Daughter),NUMBER OF ITEMS) # fill this in
for (i in 1:nrow(Daughter))
 {Itemdiff[i,] <- Daughter[i,]-Mother[Mother$Code==Daughter[i,Code]}

Jon
-- 
Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page:            http://www.sas.upenn.edu/~baron
R search page:        http://finzi.psych.upenn.edu/




More information about the R-help mailing list