How to replace a dot with a space in variable names, at least when printing?

When importing data from a file with column headers where the data items are tab-delimited, the environment RStudio automatically replaces space between strings to dot, although the correct variable names are Species names containing a space and possibly other characters.

When outputting final reports, the correct spelling of species names is critical. I need to replace the dots with spaces. However, the str_replace function does not change the dots.

name_i<-str_replace(all_names[[i]], ".", " ")

Removes the first character in the name, instead of replacing the dots in the middle of the name:
"Genus.species" --> " enus.species"

name_i=str_replace_all(name_i, ".", " ")

Removes all characters in the name, instead of replacing the dots in the middle of the name:
"Genus.species" --> " "

This behavior is similar to that of a regular expression, but escaping "\." also does not work, so this is not a regular expression.

In general, I would like to solve this problem not for printing, but in general, because scientific names of variables (Species) often contain dots and other symbols.

For example:
"Bembidion mannerheimii C. R. Sahlberg, 1827"
"Genus species subspecies (N. Fam., 2000)"

ps. It is strange that they decided to implement the code this way, instead of creating auxiliary alphanumeric variable names.

So far hacked this problem in this way, but it's ugly

Replaced in the data file

" " -> "_"
"(" -> "xxx"
")" -> "yyy"
"," -> "zzz"

Reverse replacement:

         name_i=str_replace_all(name_i, "_", " ")
         name_i=str_replace_all(name_i, "xxx", "(")
         name_i=str_replace_all(name_i, "yyy", ")")
         name_i=str_replace_all(name_i, "zzz", ",")

In a particular case, this problem was solved in the following way. And in the general case, either you need to change either the runtime options or the input data.

name_i=gsub("\\."," ",name_i)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.