为什么我需要在执行map(str_match_all())之后将我的数据帧索引到map()?

时间:2017-09-13 23:52:21

标签: r dplyr tidyverse stringr purrr

我试图解析一个包含字符串的数据框,以提取最大值(以数字方式),并遇到一些麻烦。

如果我从这样的开头开始:

tester <- tibble("phyloP46way_primate" = c(".{9}", "0.055{1}0.064{3}", "0.225{1}", "0.271{1}", "-0.706{1}-0.708{1}0.248{3}0.298{3}"))

然后使用map()modify()来应用str_match_all()从每个字符向量中选择值,我会回复一个tibble(对于modify())观察(每个由str_match_all()的5个调用返回的字符矩阵列表)(或包含5个字符矩阵列表的1个列表(对于map())。

regex ≤- "(?:(?:-?\\d+\\.?\\d+?)|\\.)(?=(?:\\{\\d+\\}|;|$))"

> str(foo_tbl<- tester %>% modify(str_match_all, pattern = regex))
Classes 'tbl_df', 'tbl' and 'data.frame':    5 obs. of  1 variable:
 $ phyloP46way_primate:List of 5
  ..$ : chr [1, 1] "."
  ..$ : chr [1:2, 1] "0.055" "0.064"
  ..$ : chr [1, 1] "0.225"
  ..$ : chr [1, 1] "0.271"
  ..$ : chr [1:4, 1] "-0.706" "-0.708" "0.248" "0.298"

> str(foo_list<- tester %>% map(str_match_all, pattern = regex))
List of 1
 $ phyloP46way_primate:List of 5
  ..$ : chr [1, 1] "."
  ..$ : chr [1:2, 1] "0.055" "0.064"
  ..$ : chr [1, 1] "0.225"
  ..$ : chr [1, 1] "0.271"
  ..$ : chr [1:4, 1] "-0.706" "-0.708" "0.248" "0.298"

现在,我想要做的是将函数应用于每个“行”。但是当我尝试映射时,它似乎只是在一个向量中将它们连接在一起,只从整个批次中选择单个最大值,而不是一个/行:

> map(foo_tbl, function(x) list_to_max(x))
$phyloP46way_primate
$phyloP46way_primate[[1]]
[1] "0.298"

除非我做了一些奇怪的索引并映射到foo_tbl[[1]]而不是foo_tbl

map(foo_tbl[[1]], function(x) list_to_max(x)) %>% unlist()
[1] "."     "0.064" "0.225" "0.271" "0.298"

我认为我的list_to_max()必须做出意想不到的事情,因为这些行为符合我的预期:

> invisible(map(foo_tbl, function(x) print(paste0("x is: ", x))))
[1] "x is: ."                                              
[2] "x is: c(\"0.055\", \"0.064\")"                        
[3] "x is: 0.225"                                          
[4] "x is: 0.271"                                          
[5] "x is: c(\"-0.706\", \"-0.708\", \"0.248\", \"0.298\")"
> invisible(modify(foo_tbl, function(x) print(paste0("x is: ", x))))
[1] "x is: ."                                              
[2] "x is: c(\"0.055\", \"0.064\")"                        
[3] "x is: 0.225"                                          
[4] "x is: 0.271"                                          
[5] "x is: c(\"-0.706\", \"-0.708\", \"0.248\", \"0.298\")"

这是我的功能:

list_to_max <- function(character_vector) {
  numbers <- suppressWarnings(as.numeric(character_vector))
  if (all(is.na(numbers))) {
    return(".")
    } else {
      numbers %>% max(., na.rm = TRUE) %>% toString()
    }
}

1 个答案:

答案 0 :(得分:4)

toString会将所有内容强制转换为以逗号分隔的字符串,这不是很有用。这是一个将所有内容保存在原始data.frame中的工作流程:

library(tidyverse)

tester <- tibble("phyloP46way_primate" = c(".{9}", "0.055{1}0.064{3}", "0.225{1}", "0.271{1}", "-0.706{1}-0.708{1}0.248{3}0.298{3}"))

tester %>% 
    mutate(p_clean = gsub('\\{.*?\\}', ' ', phyloP46way_primate), 
           p_list = strsplit(p_clean, '\\s+'), 
           p_list = map(p_list, as.numeric), 
           p_max = map_dbl(p_list, max))
#> # A tibble: 5 x 4
#>                  phyloP46way_primate                    p_clean    p_list p_max
#>                                <chr>                      <chr>    <list> <dbl>
#> 1                               .{9}                         .  <dbl [1]>    NA
#> 2                   0.055{1}0.064{3}               0.055 0.064  <dbl [2]> 0.064
#> 3                           0.225{1}                     0.225  <dbl [1]> 0.225
#> 4                           0.271{1}                     0.271  <dbl [1]> 0.271
#> 5 -0.706{1}-0.708{1}0.248{3}0.298{3} -0.706 -0.708 0.248 0.298  <dbl [4]> 0.298