replace space in string with gsub

Hi,

In R desktop, I often use gsub to replace string characters. However I found that in R studio cloud, gsub can not recognise white space with any syntax such as "\s", " ", or "[[:space:]]". In R desktop, everything is OK.

Any ideas why and how to fix this?

Thanks!

Works fine for me

gsub("\\s", "_", "string with  space")
# [1] "string_with__space"
gsub("[[:space:]]", "_", "string with  space")
# [1] "string_with__space"
1 Like

This happened to me. I found that this might related to the encoding of original dataset, but not sure.

typeof(a)
[1] "character"
a
[1] "33 194" "30 702" "29 644" "26 107" "2 396"
gsub("\ \s", "_", a)
[1] "33 194" "30 702" "29 644" "26 107" "2 396"

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.