I have a dataset with a string column like below, and I am trying to extract the number from the string. I have achieved the extraction for observations that do not contain two decimals. When attempting to extract an observation with two decimals, I am having trouble. I am trying to replace the first decimal with a |
like below:
library(stringr)
words=data.frame(text=c('I need a number. It is the number 40.6',
'I bet youd like this number. Too bad but it is 52.3',
'This number is important. It is 1.6'))
words$new_text=str_replace(string = words$text,
pattern = '.',
replacement = '|')
words$new_text
#> [1] "| need a number. It is the number 40.6"
#> [2] "| bet youd like this number. Too bad but it is 52.3"
#> [3] "|his number is important. It is 1.6"
The problem arises that we can see instead of the first .
being replaced with |
as the case would be with other character types, the first character in the string is replaced with |
, i.e. I expected this:
library(stringr)
words=data.frame(text=c('I need a number. It is the number 40.6',
'I bet youd like this number. Too bad but it is 52.3',
'This number is important. It is 1.6'))
words$new_text2=str_replace(string = words$text,
pattern = 'n',
replacement = '|')
words$new_text2
#> [1] "I |eed a number. It is the number 40.6"
#> [2] "I bet youd like this |umber. Too bad but it is 52.3"
#> [3] "This |umber is important. It is 1.6"
EDIT: "... trying to extract the number ...", not "... trying to extract the second number ..."