R否则将两个变量组合为一个的代码

问题描述 投票:1回答:2

我有两个变量-Small_Vehicle和Large_Vehicle,它们分别编码为0和1s。我想将它们组合为一个变量,如果车辆较小,则为1;如果车辆较大,则为2。如果没有数据,我希望R离开的是NA。我已经写了下面的代码,但它给了我错误:

错误:“ TRIdata $ Combined

组合变量已存在于数据集中,但当前具有错误的值。

if (TRIdata$Small_Vehicle = 1) {
TRIdata$Combined<-1 } else {
if (TRIdata$Large_Vehicle=1)  {
TRIdata$Combined<-2 } else {
TRIdata$Combined <- NA }}

请问您对我做错了什么吗?

谢谢!

r if-statement variables
2个回答
2
投票

假设small_vehiclelarge_vehicle是互斥和穷举的类别,我们可以创建Combined而不使用if / then逻辑,如下所示。

small_vehicle <- c(1,0,0,1,1,1,0)
large_vehicle <- c(0,1,1,0,0,0,1)

TRIdata <- data.frame(small_vehicle,large_vehicle)
TRIdata$Combined <- 2*TRIdata$large_vehicle + small_vehicle

TRIdata

...和输出:

> TRIdata
  small_vehicle large_vehicle Combined
1             1             0        1
2             0             1        2
3             0             1        2
4             1             0        1
5             1             0        1
6             1             0        1
7             0             1        2
> 

替代方法

我们可以用ifelse()做同样的事情。

# alternate approach 
small_vehicle <- c(1,0,0,1,1,1,0)
large_vehicle <- c(0,1,1,0,0,0,1)

TRIdata <- data.frame(small_vehicle,large_vehicle)
TRIdata$Combined <- ifelse(TRIdata$small_vehicle == 1,1,2)
TRIdata

...和输出。

> TRIdata
  small_vehicle large_vehicle Combined
1             1             0        1
2             0             1        2
3             0             1        2
4             1             0        1
5             1             0        1
6             1             0        1
7             0             1        2
> 

为什么原始代码不起作用?

我将使用以下代码说明对原始帖子的评论。我们将更正原始文件中的===错误,然后尝试运行它。

# original code with my sample data
small_vehicle <- c(1,0,0,1,1,1,0)
large_vehicle <- c(0,1,1,0,0,0,1)
TRIdata <- data.frame(Small_Vehicle = small_vehicle,
                      Large_Vehicle = large_vehicle)

if (TRIdata$Small_Vehicle == 1) {
     TRIdata$Combined <- 1
} else {
     if (TRIdata$Large_Vehicle == 1)  {
          TRIdata$Combined <- 2
     } else {
          TRIdata$Combined <- NA
     }
}

...产生以下警告:

Warning message:
In if (TRIdata$Small_Vehicle == 1) { :
  the condition has length > 1 and only the first element will be used
> 

[当我们打印结果时,我们观察到R将TRIdata$Small_Vehicle的第一个元素评估为TRUE,并将值1分配给TRIdata$Combined中的每个元素。

> TRIdata
  Small_Vehicle Large_Vehicle Combined
1             1             0        1
2             0             1        1
3             0             1        1
4             1             0        1
5             1             0        1
6             1             0        1
7             0             1        1
>

1
投票

尝试一下。

if (TRIdata$Small_Vehicle == 1) {
    TRIdata$Combined<-1 } elseif (TRIdata$Large_Vehicle=1) {
    TRIdata$Combined<-2 } else {
    TRIdata$Combined <- NA }
© www.soinside.com 2019 - 2024. All rights reserved.