尝试子集时出现未定义的列错误

时间:2019-02-07 06:10:39

标签: r dataframe subset

subset_car_data <- car_data[car_data, car_data$Car_Type == "N" & car_data$Term == 60 & car_data$FICO>=675 & car_data$FICO<=725 & car_data$Amount>=30000 & car_data$Amount<=40000]

这是我的代码。我正在尝试根据特定条件从car_data创建子集subset_car_data。但是,我不断收到错误消息:enter image description here

1 个答案:

答案 0 :(得分:1)

df <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
df
   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1   0  0  0  1  1  1  0  1  0   1
2   0  1  1  1  0  0  1  1  0   0
3   0  1  1  0  0  0  1  0  0   0
4   0  0  1  0  1  1  1  1  1   0
5   0  0  1  0  0  1  0  1  0   0
6   1  0  0  1  1  0  1  1  1   0
7   1  0  1  0  1  0  1  1  1   0
8   0  0  0  1  0  0  1  0  0   1
9   0  0  0  0  1  0  1  0  1   1
10  0  0  1  0  0  0  1  1  1   1

您应该执行以下操作:

subset_df <- df[df$X1 == 1 & df$X2 == 1 & df$X3 == 1,]
subset_df
   X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
8   1  1  1  0  0  1  0  0  0   0
10  1  1  1  1  0  1  0  0  0   1

代替:

subset_df <- df[df,df$X1 == 1 & df$X2 == 1 & df$X3 == 1]