1

I find myself struggling with data import for further nMDS and Bioenv analysis with "vegan" and "ggplot2". I have a data frame "Taxa" that looks like this (the values are there to mean it is "numeric". —

head(Taxa)

    X1      Station1   Stations1_2     Stations1_3  ...

  Species1    123           456              789

  Species2    123           456              789

   Species3    123           456              789

...

After I transpose my data to have the stations (observations) as rows

Taxa <- t(Taxa)

       X_1        Species1     Species2     Species3  ...
     Station1      123           456              789
     Species1_2    123           456              789
     Species1_3    123           456              789

...

Now if I check how the data has been transposed I see that it has been converted into a "matrix"

class(Taxa) [1] "matrix"

Now I can change again the matrix into a data frame

Taxa.df <- data.frame(Taxa)

And what I get then is the following:

head(Taxa.df)

                       X1          X2             X3 
          X_1        Species1     Species2     Species3  ...
          Station1      123           456              789
          Species1_2    123           456              789
          Species1_3    123           456              789
          ...

Now what I would need is to get the first row to become the columns header so that I can restore the initial structure

colnames(Taxa.df)=Taxa.df[1,]

When I do this this happens to the data frame

                         23          10             16    ....    
           X_1        Species1     Species2     Species3  ...
           Station1      123           456              789
           Species1_2    123           456              789
           Species1_3    123           456              789
           ...

I don't manage to get to have the first row as header.

If I can't do this I can't run the transformation I need and all the stats analysis I still need to run. I spent the whole day simply trying to import the data from xlsx on Rstudio for Mac and solve this issue. I hope you guys can help. I did already look around a lot and mostly thought to have found these two links as useful answers, but nothing solved my exact problem.

http://r.789695.n4.nabble.com/Transposing-Data-Frame-does-not-return-numeric-entries-td852889.html

Why does the transpose function change numeric to character in R?

1 Answers1

0

The first variable in your data frame was X1 with values Species1 etc. You should have read your data so that the first variable is numeric (Species1) which you can achieve with argument row.names=1 in the read.* command. Alternatively, you can only transpose the numeric data and then label the rows and columns with the original data. The following may work

mat <- t(Taxa[,-1]) # remove col 1
colnames(mat) <- rownames(Taxa)
mat <- as.data.frame(mat)

However, I think you have not posted the actual output of your R commands, but written by hand the things you think are essential to the structure. So it may be that your data are different than you display, and you may also have non-numeric rows. Just check sum(Taxa) which is number if your data are numeric, and sum(Taxa[,-1]) which is a number if removing the first column is sufficient, and summary(Taxa) which gives Mean and Median for columns which are all numeric (including first row).

Jari Oksanen
  • 3,287
  • 1
  • 11
  • 15