gpt4 book ai didi

r - 如果第三列中的值为正数或负数,则翻转/切换两列中的值

转载 作者:行者123 更新时间:2023-12-05 09:27:18 25 4
gpt4 key购买 nike

如果第三列中的值为负数,我想交换/切换两列中的值。

id <- c("1", "2", "3", "4", "5")
effect <- c("A", "C", "G", "A", "G")
non_effect <- c("C", "A", "T", "C", "C")
variable1 <- c("X", "X", "X", "X", "X")
variable2 <- c("Y", "Y", "Y", "Y", "Y")
value <-c("0.2", "-0.1", "-0.3", "0.5", "0.8")

df <- data.frame(id,effect,non_effect,variable1,variable2,value)

我会把它作为一个 data.frame

id effect non_effect variable1 variable2 value
1 A C X Y 0.2
2 C A X Y -0.1
3 G T X Y -0.3
4 A C X Y 0.5
5 G C X Y 0.8

我想要的是:

id effect non_effect variable1 variable2 value
1 A C X Y 0.2
2 A C X Y 0.1
3 T G X Y 0.3
4 A C X Y 0.5
5 G C X Y 0.8

最佳答案

df %>%
transform(effect = ifelse(value < 0, non_effect, effect),
non_effect = ifelse(value < 0, effect, non_effect),
value = abs(as.numeric(value)))

# A tibble: 5 x 6
id effect non_effect variable1 variable2 value
<chr> <chr> <chr> <chr> <chr> <dbl>
1 1 A C X Y 0.2
2 2 A C X Y 0.1
3 3 T G X Y 0.3
4 4 A C X Y 0.5
5 5 G C X Y 0.8

关于r - 如果第三列中的值为正数或负数,则翻转/切换两列中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72645188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com