[R] beginner programming question

Adrian Dusa adi at roda.ro
Mon Dec 22 23:45:34 CET 2003


Thank you all! I did it, and it worked just fine. In the last week I've been 
torturing the syntaxes in various ways, until finally it was all clear. The 
subscripting solution opened new doors for me.
Particularly, the reshape command gave me about three days of a head ache. I 
read the help about 20 times, trying to figure out how to do it; the trouble 
with the help was that it doesn't present examples of reshaping for multiple 
sets of varying variables, nor that the new variables' names in the long format 
should be defined as a vector with the v.names attribute.

Anyway, the syntax is:

> x <- read.table("clipboard", header=T)
> x
  rel1 rel2 rel3 age0 age1 age2 age3 sex0 sex1 sex2 sex3
1    1    3   NA   25   23    2   NA    1    2    1   NA
2    4    1    3   35   67   34   10    2    2    1    2
3    1    4    4   39   40   59   60    1    2    2    1
4    4   NA   NA   45   70   NA   NA    2    2   NA   NA

> xx <- reshape(x, varying=list(names(x)[1:3], names(x)[5:7], 
+ names(x)[9:11]), v.names=c("rel", "age", "sex"), direction="long")
> xx
    age0 sex0 time rel age sex id
1.1   25    1    1   1  23   2  1
2.1   35    2    1   4  67   2  2
3.1   39    1    1   1  40   2  3
4.1   45    2    1   4  70   2  4
1.2   25    1    2   3   2   1  1
2.2   35    2    2   1  34   1  2
3.2   39    1    2   4  59   2  3
4.2   45    2    2  NA  NA  NA  4
1.3   25    1    3  NA  NA  NA  1
2.3   35    2    3   3  10   2  2
3.3   39    1    3   4  60   1  3
4.3   45    2    3  NA  NA  NA  4

> xx <- subset(xx, xx$rel==1)
> rbind(subset(xx, xx$sex0==1)[,c("age0","age")],
+ subset(xx, xx$sex==1)[,c("age","age0")])
    age0 age
1.1   25  23
3.1   39  40
2.2   35  34

I wish you a Merry Xmas, you are a truly great community.
Adrian

-----Original Message-----
From: Thomas Lumley [mailto:tlumley at u.washington.edu] 
Sent: Thursday, December 18, 2003 5:53 PM
To: Tony Plate
Cc: adi at roda.ro; r-help at stat.math.ethz.ch
Subject: Re: [R] beginner programming question

On Wed, 17 Dec 2003, Tony Plate wrote:

> Another way to approach this is to first massage the data into a more
> regular format.  This may or may not be simpler or faster than other
> solutions suggested.

You could also use the reshape() command to do the massaging

	-thomas

>  > x <- read.table("clipboard", header=T)
>  > x
>    rel1 rel2 rel3 age0 age1 age2 age3 sex0 sex1 sex2 sex3
> 1    1    3   NA   25   23    2   NA    1    2    1   NA
> 2    4    1    3   35   67   34   10    2    2    1    2
> 3    1    4    4   39   40   59   60    1    2    2    1
> 4    4   NA   NA   45   70   NA   NA    2    2   NA   NA
>  > nn <- c("rel","age0","age","sex0","sex")
>  > xx <- rbind("colnames<-"(x[,c("rel1","age0","age1","sex0","sex1")], nn),
> +  "colnames<-"(x[,c("rel2","age0","age2","sex0","sex2")], nn),
> +  "colnames<-"(x[,c("rel3","age0","age3","sex0","sex3")], nn))
>  > xx
>     rel age0 age sex0 sex
> 1    1   25  23    1   2
> 2    4   35  67    2   2
> 3    1   39  40    1   2
> 4    4   45  70    2   2
> 11   3   25   2    1   1
> 21   1   35  34    2   1
> 31   4   39  59    1   2
> 41  NA   45  NA    2  NA
> 12  NA   25  NA    1  NA
> 22   3   35  10    2   2
> 32   4   39  60    1   1
> 42  NA   45  NA    2  NA
>  >
>  > rbind(subset(xx, xx$rel==1 & (xx$sex0==1 |
> xx$sex0==xx$sex))[,c("age0","age")], subset(xx, xx$rel==1 & xx$sex==1 &
> xx$sex0!=xx$sex)[,c("age","age0")])
>     age0 age
> 1    25  23
> 3    39  40
> 21   35  34
>  >
>
> hope this helps,
>
> Tony Plate
>
> PS.  To advanced R users: Is the above usage of the "colnames<-" function
> within an expression regarded as acceptable or as undesirable programming
> style? -- I've rarely seen it used, but it can be quite useful.




-------------------------------------------------
This mail sent through IMP: http://horde.org/imp/




More information about the R-help mailing list