# [R] Transformation of a data frame

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Sep 13 09:31:40 CEST 2006

```one approach is to use reshape(), e.g.,

# suppose that 'dat' is your data.frame, then
res <- reshape(dat, direction = "wide", idvar = "Q", timevar = "S")
res[is.na(res)] <- 0
res

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm

----- Original Message -----
From: "Des Callaghan" <descall at blueyonder.co.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, September 12, 2006 8:43 AM
Subject: [R] Transformation of a data frame

> Dear R-helpers,
>
> Apologies in advance for this (probably) simple question.  I've
> searched the
> R Archive and can't seem to find a solution to my problem.
>
> I have a data frame of vegetation quadrat data with the following
> format:
>
> Q S C
> 1 A 5
> 1 B 10
> 1 C 50
> 1 D 10
> 2 A 20
> 2 E 10
> 2 C 40
> 3 D 5
> 3 F 1
> 3 G 5
> 3 B 75
>
> Where Q is the sample (vegetation quadrats), S is the species and C
> is the
> percentage cover of each species within the sample.  I wish to
> transform
> this into a community data matrix for analysis within the vegan
> package,
> which needs the following data frame format:
>
> Q A  B  C  D  E  F  G
> 1 5  10 50 10 0  0  0
> 2 20 0  40 0  10 0  0
> 3 0  75 0  5  0  1  75
>
> I can't manage to do this transformation so your help would be much
> appreciated.  Thanks very much in advance.
>
> All the best,
> Des
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

```