简体   繁体   English

根据 R 中的另一列更改一列中的值

[英]Changing values in one column based on another in R

So I am using R and trying to change values in a data frame in one column by comparing two columns together.所以我正在使用 R 并尝试通过比较两列来更改一列中数据框中的值。 I have something like我有类似的东西

Median   MyPrice
10       0
20       18
20       20
30       35
15       NA

And I would like to say something like我想说类似的话

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

To come up with想出

Median   MyPrice
10       1
20       1
20       2
30       3
15       4

But there is always an error.但总是有错误。 I have also tried something like我也尝试过类似的东西

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}
  }

The for loop runs but it changes all values in MyPrice to 4. I also tried the ifelse() function but it seemed to have an issue taking that many arguments at once. for 循环运行,但它将 MyPrice 中的所有值更改为 4。我也尝试了 ifelse() 函数,但它似乎有一个问题,一次接受这么多参数。

I would also not be opposed to a new column being added to the end of the data frame if a solution like that is easier.如果这样的解决方案更容易,我也不反对将新列添加到数据框的末尾。

Given your first argument that if MyPrice == 0 & MyPrice < Median , your 2nd row where Median: 20 and MyPrice: 18 should also be 4 .鉴于您的第一个论点,如果MyPrice == 0 & MyPrice < Median ,则您的第二行 Median: 20和 MyPrice: 18也应该是4 Here is a working nested ifelse statement with an NA handler after.这是一个带有 NA 处理程序的工作嵌套 ifelse 语句。

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

You don't necessarily have to use a for loop.您不一定必须使用for循环。 Start by setting every comparison to 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

What about setting a new variable with all values in 4 and then, replace those cases where your conditions apply?使用 4 中的所有值设置一个新变量,然后替换您的条件适用的那些情况怎么样? Simple, straight forward and easy to read :-)简单、直接且易于阅读:-)

#(Following the example from @Evans Friedland) 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") #(按照@Evans Friedland 的例子) 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 <- mutate(df, myNewPrice = 4) #set my new price to 4, then edit by following your conditions df$myNewPrice<- replace (df$myNewPrice, df$MyPrice == 0 & df$MyPrice < df$Median, 1) df$myNewPrice<- replace (df$myNewPrice, df$MyPrice == df$Median , 2) df$myNewPrice<- replace (df$myNewPrice, df$MyPrice > df$Median , 3) df <- mutate(df, myNewPrice = 4) #将我的新价格设置为 4,然后按照您的条件进行编辑 df$myNewPrice<-replace (df$myNewPrice, df$MyPrice == 0 & df$MyPrice < df$Median , 1) df$myNewPrice<- 替换 (df$myNewPrice, df$MyPrice == df$Median , 2) df$myNewPrice<- 替换 (df$myNewPrice, df$MyPrice > df$Median , 3)

df$myNewPrice <- as.numeric (df$myNewPrice) #might, might not be needed. df$myNewPrice <- as.numeric (df$myNewPrice) #might,可能不需要。

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

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