我有一个数据框,该数据框应为具有 num,char,num,num (例如:1C72)模式的字符,但任何带有E的观察结果均以科学记数法写成原文CSV (例如:8.00E + 04)
这是我尝试过的:
library(data.table)
library(dplyr)
example = as.data.frame(c("1B73","1C24","2.00E+08", "4.00E+04", "SBV123"))
colnames(example)[1] <- "x"
example$x = as.character(example$x)
#isolate problematic column
setDT(example)[nchar(example$x) == 8, x8:=x]
#create new columns for data we want
example$a <- substr(example$x8,1,1)
example$b <- substr(example$x8,5,5)
example$c <- substr(example$x8,7,8)
example$good <- paste(example$a,example$b,example$c,sep = "")
example$good[example$good == "NANANA"] <- NA
#combine good data:
example = example %>% mutate(g2 = coalesce(good, x))
此解决方案有效,但时间很长,并且创建了大量列。有没有办法创建一堆列?
所需的输出:
|g2 |
|1B73 |
|1C24 |
|2E08 |
|4E04 |
|SBV123|