Matching row and column names in two files during randomization

270 views
Skip to first unread message

Catherine Hulshof

unread,
Feb 25, 2015, 9:43:37 AM2/25/15
to davi...@googlegroups.com
Dear Davis R Users Group!

I'm having a problem with (I think) the way two tables are being read into R. The problem boils down to (I think) column names in one file not matching row names in another file (required by functions in FD library) when I run a randomization function. Does anyone see any obvious problems with the following code? (sample files attached too instead of just making sample dataframe because I think it may have to do with the txt format of the files themselves...just shooting in the dark at this point). 

Thanks for any advice!
Catherine


library(FD)
library(picante)

my.sample <- read.table("null.example.sample.txt",sep="\t",row.names=1,header=T)
traits<-read.table("null.example.traits.txt",sep="\t",row.names=1,header=T)

dbfd.shuff<-function(x)
{
  rownames(x)<-sample(rownames(x),length(rownames(x)),replace=F)
  dbFD(x[colnames(my.sample),],my.sample)$FDis
}
obs.null.output<-cbind(dbFD(traits[colnames(my.sample),],my.sample)$FDis,replicate(9,dbfd.shuff(traits)))
obs.null.output


--
Catherine M. Hulshof, PhD
Assistant Professor, 
Department of Biology
Recinto Universitario de Mayagüez
Universidad de Puerto Rico
http://catherinehulshof.wordpress.com/

null.example.sample.txt
null.example.traits.txt

Vince S. Buffalo

unread,
Feb 25, 2015, 1:16:43 PM2/25/15
to davi...@googlegroups.com
Hi Catherine,

First off — I'm not familiar with these particular packages, so I can't help you entirely there. But, I tried to replicate the issue and found this:

> obs.null.output<-cbind(dbFD(traits[colnames(my.sample),],my.sample)$FDis,replicate(9,dbfd.shuff(traits)))
Error in dbFD(traits[colnames(my.sample), ], my.sample) :
  'x' must have names.

x is the first argument, and you've provided it a vector. This is a subtly of R: when accessing a single column, R will return a vector, not a dataframe of one column. You can prevent R from dropping these other dimensions by setting drop=FALSE in the bracket operator:

> dbFD(traits[colnames(my.sample),, drop=FALSE],my.sample)
FRic: Only one continuous trait or dimension in 'x'. FRic was measured as the range, NOT as the convex hull volume.
FDiv: Cannot not be computed when 'x' contains one single continuous trait or dimension.

Ok, that worked — let's make the same change to your function:

dbfd.shuff<-function(x)
{
  rownames(x)<-sample(rownames(x),length(rownames(x)),replace=F)
  dbFD(x[colnames(my.sample),, drop=FALSE],my.sample)$FDis
}

Now test it:

> obs.null.output<-cbind(dbFD(traits[colnames(my.sample),, drop=FALSE],my.sample)$FDis,replicate(9,dbfd.shuff(traits)))
FRic: Only one continuous trait or dimension in 'x'. FRic was measured as the range, NOT as the convex hull volume.
FDiv: Cannot not be computed when 'x' contains one single continuous trait or dimension.
FRic: Only one continuous trait or dimension in 'x'. FRic was measured as the range, NOT as the convex hull volume.
FDiv: Cannot not be computed when 'x' contains one single continuous trait or dimension.

 
Something's working! And this produces output (that you should check in light of all these warnings):

> obs.null.output[1:5, 1:5]
          [,1]      [,2]      [,3]      [,4]      [,5]
p140 0.5186213 0.7345921 1.2245037 0.5973686 0.8279857
p190 0.6575400 0.5258475 0.5867715 0.5258377 0.5463295
p240 0.6714371 0.9018721 0.8990632 0.5508297 0.6955668
p290 0.2113815 1.1923106 0.6213570 0.5040267 0.8639043
p340 0.4941634 1.0224523 0.9082391 0.6117156 0.8981580

Hope this helps,
Vince





--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--
Vince Buffalo
@vsbuffalo :: vincebuffalo.com
Coop Lab :: Population Biology Graduate Group
University of California, Davis

Catherine Hulshof

unread,
Feb 25, 2015, 1:35:50 PM2/25/15
to davi...@googlegroups.com
Dear Vince:

Thank you! I had a feeling there were subtleties involved, I knew R was dropping something but I didn't quite know how to articulate it. Huge help!

Catherine

Reply all
Reply to author
Forward
0 new messages