微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

不匹配的列数

如何解决不匹配的列数

我正在尝试使用 rbind 函数为匹配进程创建一些数据,但出现此错误

Error in rbind(deparse.level,...) : 
  numbers of columns of arguments do not match

我已经检查并调整了顺序,因此它们肯定匹配,但仍然出现错误。知道为什么吗?

这是我的代码

# Match on the data from the year before treatment,matching on counttotal and countbrown
matchData <-
  rbind(treat_firms_1year_prior[,-c(
    grep("year_int_tx",colnames(treat_firms_1year_prior)),grep("matchingyear",grep("flag",colnames(treat_firms_1year_prior))
  )],control_firms_year_int_tx)

这些是列名:

> colnames(treat_firms_1year_prior)
 [1] "investor"            "dealyear"            "totalUSD"            "counttotal"          "greenUSD"            "countgreen"         
 [7] "brownUSD"            "countbrown"          "signatory"           "treatment"           "firsttreat"          "matchingyear"       
[13] "country"             "region"              "yearest"             "strategy"            "capsources"          "historicfunds"      
[19] "eligible_treat_firm" "year_int_tx"         "flag"               
> colnames(control_firms_year_int_tx)
 [1] "investor"            "dealyear"            "totalUSD"            "counttotal"          "greenUSD"            "countgreen"         
 [7] "brownUSD"            "countbrown"          "signatory"           "treatment"           "firsttreat"          "matchingyear"       
[13] "country"             "region"              "yearest"             "strategy"            "capsources"          "historicfunds"      
[19] "eligible_treat_firm" "year_int_tx"         "flag"  

解决方法

rbind 要求 2 个数据框具有相同的列数。

您的列与 treat_firms_1year_prior 删除 3 列(“year_int_tx”、“matchingyear”、flag”)不同,而 control_firms_year_int_tx 没有。

您还需要将它们放在 control_firms_year_int_tx 中,或将它们保留在 treat_firms_1year_prior 中。

matchData <-
  rbind(treat_firms_1year_prior[,-c(
    grep("year_int_tx",colnames(treat_firms_1year_prior)),grep("matchingyear",grep("flag",colnames(treat_firms_1year_prior))
  )],control_firms_year_int_tx[,colnames(treat_firms_1year_prior))
  )])

excludeColumns <- c("year_int_tx","matchingyear","flag")

matchData <-
  rbind(
    treat_firms_1year_prior[,!(names(treat_firms_1year_prior) %in% excludeColumns)],!(names(control_firms_year_int_tx) %in% excludeColumns)]
  )

matchData <-
  rbind(treat_firms_1year_prior,control_firms_year_int_tx)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。