I need a short R script that can reorganise both large matrices and data.frames into 3 column data.frames. My current script can handle matrices but returns an error message when I input a data.frame. Any advice on how I can force it to handle both classes?
library(FD)
ds<-dummy$abun #a matrix
#reorganize into 3 column data.frame
q<-rep.int(row.names(ds),ncol(ds))
p<-rep(colnames(ds),each = (nrow(ds)))
ssf<-data.frame(q,p,as.numeric(ds))
names(ssf) <- c("site", "species", "freq")
Works fine but I want it to be able to reorganize both matrices and dataframes (i.e.)
ds<-as.data.frame(dummy$abun) #a matrix
#reorganize into 3 column data.frame
q<-rep.int(row.names(ds),ncol(ds))
p<-rep(colnames(ds),each = (nrow(ds)))
ssf<-data.frame(q,p,as.numeric(ds))
names(ssf) <- c("site", "species", "freq")
returns
'Error in data.frame(q, p, as.numeric(ds)) : (list) object cannot be coerced to type 'double'