Reputation: 939
From my tibble test
, I would like to get result
:
test <- tribble(
~colA, ~regsiege, ~nbeta_reg52, ~nbeta_reg53, ~nbeta_reg75,
"a", "52", 0,1,3,
"b", "53", 0,1,2,
"c", "75", 3,2,1
)
# expected tibble
result <- tribble(
~colA, ~regsiege, ~nbeta_reg52, ~nbeta_reg53, ~nbeta_reg75,
"a", "75", 0,1,3,
"b", "53", 0,1,2,
"c", "53", 3,2,1
)
I tried something like this with this condition :
for maregion in c("52","53","75"){
test2 <- test %>%
mutate(
regsiege=if_else(regsiege==maregion & !!sym(paste0("nbeta_reg",maregion))==0,
...)
)
}
EDIT :
I would like to replace the values in regsiege
column based on this rule:
if regsiege = "XX" and nbeta_regXX == 0 then regsiege == "XX" ie the suffix of the column in which nbeta_regXX is the maximum
Upvotes: 0
Views: 31
Reputation: 160447
Here's a pipeline that implements what you say, though not what you have in result
:
library(dplyr)
library(tidyr) # pivot_longer, pivot_wider
test %>%
pivot_longer(
starts_with("nbeta"), names_pattern = "(.*[^0-9])([0-9]+)",
names_to = c("nbeta_reg", "num")
) %>%
group_by(colA) %>%
mutate(
regsiege = if (any(regsiege == num & value == 0)) num[which.max(value)] else regsiege,
nbeta_reg = paste0(nbeta_reg, num)
) %>%
select(-num) %>%
ungroup() %>%
pivot_wider(colA:regsiege, names_from = "nbeta_reg", values_from = "value")
# # A tibble: 3 x 5
# colA regsiege nbeta_reg52 nbeta_reg53 nbeta_reg75
# <chr> <chr> <dbl> <dbl> <dbl>
# 1 a 75 0 1 3
# 2 b 53 0 1 2
# 3 c 75 3 2 1
The reason it is different is that your rule says that if nbeta_regXX == 0
, but in the third row, nbeta_reg75
is not 0, so regsiege
should be unchanged.
Upvotes: 1