首页 > 解决方案 > 如何在 R 中自动化这个简单的条件列操作?

问题描述

我有一个如下所示的数据框:

tibble(term = c(
  rep("a:b", 2),
  rep("b:a", 2),
  rep("c:d", 2),
  rep("d:c", 2),
  rep("g:h", 2),
  rep("h:g", 2)
)) 

我想在此数据框中添加一个额外的列,该列对于具有相同字符但反转并由“:”分隔的任何对具有相同的值(即 a:b 和 b:a 将以相同的方式编码; 与 c:d 和 d:c 以及所有其他对类似)。

我想到了以下内容:

%>%
  mutate(term_adjusted = case_when(grepl("a:b|b:a", term) ~ "a:b"))

但是我的数据集中有大量这样的对,并且想要一种自动化的方法,因此我的问题是:

我怎样才能自动执行此操作而不必分别为每一对进行硬编码?

谢谢!

标签: rconditional-statementstidyversedplyrgrepl

解决方案


怎么样:

libary(dplyr)

your_data %>%
  mutate(term_adjusted = term %>%
                           strsplit(":") %>%
                           purrr::map_chr(~ .x %>%
                                           sort() %>%
                                           paste(collapse = ":")))

基础 R 选项

your_data$term_adjusted <- your_data$term |>
                             strsplit(":") |>
                             lapply(sort) |>
                             lapply(paste, collapse = ":") |>
                             unlist()

要么返回:

# A tibble: 12 x 2
   term  term_adjusted
   <chr> <chr>
 1 a:b   a:b
 2 a:b   a:b
 3 b:a   a:b
 4 b:a   a:b
 5 c:d   c:d
 6 c:d   c:d
 7 d:c   c:d
 8 d:c   c:d
 9 g:h   g:h
10 g:h   g:h
11 h:g   g:h
12 h:g   g:h

推荐阅读