数字粘在一起作为字符

问题描述 投票:0回答:1

我有一个带有测量值的数据集(txt文件,空格分隔),有些数字像这样粘在一起:

enter image description here

目前所有列都属于“字符”类,因为转换后这些粘贴的数字为“NA”。我为负数创建了一个例程,到目前为止很容易:

findandreplace <- function(file_name){


dat <- read_table2(file_name, col_names = FALSE)


  for (n in 0:9) {
    dat <- data.frame(lapply(dat, function(x) {gsub(paste0(n, "-"), paste0(n, " -"), x)}))
  }

  #save dat as txt and read it again
}

但现在,我不知道如何分离正值。如果你想要,你可以使用这个MWE:

b = c("340.9","341","316.1","336.8316.39","378.8","315","386.57317.33",NA,NA)
a  =c(1,2,3,4,5,6,7,8,9)
c  = data.frame(a,b)

这应该是这样的:

b = c("340.9","341","316.1","336.8","316.39","378.8","315","386.57", "317.33")
a  =c(1,2,3,4,5,6,7,8,9)
c  = data.frame(a,b)
r type-conversion
1个回答
3
投票
 x=unlist(strsplit(gsub("(.*)(3(?>\\d{2}\\.))","\\1 \\2",b,perl=T)," "))  
grep("\\d",x,value = T)
[1] "340.9"  "341"    "316.1"  "336.8"  "316.39" "378.8"  "315"    "386.57" "317.33"


transform(c,b=grep("\\d",x,value = T))
  a      b
1 1  340.9
2 2    341
3 3  316.1
4 4  336.8
5 5 316.39
6 6  378.8
7 7    315
8 8 386.57
9 9 317.33
© www.soinside.com 2019 - 2024. All rights reserved.