使用dplyr有条件地将列中的值替换为另一列中的值

编程入门 行业动态 更新时间:2024-10-23 09:38:49
本文介绍了使用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有条件地将列中的值替换为另一列中的值

本文发布于:2023-10-23 05:58:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有条件   dplyr   将列中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!