[R] Rearrange data.

From: Ritwik Sinha <ritwik.sinha_at_gmail.com>
Date: Thu 20 Jul 2006 - 12:56:32 EST


Hi,

I am trying to rearrange the following data

d.f <- data.frame(x=c(1,1,2,2), y=c(1,2,1,2), vals=c("a11", "a12", "a21", "a22"))

to look like a table with x as the rows and y as the columns, something like

    y 1 2
x

1    a11         a12
2    a21         a22

I tried doing this

funny <- function(x,y){d.f[d.f$x==x & d.f$y==y,3]}

outer(1:2,1:2, FUN="funny")
But get the error

Error in outer(1:2, 1:2, FUN = "funny") : dim<- : dims [product 4] do not match the length of object [2]

What am I doing wrong? I am sure there are a hundred different ways of doing this.

version

platform i486-pc-linux-gnu

arch     i486
os       linux-gnu

system i486, linux-gnu
status
major 2
minor 2.1
year 2005
month 12
day 20
svn rev 36812
language R
-- 
Ritwik Sinha
Graduate Student
Epidemiology and Biostatistics
Case Western Reserve University

http://darwin.cwru.edu/~rsinha

______________________________________________
R-help@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.
Received on Thu Jul 20 13:01:32 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 20 Jul 2006 - 14:18:18 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.