英文:
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 <- 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_'))
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 %>%
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
答案2
得分: 0
不需要翻译的部分:
library(stringr)
library(dplyr, warn=FALSE)
library(tidyr)
set.seed(123)
score <- sample(1:100, 20, replace = TRUE)
df <- 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 <- sample(1:100, 20, replace = TRUE)
df <- 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)
#> # A tibble: 20 × 6
#> score G2_20_40 G4_60_80 G3_40_60 G1_0_20 G5_80_100
#> <int> <int> <int> <int> <int> <int>
#> 1 31 31 NA NA NA NA
#> 2 79 NA 79 NA NA NA
#> 3 51 NA NA 51 NA NA
#> 4 14 NA NA NA 14 NA
#> 5 67 NA 67 NA NA NA
#> 6 42 NA NA 42 NA NA
#> 7 50 NA NA 50 NA NA
#> 8 43 NA NA 43 NA NA
#> 9 14 NA NA NA 14 NA
#> 10 25 25 NA NA NA NA
#> 11 90 NA NA NA NA 90
#> 12 91 NA NA NA NA 91
#> 13 69 NA 69 NA NA NA
#> 14 91 NA NA NA NA 91
#> 15 57 NA NA 57 NA NA
#> 16 92 NA NA NA NA 92
#> 17 9 NA NA NA 9 NA
#> 18 93 NA NA NA NA 93
#> 19 99 NA NA NA NA 99
#> 20 72 NA 72 NA NA NA
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论