【发布时间】: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。这是警告而不是错误。