[英]Replace values in one column based on condition using its name as a string
If I have a dataframe like this: 如果我有一个像这样的数据框:
df <- data.frame(c1=1:6, c2=2:7)
I can happily replace values in c2
that are larger then 4
doing 我可以很高兴地替换
c2
中大于4
值
df$c2[df$c2 > 4] <- 10
yielding the desired output 产生所需的输出
c1 c2
1 1 2
2 2 3
3 3 4
4 4 10
5 5 10
6 6 10
However, I want to select the column by its name using a string, in this case "c2"
, as the column selection should not be hard-coded but it is context dependent. 但是,我想使用字符串按名称选择列,在本例中为
"c2"
,因为列选择不应该是硬编码的,但它取决于上下文。
The best I could come up with is 我能想到的最好的是
df[,c('c2')][df[,c('c2')] > 4] <- 1000
yielding 屈服
c1 c2
1 1 2
2 2 3
3 3 4
4 4 1000
5 5 1000
6 6 1000
It works, but I find it rather ugly. 它可以工作,但是我觉得它很丑。 Is there a better way of doing the same thing?
有没有更好的方法来做同样的事情?
Maybe using replace
也许使用
replace
df['c2'] <- replace(df['c2'], df['c2'] > 4, 100)
df
# c1 c2
#1 1 2
#2 2 3
#3 3 4
#4 4 100
#5 5 100
#6 6 100
Or something similar as your attempt 或类似的尝试
df['c2'][df['c2'] > 4] <- 100
If one is open to packages, we can use purrr
's modify_at
or dplyr
's mutate_at
如果对包开放,我们可以使用
purrr
的modify_at
或dplyr
的mutate_at
purrr::modify_at(df,"c2",
function(x)
ifelse(x>4,100,x))
With dplyr
: 与
dplyr
:
mutate_at(df,"c2",
function(x)
ifelse(x>4,100,x))
Using transform
and ifelse
使用
transform
和ifelse
transform(df, c2 = ifelse(c2 > 4, 100, c2))
# c1 c2
#1 1 2
#2 2 3
#3 3 4
#4 4 100
#5 5 100
#6 6 100
If we need to pass a string, one option with dplyr
, would be convert to symbol and evaluate 如果需要传递字符串,则使用
dplyr
一个选项将转换为symbol并求值
library(dplyr)
df %>%
mutate(!! "c2" := replace(!! rlang::sym("c2"),
!! rlang::sym("c2") > 4, 100))
# c1 c2
#1 1 2
#2 2 3
#3 3 4
#4 4 100
#5 5 100
#6 6 100
df[df$c2 > 4, 'c2'] <- 10
# or
df$c2 <- with(df, replace(c2, c2 > 4, 10))
Using package data.table
you could do: 使用包
data.table
可以执行以下操作:
library(data.table)
setDT(df)
df[c2 > 4, c2 := 10]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.