[R] transpose? reshape? flipping? challenge with data frame

David.Gobbett at csiro.au David.Gobbett at csiro.au
Fri Apr 23 07:03:42 CEST 2010


Fabulous!  I managed to get close with t(), but had obviously missed the step of setting the rownames first.

Thanks for your help!
David

-----Original Message-----
From: Ista Zahn [mailto:istazahn at gmail.com] 
Sent: Friday, 23 April 2010 2:29 PM
To: Gobbett, David (CSE, Waite Campus)
Cc: r-help at r-project.org
Subject: Re: [R] transpose? reshape? flipping? challenge with data frame

Hi David,
There are many ways, including

rownames(propsum) <- propsum$coverClass
propsum$coverClass <- NULL
t(propsum)

Best,
Ista

On Fri, Apr 23, 2010 at 5:43 AM,  <David.Gobbett at csiro.au> wrote:
> Greetings all,
>
> I am having difficulty transposing, reshaping, flipping (not sure which) a data frame which is read from a DBF file.  I have tried using t(), reshape() and other approaches without success. Can anyone please suggest an way (elegant or not) of flipping this data around ?
>
> The initial data is like propsum (defined below), and I want it to look like tpropsum once reformed.
>> propsum
>  coverClass R209120812 R209122212
> 1          C         NA       0.05
> 2          G       0.49       0.35
> 3          L       0.38       0.41
> 4          O       0.04       0.09
> 5          S       0.09       0.10
>
>> tpropsum
>       Image    C    G    L    O  L.1
> 1 R209120812   NA 0.49 0.38 0.04 0.09
> 2 R209122212 0.05 0.35 0.41 0.09 0.10
>
> # Example datasets
> # Input
> propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"),
>                      R209120812=c(NA, 0.49, 0.38, 0.04, 0.09),
>                      R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10))
>
> # Desired output
> tpropsum <- data.frame(Image=c("R209120812", "R209122212"),
>                      C=c(NA, 0.05),
>                      G=c(0.49, 0.35),
>                      L=c(0.38, 0.41),
>                      O=c(0.04, 0.09),
>                      L=c(0.09, 0.10))
>
> Thanks,
> David
>
>        [[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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list