liamrevell / phytools

GNU General Public License v3.0
198 stars 56 forks source link

Error in apply(X, 1, sum/rstate) : dim(X) must have a positive length #149

Open alrichardbollans opened 5 months ago

alrichardbollans commented 5 months ago

I am trying to use ancThresh to do ancestral state reconstruction but when I pass my data to ancThresh I keep getting errors related to the format of the data and I'm not sure how to change the input to fix this.

Some code to recreate the errors is given below:

### MWE
### Create some data
# Define the row names and column values
row_names <- c('Mastixiodendron pachyclados', 'Aphanocarpus steyermarkii', 'Perama dichotoma')
values <- c(0, 0, 1)
df <- data.frame(`Binary_Var` = values, row.names = row_names)

simulated_tree <- ape::rtree(3)
simulated_tree$tip.label <- row_names # Assign tip names to the simulated tree

### The following give:
### Error in apply(X, 1, sum) : dim(X) must have a positive length
mcmc<-phytools::ancThresh(simulated_tree,df,sequence = c(0,1))

mcmc<-phytools::ancThresh(simulated_tree,as.matrix(df),sequence = c(0,1))

### The following give:
### Error in apply(X, 1, rstate) : dim(X) must have a positive length

mcmc<-phytools::ancThresh(simulated_tree,df$Binary_Var,sequence = c(0,1)) 

mcmc<-phytools::ancThresh(simulated_tree,c(setNames(df$Binary_Var, row_names)),sequence = c(0,1))

R version: 4.2 phytools version: 2.1-1