首页 > 解决方案 > 加入没有 NA 的字符串

问题描述

我想总结没有na的字符串。

它与 str_c 一起使用,如下例所示

代码

df <- tibble(x = c("ISSUE-1", "ISSUE-2", "ISSUE-3", "ISSUE-4"), ID = c("ID-1", "ID-1", "ID-2", "ID-2")) %>%
  group_by(ID) %>%
  summarise(IDissue = str_c(x, collapse = "; "))

输出

ID          issueID
ID-1    ISSUE-1; ISSUE-2
ID-2    ISSUE-3; ISSUE-4

但是,当 str_c 列中有 NA 时,会将完整输出转换为 NA,如文档中所述: https ://stringr.tidyverse.org/reference/str_c.html

与大多数其他 R 函数一样,缺失值具有“传染性”:每当缺失值与另一个字符串组合时,结果总是会缺失。使用 str_replace_na() 将 NA 转换为“NA” 代码

df <- tibble(x = c("ISSUE-1", "ISSUE-2", "ISSUE-3", NA), ID = c("ID-1", "ID-1", "ID-2", "ID-2")) %>%
  group_by(ID) %>%
  summarise(IDissue = str_c(x, collapse = "; "))

输出

ID    IDissue
ID-1    ISSUE-1; ISSUE-2
ID-2    NA

有没有办法获得输出?

ID    IDissue
ID-1    ISSUE-1; ISSUE-2
ID-2    ISSUE-3

标签: rstringr

解决方案


更简单的方法是filtergroup_by

library(stringr)
library(dplyr)
tibble(x = c("ISSUE-1", "ISSUE-2", "ISSUE-3", NA), 
     ID = c("ID-1", "ID-1", "ID-2", "ID-2")) %>%  
  filter(!is.na(x)) %>% 
  group_by(ID) %>%
  summarise(IDissue = str_c(x, collapse = "; "))

推荐阅读