简体   繁体   English

将跨列的选定值合并为一列

[英]Combine selected values across columns into one column

I have a dataframe df where:我有一个 dataframe df ,其中:

 a  b  c  d
 4  K  12 6
 6  L  K  P
 7  3  P  1
 0  L  90 K
 1  P  4  5
 0  K  17 23

How do I combine the two columns b , c , and d into a new column named x such that it only keeps the letters K , L , and P to look like this:如何将两列bcd合并到一个名为x的新列中,这样它只保留字母KLP看起来像这样:

 a  x  
 4  K  
 6  L, K, P
 7  P
 0  L, K 
 1  P
 0  K

We can use unite with separate_rows .我们可以将uniteseparate_rows一起使用。 unite the columns 'b', 'c', 'd' to a single column 'x', then split the column at the default delimiter ( _ ) with separate_rows while creating a unique row id with row_number , then filter the rows where we have 'K', 'L', 'P' elements, grouped by 'rn', summarise by paste ing the unique elements in 'x'将列 'b'、'c'、'd' unite为单个列 'x',然后在默认分隔符 ( _ ) 处使用separate_rows的行拆分列,同时使用row_number创建唯一的行 ID,然后filter我们所在的行有 'K', 'L', 'P' 元素,按 'rn' 分组,通过paste 'x' 中的unique元素进行summarise

library(dplyr)
library(tidyr)
df %>%
   unite(x, b, c, d) %>%
   mutate(rn = row_number()) %>%
   separate_rows(x) %>%
   filter(x %in% c("K", "L", "P")) %>%
   group_by(rn) %>%
   summarise(a = first(a), x = toString(unique(x))) %>%
   select(-rn)
# A tibble: 6 x 2
#      a x      
#  <int> <chr>  
#1     4 K      
#2     6 L, K, P
#3     7 P      
#4     0 L, K   
#5     1 P      
#6     0 K     

Or another option is pivot_longer或者另一个选项是pivot_longer

df %>% 
   mutate(rn = row_number()) %>% 
   pivot_longer(cols = b:d, values_to = 'x') %>% 
   filter( x %in% c("K", "L", "P")) %>% 
   group_by(rn) %>% 
   summarise(a = first(a), x = toString(unique(x))) %>%
   select(-rn)

data数据

df <- structure(list(a = c(4L, 6L, 7L, 0L, 1L, 0L), b = c("K", "L", 
"3", "L", "P", "K"), c = c("12", "K", "P", "90", "4", "17"), 
    d = c("6", "P", "1", "K", "5", "23")), class = "data.frame", 
    row.names = c(NA, 
-6L))

Another solution would be if you want to exclude all numbers如果您想排除所有数字,另一种解决方案是

df %>% 
  mutate(x = purrr::pmap_chr(list(b,c,d),
                             ~ paste0(c(..1, ..2, ..3) %>% 
                                        .[!grepl("^[0-9]{1,}$", .)], 
                                      collapse = ", ")))

or if you only want to keep K, L and P或者如果你只想保留 K、L 和 P

df %>% 
  mutate(x = purrr::pmap_chr(list(b,c,d),
                             ~ paste0(c(..1, ..2, ..3) %>% 
                                        .[. %in% c("K","L","P")], 
                                      collapse = ", ")))

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

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