I have text file with many lines and I want to change (replace) the value (text or number), which is after colon. For example, I want to change one value (0.0000000) into another one; change a text into a value, and a text into a text. How to do that, while not messing the data structure in R?
我有很多行的文本文件,我想更改(替换)冒号后的值(文本或数字)。例如,我想将一个值(0.0000000)更改为另一个值;将文本更改为值,将文本更改为文本。如何做到这一点,而不是搞乱R中的数据结构?
I put my example data below. How to do that, while not messing the data structure in R? I've tried sub, but without any good results.
我把我的示例数据放在下面。如何做到这一点,而不是搞乱R中的数据结构?我尝试过sub,但没有任何好结果。
R Data:
text_data <- c("some parameter : 0.0000000", "another one : none", "third one : none")
Data:
some parameter : 0.0000000
another one : none
third one : none
Result:
some parameter : 7500.0000000
another one : 0.0000000
third one : "Missing Data"
1 个解决方案
#1
2
You can use the strsplit
function.
您可以使用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"
#1
2
You can use the strsplit
function.
您可以使用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"