删除自定义字符串并转换科学计数法

时间:2019-03-28 22:52:07

标签: r dplyr

样品

    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)

1 个答案:

答案 0 :(得分:2)

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 package(v0.2.1)于2019-03-29创建