[R] manipulate dataframe

jim holtman jholtman at gmail.com
Sun Feb 6 15:07:48 CET 2011


Here is one way of doing it:

> x
  x1 x2 row
1  1  a   1
2  1  b   2
3  1  c   3
4  2  d   1
5  2  e   2
6  2  f   3
7  3  g   1
8  3  h   2
9  3  i   3
> # create indices for new table
> x$row <- ave(x$x1, x$x1, FUN=seq_along)
> # create output matrix
> result <- matrix('', max(x$row), max(x$x1))
> result[cbind(x$row, x$x1)] <- x$x2
> result
     [,1] [,2] [,3]
[1,] "a"  "d"  "g"
[2,] "b"  "e"  "h"
[3,] "c"  "f"  "i"


On Sun, Feb 6, 2011 at 6:13 AM, André de Boer <rnieuws at gmail.com> wrote:
> Hello,
>
> Can someone give me hint to change a data.frame.
> I want to split a column in more columns depending on the value of a other
> column.
> Thanks for the reaction,
> Andre
>
> Example:
>> dat
>  x1 x2
> 1  1  a
> 2  1  b
> 3  1  c
> 4  2  d
> 5  2  e
> 6  2  f
> 7  3  g
> 8  3  h
> 9  3  i
>
> in
>
>> dur
>  d1 d2 d3
> 1  a  d  g
> 2  b  e  h
> 3  c  f  i
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list