简体   繁体   English

rbind(deparse.level, ...) 中的错误:arguments 的列数与 R 不匹配

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

I am trying to do some feature engineering on test and train data.我正在尝试对测试和训练数据进行一些特征工程。 I am well versed with python but new to R.我精通 python 但对 R 很陌生。

#Row binding train & test set for feature engineering
train_test = rbind(train, test)

It seems that my train and test data have different number of columns.似乎我的训练数据和测试数据的列数不同。 How to resolve this so that the only columns which are common in both dataframes stay?如何解决这个问题,以便保留两个数据框中共有的唯一列?

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

I would find out what the column names are for both data frames, take their intersection (common names), and selecting those columns from both data frames:我会找出两个数据框的列名,取它们的交集(通用名称),然后从两个数据框中选择这些列:

train_names <- colnames(train)
test_names <- colnames(test)
common_names <- intersect(train_names, test_names)

train_test <- rbind(train[common_names], test[common_names])

Find the common columns:找到常见的列:

common_cols <- intersect(colnames(train), colnames(test))

Now perform the rbind现在执行 rbind

train_test=rbind(subset(train, select = common_cols), 
  subset(test, select = common_cols))

You'll likely need to use the merge() or inner_join() functions.您可能需要使用 merge() 或 inner_join() 函数。 rbind() only works when the number of columns are the same and when the columns have the same name. rbind()在列数相同且列具有相同名称时才有效。

https://www.programmingr.com/examples/r-dataframe/merge-data-frames/ https://www.programmingr.com/tutorial/inner-join-in-r/ https://www.programmingr.com/examples/r-dataframe/merge-data-frames/ https://www.programmingr.com/tutorial/inner-join-in-r/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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