如何在分区操作中静默地撤消dplyr mutate

时间:2018-06-01 01:21:05

标签: r dplyr tidyverse

我有以下数据框:

library(tidyverse)

dat <-  structure(list(sid = c("MK1", "MK2", "MK3"), nof_reads = c(19786677L, 
29531664L, 1195340L), mapped_reads = c(19785168L, 29529532L, 
1195250L)), .Names = c("sid", "nof_reads", "mapped_reads"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -3L))


dat
#> # A tibble: 3 x 3
#>   sid   nof_reads mapped_reads
#>   <chr>     <int>        <int>
#> 1 MK1    19786677     19785168
#> 2 MK2    29531664     29529532
#> 3 MK3     1195340      1195250

当我通过将mapped_readsnof_reads分开来尝试以下操作时,我明白这一点:

> dat %>% mutate(rate = mapped_reads/nof_reads)
# A tibble: 3 x 4
  sid   nof_reads mapped_reads  rate
  <chr>     <int>        <int> <dbl>
1 MK1    19786677     19785168 1.000
2 MK2    29531664     29529532 1.000
3 MK3     1195340      1195250 1.000

注意rate列的值将四舍五入为1.000。 MK_1实际上是这个:

>  19785168/19786677 
[1] 0.9999237

如何解决问题,以便输出而不进行舍入。

1 个答案:

答案 0 :(得分:0)

dat %>% mutate(rate = mapped_reads/nof_reads) %>% as.data.frame()

  sid nof_reads mapped_reads     rate
1 MK1  19786677     19785168 0.999924
2 MK2  29531664     29529532 0.999928
3 MK3   1195340      1195250 0.999925