使用ifelse在列之间进行变异

huangapple go评论73阅读模式
英文:

mutate across columns with ifelse

问题

这是我的数据集:

library(stringr)
library(dplyr)
library(fastDummies)

score <- sample(1:100, 20, replace=TRUE)
df <- data.frame(score)

df <- df %>%
   mutate(grp = cut(score, breaks = c(-Inf, seq(0, 100, by = 20), Inf)), 
      grp = str_c("G", as.integer(droplevels(grp)), '_', 
      str_replace(grp, '\\((\\d+),(\\d+)\\]', 
     '\_\'))) %>%
   dummy_cols("grp", remove_selected_columns = TRUE) %>%
   rename_with(~ str_remove(.x, 'grp_'), starts_with('grp_'))

我想要变异以字母"G"开头的列,比如G1_0_20、G2_20_40等。

如果以字母G开头的列(比如G1_0_20、G2_20_40等)的值为1,那么它的值应该与列"Score"匹配,否则为NA。

我无法完全弄清如何使用带有ifelse语句的mutate。

我将不胜感激所有的帮助!谢谢!

英文:

I'm trying to mutate values across multiple columns to values from another column.

This is my dataset:

library(stringr)
library(dplyr)
library(fastDummies)

score &lt;- sample(1:100,20,replace=TRUE)
df &lt;- data.frame(score)

df &lt;- df %&gt;%
   mutate(grp = cut(score, breaks = c(-Inf, seq(0, 100, by = 20), Inf)), 
      grp = str_c(&quot;G&quot;, as.integer(droplevels(grp)), &#39;_&#39;, 
      str_replace(grp, &#39;\\((\\d+),(\\d+)\\]&#39;, 
     &#39;\_\&#39;))) %&gt;% 
   dummy_cols(&quot;grp&quot;, remove_selected_columns = TRUE) %&gt;% 
   rename_with(~ str_remove(.x, &#39;grp_&#39;), starts_with(&#39;grp_&#39;))

I want to mutate columns that start with the letter "G", so G1_0_20, G2_20_40, etc.

If columns that start with G (G1_0_20, G2_20_40,etc) has value of 1, then its value should match column "Score", otherwise NA.

I can't quite figure out how to use mutate across with ifelse statement.

I would appreciate all the help there is! Thanks!!!

答案1

得分: 1

我认为这就是:

df %>%
  mutate(across(starts_with("G"), ~ifelse(. == 1, score, NA)))
score G1_0_20 G2_20_40 G3_40_60 G4_60_80 G5_80_100
1     52      NA       NA       52       NA        NA
2     90      NA       NA       NA       NA        90
3     73      NA       NA       NA       73        NA
4     11      11       NA       NA       NA        NA
5     16      16       NA       NA       NA        NA
6     47      NA       NA       47       NA        NA
7     42      NA       NA       42       NA        NA
8     62      NA       NA       NA       62        NA
9     64      NA       NA       NA       64        NA
10    25      NA       25       NA       NA        NA
11    47      NA       NA       47       NA        NA
12    63      NA       NA       NA       63        NA
13    96      NA       NA       NA       NA        96
14    95      NA       NA       NA       NA        95
15     3       3       NA       NA       NA        NA
16    25      NA       25       NA       NA        NA
17    78      NA       NA       NA       78        NA
18    10      10       NA       NA       NA        NA
19    51      NA       NA       51       NA        NA
20    12      12       NA       NA       NA        NA

希望这可以帮助你!

英文:

I think this is it:

df %&gt;%
  mutate(across(starts_with(&quot;G&quot;), ~ifelse(. == 1, score, NA)))
   score G1_0_20 G2_20_40 G3_40_60 G4_60_80 G5_80_100
1     52      NA       NA       52       NA        NA
2     90      NA       NA       NA       NA        90
3     73      NA       NA       NA       73        NA
4     11      11       NA       NA       NA        NA
5     16      16       NA       NA       NA        NA
6     47      NA       NA       47       NA        NA
7     42      NA       NA       42       NA        NA
8     62      NA       NA       NA       62        NA
9     64      NA       NA       NA       64        NA
10    25      NA       25       NA       NA        NA
11    47      NA       NA       47       NA        NA
12    63      NA       NA       NA       63        NA
13    96      NA       NA       NA       NA        96
14    95      NA       NA       NA       NA        95
15     3       3       NA       NA       NA        NA
16    25      NA       25       NA       NA        NA
17    78      NA       NA       NA       78        NA
18    10      10       NA       NA       NA        NA
19    51      NA       NA       51       NA        NA
20    12      12       NA       NA       NA        NA

答案2

得分: 0

不需要翻译的部分:

library(stringr)
library(dplyr, warn=FALSE)
library(tidyr)

set.seed(123)

score &lt;- sample(1:100, 20, replace = TRUE)
df &lt;- data.frame(score)

翻译的部分:

df %>%
  mutate(rowid = row_number()) |>
  mutate(
    grp = cut(score, breaks = c(-Inf, seq(0, 100, by = 20), Inf)),
    grp = str_c(
      "G", as.integer(droplevels(grp)), "_",
      str_replace(
        grp, "\\((\\d+),(\\d+)\\]",
        "\_\"
      )
    ),
    score1 = score
  ) |>
  tidyr::pivot_wider(names_from = grp, values_from = score1) |>
  select(-rowid)

在代码的翻译部分,我保留了R语言中的函数名和操作符不进行翻译。

英文:

Instead of creating dummies and replacing using an ifelse one option would be to use tidyr::pivot_wider:

library(stringr)
library(dplyr, warn=FALSE)
library(tidyr)

set.seed(123)

score &lt;- sample(1:100, 20, replace = TRUE)
df &lt;- data.frame(score)

df %&gt;%
  mutate(rowid = row_number()) |&gt;
  mutate(
    grp = cut(score, breaks = c(-Inf, seq(0, 100, by = 20), Inf)),
    grp = str_c(
      &quot;G&quot;, as.integer(droplevels(grp)), &quot;_&quot;,
      str_replace(
        grp, &quot;\\((\\d+),(\\d+)\\]&quot;,
        &quot;\_\&quot;
      )
    ),
    score1 = score
  ) |&gt;
  tidyr::pivot_wider(names_from = grp, values_from = score1) |&gt;
  select(-rowid)
#&gt; # A tibble: 20 &#215; 6
#&gt;    score G2_20_40 G4_60_80 G3_40_60 G1_0_20 G5_80_100
#&gt;    &lt;int&gt;    &lt;int&gt;    &lt;int&gt;    &lt;int&gt;   &lt;int&gt;     &lt;int&gt;
#&gt;  1    31       31       NA       NA      NA        NA
#&gt;  2    79       NA       79       NA      NA        NA
#&gt;  3    51       NA       NA       51      NA        NA
#&gt;  4    14       NA       NA       NA      14        NA
#&gt;  5    67       NA       67       NA      NA        NA
#&gt;  6    42       NA       NA       42      NA        NA
#&gt;  7    50       NA       NA       50      NA        NA
#&gt;  8    43       NA       NA       43      NA        NA
#&gt;  9    14       NA       NA       NA      14        NA
#&gt; 10    25       25       NA       NA      NA        NA
#&gt; 11    90       NA       NA       NA      NA        90
#&gt; 12    91       NA       NA       NA      NA        91
#&gt; 13    69       NA       69       NA      NA        NA
#&gt; 14    91       NA       NA       NA      NA        91
#&gt; 15    57       NA       NA       57      NA        NA
#&gt; 16    92       NA       NA       NA      NA        92
#&gt; 17     9       NA       NA       NA       9        NA
#&gt; 18    93       NA       NA       NA      NA        93
#&gt; 19    99       NA       NA       NA      NA        99
#&gt; 20    72       NA       72       NA      NA        NA

huangapple
  • 本文由 发表于 2023年3月3日 23:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定