简体   繁体   English

R dplyr 变异:使用“或”逻辑从多个列变量创建一个变量

[英]R dplyr mutate: creating one variable from multiple column variables using "or" logic

I am trying to do something that I think is straightforward but I am having an issue with.我正在尝试做一些我认为很简单但我遇到问题的事情。

I have several medication-related column headings ( med_1 , med_2 , med_3 for example).我有几个与药物相关的列标题(例如med_1med_2med_3 )。 I want to combine them all into variable anymed using or logic, so that I can then use anymed to look at any medications reported across all medication related fields.我想将它们全部组合成变量anymed using or logic,以便我可以使用anymed查看所有药物相关领域报告的任何药物。 The fields are text-based.这些字段是基于文本的。

I am trying the following, for dataset FinalData.对于数据集 FinalData,我正在尝试以下操作。

FinalData <- FinalData %>% mutate(anymed = med_1 | med_2 | med_3)

I am receiving this error:我收到此错误:

*Error: Problem with `mutate()` column `anymed`.
ℹ `anymed = |...`.
x operations are possible only for numeric, logical or complex types*

Could someone help explain what code I should use instead?有人可以帮助解释我应该使用什么代码吗?

You want to use pivot_longer from tidyverse to get them all in the same column.您想使用pivot_longer中的tidyverse将它们全部放在同一列中。 I also dropped the column name (ie, col ), but you could remove that line if you want to know what column the medication came from.我还删除了列名(即col ),但如果您想知道药物来自哪个列,可以删除该行。 I'm unsure what your data looks like, so I just made a small example to show how to do it.我不确定你的数据是什么样的,所以我只是做了一个小例子来展示如何做。

library(tidyverse)

FinalData %>%
  pivot_longer(-ind, names_to = "col", values_to = "anymed") %>%
  select(-col)

Output Output

# A tibble: 6 × 2
    ind anymed
  <dbl> <chr> 
1     1 meda  
2     1 meda  
3     1 meda  
4     2 medb  
5     2 medb  
6     2 medb  

Data数据

FinalData <-
  structure(
    list(
      ind = c(1, 2),
      med_1 = c("meda", "medb"),
      med_2 = c("meda",
                "medb"),
      med_3 = c("meda", "medb")
    ),
    class = "data.frame",
    row.names = c(NA,-2L)
  )

Are you looking for this kind of solution:您是否正在寻找这种解决方案:

# data:
df <- tibble(med_1 = "A", med_2 = "B", med_3 = "C")

library(dplyr)
df %>% 
  mutate(any_med = paste(c(med_1, med_2, med_3), collapse = " | "))
  med_1 med_2 med_3 any_med  
  <chr> <chr> <chr> <chr>    
1 A     B     C     A | B | C

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM