简体   繁体   English

根据条件使用其名称作为字符串替换一列中的值

[英]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 如果对包开放,我们可以使用purrrmodify_atdplyrmutate_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 使用transformifelse

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.

相关问题 根据列条件用 NA 替换值 - Replace values with NAs based on a column condition 如何根据R中的条件替换列值 - How to replace column values based on a condition in R 使用字符串值选择一个数据框并更改其列名 - Select a data frame using string values and change its column name 使用另一数据框的一行中的值替换一个数据框的一列中的所有值(按行名和列名匹配) - Replace all values in a column of one dataframe using values in a row of another dataframe (matching by row name and column name) R 根据一个数据集中的列信息/条件将行值替换为其他行? - R Replace row values to the other rows based on/condition on column information in one data set? 是否有 function 可以根据另一列的条件替换列中的特定值? - Is there a function to replace specific values in a column based on condition from another column? 根据另一列条件替换一个列向量 - replace one column vector based on another column condition 根据R中另一列的部分字符串值替换一列中的空白值 - Replace blank values in one column based on partial string values of another in R 如何根据该列的值中是否存在字符串来替换该列中某些索引处的值(使用 dplyr 并重复无循环)? - How to replace values at certain indices in a column based on presence of a string in values of that column (using dplyr and repeatedly with no loop)? 替换另一列上一列条件中的值:R - Replace values in one column condition on another column: R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM