如何使用两列进行条件交换?

时间:2018-06-29 07:40:10

标签: r dplyr data-manipulation

如果BLOOD_PRESSURE_SYSTOLIC <= BLOOD_PRESSURE_DIASTOLIC,我想交换2列的值。

数据:

df <- data.frame(ID = 1:5, BLOOD_PRESSURE_SYSTOLIC = c(35, 66, NA, 104, 139), BLOOD_PRESSURE_DIASTOLIC = c(98, 46, 160, 159, NA))
# ID BLOOD_PRESSURE_SYSTOLIC BLOOD_PRESSURE_DIASTOLIC
# 1  1                      35                       98
# 2  2                      66                       46
# 3  3                      NA                      160
# 4  4                     104                      159
# 5  5                     139                       NA

所需数据:

# ID BLOOD_PRESSURE_SYSTOLIC BLOOD_PRESSURE_DIASTOLIC
# 1  1                      98                       35
# 2  2                      66                       46
# 3  3                      NA                      160
# 4  4                     159                      104
# 5  5                     139                       NA

代码:我想出了以下代码,但是它没有输出我想要的结果(尤其是在处理缺失值时),并且它不是真正的可读性。 / p>

library(dplyr)
df %>%
      mutate(BLOOD_PRESSURE_SYSTOLIC2  = if_else(BLOOD_PRESSURE_SYSTOLIC <= BLOOD_PRESSURE_DIASTOLIC, BLOOD_PRESSURE_DIASTOLIC, BLOOD_PRESSURE_SYSTOLIC),
             BLOOD_PRESSURE_DIASTOLIC2 = if_else(BLOOD_PRESSURE_SYSTOLIC <= BLOOD_PRESSURE_DIASTOLIC, BLOOD_PRESSURE_SYSTOLIC, BLOOD_PRESSURE_DIASTOLIC)) %>%
      select(ID, BLOOD_PRESSURE_SYSTOLIC = BLOOD_PRESSURE_SYSTOLIC2, BLOOD_PRESSURE_DIASTOLIC = BLOOD_PRESSURE_DIASTOLIC2)

2 个答案:

答案 0 :(得分:3)

以R为底

i <- which(df$BLOOD_PRESSURE_SYSTOLIC <= df$BLOOD_PRESSURE_DIASTOLIC)

v1 <- df$BLOOD_PRESSURE_DIASTOLIC[i]
v2 <- df$BLOOD_PRESSURE_SYSTOLIC[i]

df$BLOOD_PRESSURE_SYSTOLIC[i] <- v1
df$BLOOD_PRESSURE_DIASTOLIC[i] <- v2

结果:

> df
  ID BLOOD_PRESSURE_SYSTOLIC BLOOD_PRESSURE_DIASTOLIC
1  1                      98                       35
2  2                      66                       46
3  3                      NA                      160
4  4                     159                      104
5  5                     139                       NA

答案 1 :(得分:2)

您可以添加一个NA条件:

library(dplyr)

df <- data.frame(ID = 1:5, BPS = c(35, 66, NA, 104, 139), BPD = c(98, 46, 160, 159, NA))

df %>%
  mutate(x = if_else(!is.na(BPD) & BPS <= BPD, BPD, BPS),
         y = if_else(!is.na(BPS) & BPS <= BPD, BPS, BPD)) %>%
  select(ID, BPS = x, BPD = y)

输出:

> df
  ID BPS BPD
1  1  98  35
2  2  66  46
3  3  NA 160
4  4 159 104
5  5 139  NA