通过匹配模式将 tibble 中的新列与其他列的值进行变异

问题描述 投票:0回答:2

我的小话:

df <- tibble(a = c(1,2,3,4,5,6,7,8,9,10),
                 b = c("", "", "1", "", "1", "3", "2", "", "5", ""),
                 c = c("", "", "", "", "x1", "x3", "x2", "", "x5", ""),
                 d = c("", "", "1", "", "1", "3", "2", "", "5", ""),
                 e = c("x1", "x4", "", "x5", "", "", "", "x2", "", "x2"))

# A tibble: 10 × 5
       a b     c     d     e    
   <dbl> <chr> <chr> <chr> <chr>
 1     1 ""    ""    ""    "x1" 
 2     2 ""    ""    ""    "x4" 
 3     3 "1"   ""    "1"   ""   
 4     4 ""    ""    ""    "x5" 
 5     5 "1"   "x1"  "1"   ""   
 6     6 "3"   "x3"  "3"   ""   
 7     7 "2"   "x2"  "2"   ""   
 8     8 ""    ""    ""    "x2" 
 9     9 "5"   "x5"  "5"   ""   
10    10 ""    ""    ""    "x2" 

期望的结果:

# A tibble: 10 × 6
       a b     c     d     e     f    
   <dbl> <chr> <chr> <chr> <chr> <chr>
 1     1 ""    ""    ""    "x1"  "x1" 
 2     2 ""    ""    ""    "x4"  "x4" 
 3     3 "1"   ""    "1"   ""    ""   
 4     4 ""    ""    ""    "x5"  "x5" 
 5     5 "1"   "x1"  "1"   ""    "x1" 
 6     6 "3"   "x3"  "3"   ""    "x3" 
 7     7 "2"   "x2"  "2"   ""    "x2" 
 8     8 ""    ""    ""    "x2"  "x2" 
 9     9 "5"   "x5"  "5"   ""    "x5" 
10    10 ""    ""    ""    "x2"  "x2" 

我想添加一个新列,其中包含包含“x”的行的值。我不想指定列。我只想查看任意数量的列,这些列可能会有所不同。

也许是这样:

df %>%
 find_value_with_x_per_row %>%
 put_that_value_in_new_column_per_row %>%
 if_you_dont_find_value_with_x_put_""_instead
r tibble mutate
2个回答
1
投票

这里我们检查每一列是否有 x。如果是这样,将它们组合起来并放入新的 f 列中。如果您有两列带有 x:

,这一点很重要
library(dplyr)
library(tidyr)

df %>% 
  mutate(across(a:e, ~case_when(grepl("x", .) ~.), .names = 'new_{col}')) %>%
  unite(f, starts_with('new'), na.rm = TRUE, sep = ' ')
      a b     c     d     e     f    
   <dbl> <chr> <chr> <chr> <chr> <chr>
 1     1 ""    ""    ""    "x1"  "x1" 
 2     2 ""    ""    ""    "x4"  "x4" 
 3     3 "1"   ""    "1"   ""    ""   
 4     4 ""    ""    ""    "x5"  "x5" 
 5     5 "1"   "x1"  "1"   ""    "x1" 
 6     6 "3"   "x3"  "3"   ""    "x3" 
 7     7 "2"   "x2"  "2"   ""    "x2" 
 8     8 ""    ""    ""    "x2"  "x2" 
 9     9 "5"   "x5"  "5"   ""    "x5" 
10    10 ""    ""    ""    "x2"  "x2" 

考虑这个例子:

df <- tibble(a = c(1,2,3,4,5,6,7,8,9,10),
             b = c("", "", "x1", "", "1", "3", "2", "", "5", ""),
             c = c("", "", "", "", "x1", "x3", "x2", "", "x5", ""),
             d = c("", "", "x1", "", "1", "3", "2", "", "5", ""),
             e = c("x1", "x4", "", "x5", "", "", "", "x2", "", "x2"))

# A tibble: 10 × 6
       a b     c     d     e     f    
   <dbl> <chr> <chr> <chr> <chr> <chr>
 1     1 ""    ""    ""    "x1"  x1   
 2     2 ""    ""    ""    "x4"  x4   
 3     3 "x1"  ""    "x1"  ""    x1   
 4     4 ""    ""    ""    "x5"  x5   
 5     5 "1"   "x1"  "1"   ""    x1   
 6     6 "3"   "x3"  "3"   ""    x3   
 7     7 "2"   "x2"  "2"   ""    x2   
 8     8 ""    ""    ""    "x2"  x2   
 9     9 "5"   "x5"  "5"   ""    x5   
10    10 ""    ""    ""    "x2"  x2  

library(dplyr)
library(tidyr)

df %>% 
  mutate(across(a:e, ~case_when(grepl("x", .) ~.), .names = 'new_{col}')) %>%
  unite(f, starts_with('new'), na.rm = TRUE, sep = ' ')


       a b     c     d     e     f    
   <dbl> <chr> <chr> <chr> <chr> <chr>
 1     1 ""    ""    ""    "x1"  x1   
 2     2 ""    ""    ""    "x4"  x4   
 3     3 "x1"  ""    "x1"  ""    x1 x1
 4     4 ""    ""    ""    "x5"  x5   
 5     5 "1"   "x1"  "1"   ""    x1   
 6     6 "3"   "x3"  "3"   ""    x3   
 7     7 "2"   "x2"  "2"   ""    x2   
 8     8 ""    ""    ""    "x2"  x2   
 9     9 "5"   "x5"  "5"   ""    x5   
10    10 ""    ""    ""    "x2"  x2   

0
投票
dplyr::mutate(df, f = c_across(everything())[grepl("x", c_across(everything()))][1], .by = a)
© www.soinside.com 2019 - 2024. All rights reserved.