kennyut
kennyut

Reputation: 3821

convert "Table" to "data.frame"

I want to covert count.table (class = "table") to data.frame

> count.table
         High  Low  Mid None
   P2-1   198   41   35  160
   P2-2   179   49   41  165
   P2-3   184   45   24  181
   P2-4   181   43   29  181
   P3-1    64   70   13  280
   P3-2    87   79   24  244
   P3-3    60   84   13  277
   P3-4   108   60   22  244
   P3-5    87   65   19  263

I used:

count.df <- as.data.frame.matrix(count.table)

But the problem is in count.df the variable name for the first column is set as "row.names".

see:

> names(count.df)
[1] "High" "Low"  "Mid"  "None"

Question: How to assign a variable name for the first column? Thanks!

Upvotes: 1

Views: 2746

Answers (1)

agstudy
agstudy

Reputation: 121568

You can create it :

count.df$var_names <- row.names(count.df)

If you want it as the first column:

cbind.data.frame(var_names=row.names(count.df),count.df)

Upvotes: 2

Related Questions