I would like to recode some identifiers, from upper case to lower case.
I am not sure what the issue is here.
n = c(\'AFD.434\', \'BSD.23\', \'F234
Your gsub call replaces each occurrence with the literal string "[[:lower:]]".
The simplest solution is to not use regular expressions; simply use tolower()
(as already mentioned in the comments / other answers).
One possible approach with regular expressions is the usage of Perl extended mode and the \L
modifier to convert to lowercase:
gsub(pattern = '([[:upper:]])', perl = TRUE, replacement = '\\L\\1', n)
This approach
(...)
to "remember" the match\1
to refer to the match in the replacement string\L
modifier to convert the match to lowercaseSee the online help for gsub
for further details.
The gsub function takes a regular expression as the first argument, and a replacement string as a second one that cannot have special characters/classes that are used in a regular expression. They are rather treated as literals. Thus, you are trying to replace each uppercase letter with a literal string [[:lower:]]
(see my demo).
To turn the values of your data frame to lower case, you must use tolower() (as already have been pointed out):
n = c('AFD.434', 'BSD.23', 'F234.FF')
tolower(n)
See demo
Output:
[1] "afd.434" "bsd.23" "f234.ff"
Note that Franks's suggestion to use Perl \L
operator is handy when we need to capitalize specific parts of the string, but in your case it seems to be an unnecessary overhead.