在 R 中的冒号后替换文本或数字

Replace text or number after a colon in R

我有很多行的文本文件,我想更改(替换)冒号后的值(文本或数字)。例如,我想将一个值(0.0000000)更改为另一个值;将文本更改为值,将文本更改为文本。如何做到这一点,同时又不会弄乱 R 中的数据结构?

我把我的示例数据放在下面。如何做到这一点,同时又不弄乱 R 中的数据结构?我试过sub,但没有任何好结果。

R 数据:

text_data <- c("some parameter : 0.0000000", "another one    : none", "third one      : none")

数据:

some parameter : 0.0000000
another one    : none
third one      : none

结果:

some parameter : 7500.0000000
another one    : 0.0000000
third one      : "Missing Data"

您可以使用strsplit函数。

text_data <- c("some parameter : 0.0000000", 
               "another one    : none", 
               "third one      : none")

stext <- strsplit(text_data, ":")
s1 <- lapply(stext, function(x) x[1])
s2 <- c("7500.0000000", 0.0000000, "Missing Data")

paste(s1, ":", s2)

# [1] "some parameter  : 7500.0000000"
# [2] "another one     : 0"           
# [3] "third one       : Missing Data"