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.
Asked
Active
Viewed 7,106 times
0

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

user1566413
- 41
- 2
- 3
-
Can you try this first? http://stackoverflow.com/questions/10758961/how-to-convert-a-table-to-a-data-frame-in-r – Hana Nov 02 '12 at 21:09
-
1What does your `table()` output look like? A reproducible example is required. – Gavin Simpson Nov 02 '12 at 21:13
1 Answers
1
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))

Señor O
- 17,049
- 2
- 45
- 47