【问题标题】:merge a dataframe to a model matrix: R将数据框合并到模型矩阵:R
【发布时间】:2020-04-08 07:23:19
【问题描述】:

我在合并两个数据集时遇到了一些困难。 我想将数据集添加到模型矩阵中,但我尝试的一切都不起作用。希望你能帮帮我!

这是需要合并的数据集:

dput(head(posterior))

structure(list(comp.1 = c(0.06, 0.03, 0, 0.03, 0, 0), comp.2 = c(0.61, 
0.42, 0.07, 0.41, 0.31, 0.41), comp.3 = c(0.33, 0.56, 0.93, 0.56, 
0.69, 0.59)), row.names = c(NA, 6L), class = "data.frame")

到这个数据集:

dput(head(x))

structure(c(0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 1, 34, 2, 45, 2, 8, 0, 0, 0, 0, 0, 1, 0, 
0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 
0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 
1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 
0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 29.85, 56.95, 53.85, 
42.3, 70.7, 99.65, 1.47494433546539, 3.27634689625303, 2.03402652377511, 
3.26499480914874, 2.18084241464668, 2.91407858538911), .Dim = c(6L, 
21L), .Dimnames = list(c("1", "2", "3", "4", "5", "6"), c("genderMale", 
"SeniorCitizen", "PartnerYes", "DependentsYes", "tenure", "MultipleLinesYes", 
"InternetServiceFiber optic", "OnlineSecurityYes", "OnlineBackupYes", 
"DeviceProtectionYes", "TechSupportYes", "StreamingTVYes", "StreamingMoviesYes", 
"ContractOne year", "ContractTwo year", "PaperlessBillingYes", 
"PaymentMethodCredit card (automatic)", "PaymentMethodElectronic check", 
"PaymentMethodMailed check", "MonthlyCharges", "TotalCharges"
)))

【问题讨论】:

  • 你能显示你的预期输出吗? cbind(posterior, x) 不行吗?
  • 奇怪的是它起作用了,我尝试了几个不起作用的选项。但是,它现在有效。谢谢!

标签: r matrix merge


【解决方案1】:

我们可以cbind

cbind(posterior, x)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    相关资源
    最近更新 更多