Converting array (produced by "table") to data frame in r or s
I am trying to convert an array that was produced by the "table" function into a normal data frame within a function. I've tried as.data.frame
, and get is.data.frame=TRUE
. However, the object is still an array with an extra dim. This causes problems when I merge it with a data frame, and I end up with a single column of the data frame having array dimensions. How can I coerce the object to a simple data frame? The extra dim contains only the rownames. I've tried setting rownames to null to no avail.
A reproducible example will help if you can provide one, but I've had this problem before too. There's a number of solutions to this, none of them are perfect.
DF <- as.data.frame(as.matrix(table(whatev))) ## makes a data frame w/ one column
## and rownames = names(table)
DF$V2 <- rownames(DF) ## or as.numeric(rownames(DF)) if you want
rownames(DF) <- NULL ## no need for them anymore
Also
DF <- data.frame(V1 = as.vector(names(table(whatever))), V2 = as.numeric(table(w.e))
链接地址: http://www.djcxy.com/p/96258.html
上一篇: Hmisc tilde rowname