CuriousDude
CuriousDude

Reputation: 1117

data.frame function giving error "differing number of rows"

I'm trying to make a PCoA plot and after running cmdscale, I want to create a dataframe of the X and Y points for later use and plotting purposes. Below is my code and the error I get:

my_data <- decostand(data_sp, method = "total") # quick transform of my species abundances
my_data.dist <- vegdist(my_data, method = "bray")
mds <- cmdscale(my_data.dist, eig=T, add=T, k=2)

str(mds)
List of 5
 $ points: num [1:179, 1:2] 0.148 0.323 -0.305 0.246 0.293 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:179] "EK0454_3" "EK0462_3" "EK0501_4" "EK0513_3" ...
  .. ..$ : NULL
 $ eig   : num [1:179] 33.32 9.69 8.95 7.8 7.46 ...
 $ x     : NULL
 $ ac    : num 0.84
 $ GOF   : num [1:2] 0.227 0.227

mds$points
                 [,1]          [,2]
EK0454_3  0.147928991  2.612121e-01
EK0462_3  0.322609620 -4.035117e-01
EK0501_4 -0.304955855  1.127808e-01
EK0513_3  0.246433313 -5.261442e-01
EK0523_3  0.292602589  1.271712e-01
EK0553_3  0.458699623  5.973354e-03
.
.
.

# use the site labels in metadata file data_env
mds.df <- data.frame(Sample=rownames(data_env$Sites), X=mds$points[,1], Y=mds$points[,2])

But I get the following error at the last point when making my dataframe:

Error in data.frame(Sample = rownames(data_env$Sites), X = mds.values[,  : 
  arguments imply differing number of rows: 0, 179

When I run mds$points[,1] and mds$points[,2] I get values for my 179 data points so I don't see what is causing this type of error indicating I have different number of rows? I have 179 rows in both instances.

Furthermore, when calling data_env$Sites, it has 179 sites and the labels show up properly when called. No issues there and the length is correct.

str(data_env$Sites)
chr [1:179] "EK0454_3" "EK0462_3" "EK0501_4" "EK0513_3" "EK0523_3" "EK0553_3" "EK0570_4" "EK0609_4" "EK0684_4" ...

Upvotes: 0

Views: 228

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

data_env$Sites is a vector. It has no row names. If you really want the row names, omit $Sites:

mds.df <- data.frame(Sample = rownames(data_env), X = mds$points[, 1], Y = mds$points[, 2])

Upvotes: 2

Related Questions