-1

table

I attached a picture, but I currently have a table and would like to turn it into a data frame. However, when I tried

as.data.frame(pred_table)

it returned something completely different. I would like a data frame that has the first column as name, the second titled x, the third y, etc. Thanks for the help!

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Did you try `as.data.frame.matrix(pred_table)` from https://stackoverflow.com/questions/10758961/how-to-convert-a-table-to-a-data-frame – Ronak Shah May 04 '20 at 03:56
  • Please see https://stackoverflow.com/editing-help. Code fences are not single-quotes `'''` followed immediately by code, they are three backticks with a language on a line on its own, in this case `\`\`\`lang-r` and then a newline. Similarly, the code end-fence is three backticks (no language), as in `\`\`\``. – r2evans May 04 '20 at 03:56
  • Please do not post an image of code/data/errors: it cannot be copied or searched (SEO), it breaks screen-readers, and it may not fit well on some mobile devices. Ref: https://meta.stackoverflow.com/a/285557 (and https://xkcd.com/2116/). Please just include the code, console output, or data (e.g., `dput(head(x))` or `data.frame(...)`) directly. – r2evans May 04 '20 at 03:56
  • What do the names `x` and `y` have to do with that table? (Perhaps it has something to do with how you formed that table or the original dataset?) – r2evans May 04 '20 at 04:05
  • @RonakShah that works great, but there doesn't appear to be a column for name, only 0:4. Any ideas on how to fix that? Thanks! – coding_guy0042 May 04 '20 at 04:09

1 Answers1

0

Probably, you can do something like this :

#Convert to dataframe
data <- as.data.frame.matrix(pred_table)
#Assign column names
names(data) <- letters[seq_along(data)]
#Convert rownames to column
data$names <- rownames(data)
#Remove row names. 
rownames(data) <- NULL
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213