[R] Rearrange data.

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 20 05:04:38 CEST 2006


This is FAQ 7.17:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-does-outer_0028_0029-behave-strangely-with-my-function_003f

On 7/19/06, Ritwik Sinha <ritwik.sinha at gmail.com> wrote:
> 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 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.
>



More information about the R-help mailing list