【问题标题】:How to convert percentage text into numeric using dplyr pipe?如何使用 dplyr 管道将百分比文本转换为数字?
【发布时间】:2019-11-06 05:37:15
【问题描述】:

我有以下小标题:

library(tidyverse)
dat <- structure(list(V1 = c("Number of input reads", "Uniquely mapped reads number", 
"Uniquely mapped reads %", "Average mapped length"), V2 = c("26265603", 
"13330431", "50.75%", "47.37")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L))

看起来像这样:

  V1                           V2      
  <chr>                        <chr>   
1 Number of input reads        26265603
2 Uniquely mapped reads number 13330431
3 Uniquely mapped reads %      50.75%  
4 Average mapped length        47.37 

我想要做的是将V2 列转换为数字。预期的最终结果是这样的:

  V1                           V2      
  <chr>                        <dbl>   
1 Number of input reads        26265603
2 Uniquely mapped reads number 13330431
3 Uniquely mapped reads %      0.5075 
4 Average mapped length        47.37 

我试过了

dat %>%
mutate(V2 = case_when(V1 == "Uniquely mapped reads %" ~ as.numeric(sub("%","",V2))/100, 
                        TRUE ~ as.numeric(V2)))

但它给了我警告:

Warning message:
In eval_tidy(pair$rhs, env = default_env) : NAs introduced by coercion

正确的做法是什么?

【问题讨论】:

  • 您的尝试对我有用并给出了预期的输出。你得到什么输出?
  • @RonakShah 谢谢。我更新了OP。这是警告而不是错误。

标签: r string dplyr tidyverse


【解决方案1】:

使用管道可能有点复杂,因为我们只想更新几行,但在基础 R 中,我们可以首先找出其中包含特定字符串的行,然后只更新那些 V2 值。

inds <- dat$V1 ==  "Uniquely mapped reads %"
dat$V2[inds] <- as.numeric(sub("%", "", dat$V2[inds]))/100

dat
# A tibble: 4 x 2
#  V1                           V2      
#  <chr>                        <chr>   
#1 Number of input reads        26265603
#2 Uniquely mapped reads number 13330431
#3 Uniquely mapped reads %      0.5075  
#4 Average mapped length        47.37 

一种使用管道的方式可以是

library(dplyr)

dat %>%
   mutate(V2 = as.numeric(sub("%", "", V2))/
               (c(1, 100)[(V1 == "Uniquely mapped reads %") + 1]))

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多