首页 > 解决方案 > 删除自定义字符串并转换科学计数法

问题描述

样本

    data=data.frame("first"=c("A","B","C"),
"one"=c(1:3),
                    "two"=c("2.1e-003*", 5, "1.9e-9*"),
                            "three"=c("1.6e-002*", 5, "8.1e-2*"))

我的目标是删除“*”并将科学记数法转换为数字。

我试过无济于事

WANT=gsub("\\*.*","",data)

标签: rdplyr

解决方案


library("tidyverse")

data <- data.frame(
  "first" = c("A", "B", "C"),
  "one" = c(1:3),
  "two" = c("2.1e-003*", 5, "1.9e-9*"),
  "three" = c("1.6e-002*", 5, "8.1e-2*")
)

data %>%
  # Cast unhelpful `factor` columns to `character`
  mutate_at(vars(two, three), as.character) %>%
  mutate_at(vars(two, three), parse_number) %>%
  # You can turn off the scientific notation
  # but you end up with a lot of zeros...
  mutate_at(vars(two, three), format, scientific=FALSE)
#>   first one          two three
#> 1     A   1 0.0021000000 0.016
#> 2     B   2 5.0000000000 5.000
#> 3     C   3 0.0000000019 0.081

reprex 包(v0.2.1)于 2019 年 3 月 29 日创建


推荐阅读