[R] Data transformation

Sarah Goslee sarah.goslee at gmail.com
Mon Jan 25 23:49:53 CET 2010


Well, I have no idea how to get from one to the other. There's
col1 and col2 but no var1 var2 var3, etc. I thought perhaps col1
was the row index and col2 was the column index, but that doesn't
match up either, and not all the cell values are 1.

So you will need to explain more clearly what you intend.

Meanwhile, you might try reshape, or perhaps crosstab from the
ecodist package.

Sarah

On Mon, Jan 25, 2010 at 5:39 PM, Lisa <lisajca at gmail.com> wrote:
>
> Dear all,
>
> I  have a dataset that looks like this:
>
> x <- read.table(textConnection("col1 col2
> 3 1
> 2 2
> 4 7
> 8 6
> 5 10"), header=TRUE)
>
> I want to rewrite it as below:
>
> var1 var2 var3 var4 var5 var6 var7 var8 var9 var10
>    1     0     1      0     0     0     0     0      0      0
>    0     2     0      0     0     0     0     0      0      0
>    0     0     0      1     0     0     1     0      0      0
>    0     0     0      0     0     1     0     1      0      0
>    0     0     0      0     1     0     0     0      0      1
>
> Can anybody please help how to get this done? Your help would be greatly
> appreciated.
>
> Lisa
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list