Re: [R] Sorting Data?

About this list Date view Thread view Subject view Author view Attachment view

From: Jonathan Baron (
Date: Fri 28 May 2004 - 06:43:02 EST

Message-id: <20040527204302.GA7557@psych>

On 05/27/04 21:58, Martin Klaffenboeck wrote:
>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

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
Itemdiffs <- (Mothers-Daughters)[,-(1:4)]
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]}


Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page:  
R search page:

______________________________________________ mailing list PLEASE do read the posting guide!

About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:13 EST