如何为特定行 tidyverse 重新编码多个值
How to recode several values for a specific row tidyverse
我想找到一种方法来使用 tidyverse
更改特定行的多个列中的值。
例如,base
R
和 iris
数据集:
iris[iris$Sepal.Length == 5.1, c("Petal.Length",
"Petal.Width")] <- c(1.5,
0.5)
到目前为止,我用 tidyverse
找到了这个,但是如何仅更改 Sepal.Length == 5.1
所在行的这些值?我知道我可以 filter
我想要的行,然后更改值,但是如果我想像上面 base
示例中那样保留整个数据集怎么办?:
myiris <- iris %>%
mutate_at(c("Petal.Length","Petal.Width"),
funs(recode(., "1.4" = 1.5, "0.2" = 0.5)))
谢谢!
也许这会起作用,它不是最优雅的,但它对我有用:
library(tidyverse)
myiris <- iris %>%
mutate_at(c("Petal.Length","Petal.Width"),
funs(ifelse(Sepal.Length == 5.1, c(1.5, 0.5), c(Sepal.Length, Sepal.Width))))
我想找到一种方法来使用 tidyverse
更改特定行的多个列中的值。
例如,base
R
和 iris
数据集:
iris[iris$Sepal.Length == 5.1, c("Petal.Length",
"Petal.Width")] <- c(1.5,
0.5)
到目前为止,我用 tidyverse
找到了这个,但是如何仅更改 Sepal.Length == 5.1
所在行的这些值?我知道我可以 filter
我想要的行,然后更改值,但是如果我想像上面 base
示例中那样保留整个数据集怎么办?:
myiris <- iris %>%
mutate_at(c("Petal.Length","Petal.Width"),
funs(recode(., "1.4" = 1.5, "0.2" = 0.5)))
谢谢!
也许这会起作用,它不是最优雅的,但它对我有用:
library(tidyverse)
myiris <- iris %>%
mutate_at(c("Petal.Length","Petal.Width"),
funs(ifelse(Sepal.Length == 5.1, c(1.5, 0.5), c(Sepal.Length, Sepal.Width))))