简体   繁体   中英

How to combine tables with different numbers of columns in R?

I know it must be a simple fix but I've been stuck on this for ages. I've got three tables, two with three columns, one with two columns. I want to combine them to show, for each statistical test, how many times a particular model was the best one. Below are the tables I have at the moment. What's the best way to combine them so I have R_Squared, AIC and BIC as the column headers and Quadratic, Polynomial and Linear as the row names with the results (and a zero for Linear under the R Squared column)? Thank you so much for your help!

这是我目前的桌子

Not the most elegant solution but does create the desired table.

library(dplyr)

# Data
R_Squared <- data.frame(Polynomial = c(283), Quadratic = c(2))
AIC <- data.frame(Polynomial = c(201), Quadratic = c(60), Linear = c(24))
BIC <- data.frame(Polynomial = c(196), Quadratic = c(62), Linear = c(27))

df <- bind_rows(AIC, BIC, R_Squared)
# add row names
row.names(df) <- c('AIC','BIK','R_Squared')
# returns the transpose of df
df <- t(df)

Output:

           AIC BIK R_Squared
Polynomial 201 196       283
Quadratic   60  62         2
Linear      24  27        NA

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM