[R] Transposing Data Frame does not return numeric entries
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Wed Apr 16 03:17:03 CEST 2008
> dat <- read.table(textConnection("
site1 site2 site3 site4
species1 1 2 3 4
species2 3 0 4 5
species3 5 3 7 3
spesies4 0 3 6 7
"))
> dat
site1 site2 site3 site4
species1 1 2 3 4
species2 3 0 4 5
species3 5 3 7 3
spesies4 0 3 6 7
> tdat <- t(dat)
> tdat
species1 species2 species3 spesies4
site1 1 3 5 0
site2 2 0 3 3
site3 3 4 7 6
site4 4 5 3 7
> class(tdat)
[1] "matrix"
Now it does make sense to turn this into a data frame, as all variables
are numeric.
> tdat.df <- data.frame(tdat)
> tdat.df
species1 species2 species3 spesies4
site1 1 3 5 0
site2 2 0 3 3
site3 3 4 7 6
site4 4 5 3 7
>
and Bob's your uncle.
Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary): +61 7 3826 7304
Mobile: +61 4 8819 4402
Home Phone: +61 7 3286 7700
mailto:Bill.Venables at csiro.au
http://www.cmis.csiro.au/bill.venables/
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of stephen sefick
Sent: Wednesday, 16 April 2008 10:40 AM
To: Rolf Turner
Cc: r-help at r-project.org
Subject: Re: [R] Transposing Data Frame does not return numeric entries
ok
site1 site2 site3 site4
species1 1 2 3 4
species2 3 0 4 5
species3 5 3 7 3
spesies4 0 3 6 7
species1 species2 species3 species4
site1 1 3 5 0
site2 2 0 3 3
site3 3 4 7 6
site4 4 5 3 7
this is what I want to do. It is hard to learn R as I have never
programed
anything in my life, but I am learning slowly. I am a researcher who
finds
statistics indispensable. I apologize for my flawed logic but this is
what
I want to do.
x <- read.table("file.csv", sep=",", header=T)
it is is correct I just want to flip the rows and columns. How do I do
this? sorry for my lack of understanding. All help is greatly
appreciated.
Stephen
On Tue, Apr 15, 2008 at 7:08 PM, Rolf Turner <r.turner at auckland.ac.nz>
wrote:
>
> RML: *** Data frames are NOT matrices!!! ***
>
> On 16/04/2008, at 10:35 AM, stephen sefick wrote:
>
> x <- read.table("LittleGarvin.csv", sep=",", header=TRUE)
> > y <- t(x)
> > str(y)
> >
> > chr [1:193, 1:288] "oligocha" "0" " 0" " 0" " 0" "0" ...
> > - attr(*, "dimnames")=List of 2
> > ..$ : chr [1:193] "X" "upwd1201" "upwd0502" "upwd0702" ...
> > ..$ : NULL
> >
> > x is a data frame with a whole bunch of numeric vectors I would like
for
> > the
> > rows and columns to be reversed == transposed but with the same
> > attributes.
> > I am trying to feed the transposed data frame to metaMDS(vegan). I
am
> > stumped- sorry for the large attached file- I am at a loss.
> >
>
> Although it is possible, transposition makes little to no sense
> for data frames. If you want to transpose something you are
> thinking of it as being a *matrix* not a data frame. The
result
> of transposing a data frame is in fact a matrix.
>
> There is as far as I know no structure in R whereby the rows of
> a matrix-like object can have different characteristics. (The
> term ``attributes'' has a technical meaning in R which is
probably
> not what you intend --- but even if you do intend that, it's
not
> possible.)
>
> The complaint made in your subject line is that the result of
> your transposition is not numeric. It is in fact character.
> That's because there is character data in the data frame x.
> When you transpose x, it is first coerced into a matrix. If
there
> are any character or factor columns in x the coercion will
result
> in a character matrix, the transpose of which will of course be
a
> character matrix.
>
> The solution is of course to remove the character or factor
columns
> from x before transposition.
>
> If you ``can't'' do this, then you ***can't*** transpose ---
> transposition
> just doesn't make any sense at all. Simple as that.
>
> cheers,
>
> Rolf Turner
>
>
>
> ######################################################################
> Attention:This e-mail message is privileged and confidential. If you
are
> not theintended recipient please delete the message and notify the
> sender.Any views or opinions presented are solely those of the author.
>
> This e-mail has been scanned and cleared by MailMarshal
> www.marshalsoftware.com
> ######################################################################
>
--
Let's not spend our time and resources thinking about things that are so
little or so large that all they really do for us is puff us up and make
us
feel like gods. We are mammals, and have not exhausted the annoying
little
problems of being mammals.
-K. Mullis
[[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.
More information about the R-help
mailing list