0

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1566413
  • 41
  • 2
  • 3

1 Answers1

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