如何在R中匹配数据表中的字符串/字符变量,然后打印到另一列?

gzszwxb4  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(103)

我有一个数据表,其中一列包含一组特定的基因,另一列包含另一组显著基因,两者都是字符变量,如何找到这些基因的重叠并打印到另一列?
示例:
a〈-c("苹果香蕉甜瓜梨"、"梨猕猴桃菠萝"、"鳄梨酸橙猕猴桃苹果"、"酸橙菠萝香蕉甜瓜")b〈-c("等等等等香蕉梨"、"等等梨等等等等猕猴桃"、"等等等等苹果"、"酸橙等等等等")df〈-data. frame(a,b)
我想返回的是c的df $new_column('香蕉梨','梨猕猴桃','苹果','酸橙')
我试过:
df $new_column〈-df $a [df $a % in % df $b],但我收到错误消息
$<-.data.frame*tmp*,新列,值=字符(0))中的错误:替换为0行,数据为4行

qjp7pelc

qjp7pelc1#

这些字符串必须首先被分离成单词,然后我们可以对这些集合对使用intersect()。
以R为底数的话可能是这样的:

df <- data.frame(a,b)
# split strings and find intersections, paste back together
df$new_column <- mapply(\(a,b) paste(intersect(a,b), collapse = " ") ,
                        strsplit(df$a, " ",),
                        strsplit(df$b, " ",))
df
#>                             a                          b  new_column
#> 1    apple banana melon pear  blah blah blah banana pear banana pear
#> 2         pear kiwi pineapple   blah pear blah blah kiwi   pear kiwi
#> 3     avocado lime kiwi apple       blah blah blah apple       apple
#> 4 lime pineapple banana melon        lime blah blah blah        lime

# all values are just plain strings:
str(df)
#> 'data.frame':    4 obs. of  3 variables:
#>  $ a         : chr  "apple banana melon pear " "pear kiwi pineapple" "avocado lime kiwi apple" "lime pineapple banana melon"
#>  $ b         : chr  "blah blah blah banana pear" "blah pear blah blah kiwi" "blah blah blah apple" "lime blah blah blah"
#>  $ new_column: chr  "banana pear" "pear kiwi" "apple" "lime"

或者:

library(dplyr, warn.conflicts = F)
library(stringr)
library(purrr)

# with Tidyverse and list columns:
df_lc <- df %>% mutate(across(c(a,b), ~ str_split(.x, " "))) %>% 
  mutate(new_col = map2(a,b, ~ intersect(.x,.y)))

# now we have list columns:
df_lc["new_col"]
#>        new_col
#> 1 banana, pear
#> 2   pear, kiwi
#> 3        apple
#> 4         lime

# when printing a tibble it's bit more evident:
as_tibble(df_lc)
#> # A tibble: 4 × 4
#>   a         b         new_column  new_col  
#>   <list>    <list>    <chr>       <list>   
#> 1 <chr [5]> <chr [5]> banana pear <chr [2]>
#> 2 <chr [3]> <chr [5]> pear kiwi   <chr [2]>
#> 3 <chr [4]> <chr [4]> apple       <chr [1]>
#> 4 <chr [4]> <chr [4]> lime        <chr [1]>

str(df_lc)
#> 'data.frame':    4 obs. of  4 variables:
#>  $ a         :List of 4
#>   ..$ : chr  "apple" "banana" "melon" "pear" ...
#>   ..$ : chr  "pear" "kiwi" "pineapple"
#>   ..$ : chr  "avocado" "lime" "kiwi" "apple"
#>   ..$ : chr  "lime" "pineapple" "banana" "melon"
#>  $ b         :List of 4
#>   ..$ : chr  "blah" "blah" "blah" "banana" ...
#>   ..$ : chr  "blah" "pear" "blah" "blah" ...
#>   ..$ : chr  "blah" "blah" "blah" "apple"
#>   ..$ : chr  "lime" "blah" "blah" "blah"
#>  $ new_column: chr  "banana pear" "pear kiwi" "apple" "lime"
#>  $ new_col   :List of 4
#>   ..$ : chr  "banana" "pear"
#>   ..$ : chr  "pear" "kiwi"
#>   ..$ : chr "apple"
#>   ..$ : chr "lime"

输入:

a <- c('apple banana melon pear ', 'pear kiwi pineapple', 'avocado lime kiwi apple', 'lime pineapple banana melon')
b <- c('blah blah blah banana pear', 'blah pear blah blah kiwi', 'blah blah blah apple', 'lime blah blah blah')

创建于2023年1月20日,使用reprex v2.0.2

相关问题