操作R中的字符串替换小数
manipulating a string in R replacing decimals
我有点卡在这个问题上了。我想删除第一个数字之前的点,但我想保留两个数字之间的任何点。
。 . . . . . . . . . . . . . 122 (100.0) 。 . . . . . . . . . . . . . 7 (5. 7)
例如上面应该输出到
122 (100.0) 。 . . . . . . . . . . . . . 7 (5. 7)
我不确定应该使用什么函数或包来执行上述操作
谢谢!
也许是这样的:
#copy pasted from your example
text <- ". . . . . . . . . . . . . . 122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)"
#find the location of the first number using gregexpr
loc <- gregexpr('[0-9]', text)[[1]][1]
#substring the text from loc and until the end
substr(text, loc, nchar(text)) # or substring(text, loc)
输出:
[1] "122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)"
这应该可以满足您的要求。
sub('^[\h.]+', '', x, perl=TRUE)
我有点卡在这个问题上了。我想删除第一个数字之前的点,但我想保留两个数字之间的任何点。
。 . . . . . . . . . . . . . 122 (100.0) 。 . . . . . . . . . . . . . 7 (5. 7)
例如上面应该输出到
122 (100.0) 。 . . . . . . . . . . . . . 7 (5. 7)
我不确定应该使用什么函数或包来执行上述操作
谢谢!
也许是这样的:
#copy pasted from your example
text <- ". . . . . . . . . . . . . . 122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)"
#find the location of the first number using gregexpr
loc <- gregexpr('[0-9]', text)[[1]][1]
#substring the text from loc and until the end
substr(text, loc, nchar(text)) # or substring(text, loc)
输出:
[1] "122 (100.0) . . . . . . . . . . . . . . 7 (5. 7)"
这应该可以满足您的要求。
sub('^[\h.]+', '', x, perl=TRUE)