使用dplyr有条件地将列中的值替换为另一列中的值 [英] Conditionally replace the values in columns to value in another column using dplyr

查看:397
本文介绍了使用dplyr有条件地将列中的值替换为另一列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常努力地找到答案,如果出现重复,我深表歉意。

I tried really hard to find an answer to this and I apologize if it's a duplicate.

我将输入一些虚拟数据来解释我的问题。

I'll make some dummy data to explain my question.

tibble(a=c(0.1, 0.2, 0.3), sample1 = c(0, 1, 1), sample2 = c(1, 1, 0))

# A tibble: 3 x 3
      a sample1 sample2
 <dbl>   <dbl>   <dbl>
1   0.1       0       1
2   0.2       1       1
3   0.3       1       0

如何有条件地更改 sample1 sample2 列中的值,以便如果它们等于1,则采用 a

How to I conditionally change the values in columns sample1 and sample2 so that if they are equal to one, they take on the value of a.

产生的小标题应如下所示:

The resulting tibble should look like this:

# A tibble: 3 x 3
      a sample1 sample2
 <dbl>   <dbl>   <dbl>
1   0.1       0     0.1
2   0.2     0.2     0.2
3   0.3     0.3       0

理想情况下,我不想为每个单独的示例列(我有> 100个示例列)执行此操作,因此一种遍历列的方法会更好(尽管我知道循环是魔鬼)。

Ideally I don't want to do this for each individual sample column (I have >100 sample columns), so a way to loop over columns would be better (although I know loops are the devil).

谢谢您的帮助!

推荐答案

您可以使用 mutate_at ifelse

df %>% mutate_at(vars(starts_with('sample')), funs(ifelse(. == 1, a, .)))

# A tibble: 3 x 3
#      a sample1 sample2
#  <dbl>   <dbl>   <dbl>
#1   0.1     0.0     0.1
#2   0.2     0.2     0.2
#3   0.3     0.3     0.0

vars(starts_with('sample'))匹配以 sample mutate_at 将函数 funs(ifelse(。== 1,a,。))应用于每一列; 代表此处的匹配列。

vars(starts_with('sample')) matches all columns that starts with sample and mutate_at applies the function funs(ifelse(. == 1, a, .)) to each column; . stands for the matched column here.

如果确保所有示例列仅包含 1 0 ,可以将其缩短为:

If you are sure all the samples columns contain only 1 and 0, it can be shortened as:

df %>% mutate_at(vars(starts_with('sample')), funs(. * a))

# A tibble: 3 x 3
#      a sample1 sample2
#  <dbl>   <dbl>   <dbl>
#1   0.1     0.0     0.1
#2   0.2     0.2     0.2
#3   0.3     0.3     0.0

这篇关于使用dplyr有条件地将列中的值替换为另一列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆