Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于R中的另一列更改一列中的值_R - Fatal编程技术网

基于R中的另一列更改一列中的值

基于R中的另一列更改一列中的值,r,R,因此,我使用R并试图通过比较两列来更改一列中数据帧中的值。我有点像 Median MyPrice 10 0 20 18 20 20 30 35 15 NA 我想说的是 if(MyPrice == 0 & MyPrice < Median){MyPrice <- 1 }else if (MyPrice == Median){MyPrice <- 2 }else if (MyPrice > Me

因此,我使用R并试图通过比较两列来更改一列中数据帧中的值。我有点像

Median   MyPrice
10       0
20       18
20       20
30       35
15       NA
我想说的是

if(MyPrice == 0 & MyPrice < Median){MyPrice <- 1
  }else if (MyPrice == Median){MyPrice <- 2
  }else if (MyPrice > Median){MyPrice <- 3
  }else {MyPrice <- 4}
for(i in MyPrice){if(MyPrice == 0 & MyPrice < Median){MyPrice <- 1
  }else if (MyPrice == Median){MyPrice <- 2
  }else if (MyPrice > Median){MyPrice <- 3
  }else {MyPrice <- 4}
  }
但总有一个错误。我也试过类似的方法

if(MyPrice == 0 & MyPrice < Median){MyPrice <- 1
  }else if (MyPrice == Median){MyPrice <- 2
  }else if (MyPrice > Median){MyPrice <- 3
  }else {MyPrice <- 4}
for(i in MyPrice){if(MyPrice == 0 & MyPrice < Median){MyPrice <- 1
  }else if (MyPrice == Median){MyPrice <- 2
  }else if (MyPrice > Median){MyPrice <- 3
  }else {MyPrice <- 4}
  }

for(i in MyPrice){if(MyPrice==0&MyPrice
循环使用
。首先将每个比较设置为4

> x$Comp=4
> x$Comp[x$Median>x$MyPrice]=1 #if Median is higher, comparison = 1
> x$Comp[x$Median==x$MyPrice]=2 #if Median is equal to MyPrice, comparison = 2
> x$Comp[x$Median<x$MyPrice]=3 #if Median is lower, comparison = 3
> x
  Median MyPrice Comp
1     10       0    1
2     20      18    1
3     20      20    2
4     30      35    3
5     15      NA    4
>x$Comp=4
>x$Comp[x$Median>x$MyPrice]=1#如果中位数较高,比较=1
>x$Comp[x$Median==x$MyPrice]=2#如果Median等于MyPrice,则比较=2
>x$Comp[x$Median x
我的价格中位数
1     10       0    1
2     20      18    1
3     20      20    2
4     30      35    3
5 15 NA 4

您不必为
循环使用
。首先将每个比较设置为4

> x$Comp=4
> x$Comp[x$Median>x$MyPrice]=1 #if Median is higher, comparison = 1
> x$Comp[x$Median==x$MyPrice]=2 #if Median is equal to MyPrice, comparison = 2
> x$Comp[x$Median<x$MyPrice]=3 #if Median is lower, comparison = 3
> x
  Median MyPrice Comp
1     10       0    1
2     20      18    1
3     20      20    2
4     30      35    3
5     15      NA    4
>x$Comp=4
>x$Comp[x$Median>x$MyPrice]=1#如果中位数较高,比较=1
>x$Comp[x$Median==x$MyPrice]=2#如果Median等于MyPrice,则比较=2
>x$Comp[x$Median x
我的价格中位数
1     10       0    1
2     20      18    1
3     20      20    2
4     30      35    3
5 15 NA 4

给出第一个参数,如果
MyPrice==0&MyPrice
,那么第二行的Median:
20
和MyPrice:
18
也应该是
4
。下面是一个工作嵌套的ifelse语句,后面是一个NA处理程序

df <- as.data.frame(matrix(c(10,0,20,18,20,20,30,35,15,NA), byrow = T, ncol = 2))
colnames(df) <- c("Median","MyPrice")

df$NewPrice <- ifelse(df$MyPrice == 0 & df$MyPrice < df$Median, 1, 
                      ifelse(df$MyPrice == df$Median, 2, 
                             ifelse(df$MyPrice > df$Median, 3, 4)))
df$NewPrice[is.na(df$MyPrice)] <- 4
df
#  Median MyPrice NewPrice
#1     10       0        1
#2     20      18        4
#3     20      20        2
#4     30      35        3
#5     15      NA        4

df给出第一个参数,如果
MyPrice==0&MyPrice
,那么第二行的Median:
20
和MyPrice:
18
也应该是
4
。下面是一个工作嵌套的ifelse语句,后面是NA处理程序

df <- as.data.frame(matrix(c(10,0,20,18,20,20,30,35,15,NA), byrow = T, ncol = 2))
colnames(df) <- c("Median","MyPrice")

df$NewPrice <- ifelse(df$MyPrice == 0 & df$MyPrice < df$Median, 1, 
                      ifelse(df$MyPrice == df$Median, 2, 
                             ifelse(df$MyPrice > df$Median, 3, 4)))
df$NewPrice[is.na(df$MyPrice)] <- 4
df
#  Median MyPrice NewPrice
#1     10       0        1
#2     20      18        4
#3     20      20        2
#4     30      35        3
#5     15      NA        4

df用4中的所有值设置一个新变量,然后替换适用条件的情况,怎么样?
简单、直接且易于阅读:-)

#(以@Evans Friedland为例)
df用4中的所有值设置一个新变量,然后替换适用条件的情况,怎么样? 简单、直接且易于阅读:-)

#(以@Evans Friedland为例)
df如果您的数据结构不是数据帧,这是正常的,您可以使用
[
运算符访问数据帧中的特定元素。请尝试以下操作:
my_data$MyPrice[my_data$MyPrice[
运算符访问数据帧中的特定元素。请尝试以下操作:
my_data$MyPrice[my_data$MyPrice