尝试在数据框中用零替换一组数字

C. *_*oni 0 r

我有一个包含三列和很多行的数据框。其中一列包含 80803 和 9995 之类的数字。我想用 0 替换此列中的特定数字 80803 和 9995。

假设我的数据框名为 df。我尝试使用 gsub 函数,如 gsub(80803,0,df)。但是错误显示了哪些状态pattern has length > 1 and only the first element will be used

这是我的 df 的示例。只是有更多的行。

a <- c(85.42, 80.80, 78.56 , 70.40)
b <- c(110, 80803, 9995, 50)
c <- c(3, 4 , 7, 5)
df <- data.frame(a, b, c)
df
      a     b c
1 85.42   110 3
2 80.80 80803 4
3 78.56  9995 7
4 70.40    50 5
Run Code Online (Sandbox Code Playgroud)

这就是我希望我的 df 的样子。

df
      a     b c
1 85.42   110 3
2 80.80     0 4
3 78.56     0 7
4 70.40    50 5

Run Code Online (Sandbox Code Playgroud)

fab*_*bla 5

您可以在基础 R 中解决此问题。

 df$b[df$b == 80803|df$b == 9995] <- 0
Run Code Online (Sandbox Code Playgroud)