[英]R if else code for combining two variables into one
I have two variables - Small_Vehicle and Large_Vehicle which are coded as 0 and 1s.我有两个变量 - Small_Vehicle 和 Large_Vehicle 编码为 0 和 1。 I want to combine them into one variable where if the vehicle was small - 1, and if it was large - 2. If there is no data, I want R to leave is as NA.
我想将它们组合成一个变量,如果车辆很小 - 1,如果它很大 - 2。如果没有数据,我希望 R 离开为 NA。 I have written the code below but it gives me the error:
我已经编写了下面的代码,但它给了我错误:
Error: unexpected '}' in " TRIdata$Combined <- NA }"错误:“TRIdata$Combined <- NA }”中出现意外的“}”
The Combined variable already exists in the dataset but has the wrong values currently.组合变量已存在于数据集中,但当前具有错误值。
if (TRIdata$Small_Vehicle = 1) {
TRIdata$Combined<-1 } else {
if (TRIdata$Large_Vehicle=1) {
TRIdata$Combined<-2 } else {
TRIdata$Combined <- NA }}
Do you have any suggestions about what I am doing wrong, please?你对我做错了什么有什么建议吗?
Thank you!谢谢!
Assuming that small_vehicle
and large_vehicle
are mutually exclusive and exhaustive categories, we can create Combined
without if / then logic as follows.假设
small_vehicle
和large_vehicle
是互斥且穷举的类别,我们可以创建没有 if/then 逻辑的Combined
,如下所示。
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
...and the output: ...和 output:
> 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
>
We can do the same thing with ifelse()
.我们可以用
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
...and the output. ...和 output。
> 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
>
I'll illustrate my comment on the original post with the following code.我将使用以下代码说明我对原始帖子的评论。 We'll correct the
=
vs. ==
error in the original, and then attempt to run it.我们将更正原始版本中的
=
vs. ==
错误,然后尝试运行它。
# 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
}
}
...produces the following warning: ...产生以下警告:
Warning message:
In if (TRIdata$Small_Vehicle == 1) { :
the condition has length > 1 and only the first element will be used
>
When we print the results, we observe that R evaluated the first element of TRIdata$Small_Vehicle
as TRUE, and assigned the value of 1 to every element in TRIdata$Combined
.当我们打印结果时,我们观察到 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
>
Try this.尝试这个。
if (TRIdata$Small_Vehicle == 1) {
TRIdata$Combined<-1 } elseif (TRIdata$Large_Vehicle=1) {
TRIdata$Combined<-2 } else {
TRIdata$Combined <- NA }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.