How to extract specific characters in R String - r

I have a file name string:
directoryLocation<-"\Users\me\Dropbox\Work\"
How can I extract all the "\" and replace it with "\"? In other languages, you can loop through the string and then replace character by character, but I don't think you can do that in R.
I tried
substr(directoryLocation,1,1)
but it is highly optimized to this case...how can it be more general?
Thanks

gsub is the general tool for this, but as others have noted you need a confusing four slashes to account for the escapes: you need to escape for both R text and the regexp engine simultaneously.
An alternative, if using Windows, is to use normalizePath and setting the winslash parameter:
normalizePath(directoryLocation,winslash="/",mustWork=FALSE)
[1] "C:/Users/me/Dropbox/Work/"
Though this may perform additional work on expanding relative paths to absolute ones (seen here by prepending with C:).

In theory this would do what you want
gsub("\\\", "/", directoryLocation)
however...
R> directoryLocation<-"\\Users\\me\\Dropbox\\Work\\"
R> directoryLocation
[1] "\\Users\\me\\Dropbox\\Work\\"
R> gsub("\\\\", "/", directoryLocation)
[1] "/Users/me/Dropbox/Work/"

At least on windows one needs to escape all of the backslashes, but gsub is what you want.
gsub("\\\\","/","\\Users\\me\\Dropbox\\Work\\")
[1] "/Users/me/Dropbox/Work/"

Related

Replacing Backward slash in R

I want to replace backward slashes with forwards slashes in a string. So I used below syntax in R.
stringr::str_replace("\\", "//", "\\asd")
However it fails to replace the backward slashes in the given string.
Could you please help to find the right way to replace them?
I am using R in Windows 10 machine
Try this:
str_replace("\\asd", fixed("\\"), "//")
You have the arguments in the wrong order and you need to escape the backslashes.
> stringr::str_replace("\\asd", "\\\\", "//")
[1] "//asd"
You could use gsub function in R which is used for replacement operations. The functions takes the input and substitutes it against the specified value.
gsub("\\\\", "/", x)

Substring by specific character [duplicate]

I would like to extract filename from url in R. For now I do it as follows, but maybe it can be done shorter like in python. assuming path is just string.
path="http://www.exanple.com/foo/bar/fooXbar.xls"
in R:
tail(strsplit(path,"[/]")[[1]],1)
in Python:
path.split("/")[-1:]
Maybe some sub, gsub solution?
There's a function for that...
basename(path)
[1] "fooXbar.xls"
#SimonO101 has the most robust answer IMO, but some other options:
Since regular expressions are greedy, you can use that to your advantage
sub('.*/', '', path)
# [1] "fooXbar.xls"
Also, you shouldn't need the [] around the / in your strsplit.
> tail(strsplit(path,"/")[[1]],1)
[1] "fooXbar.xls"

How to just remove (\) from string with (\") while keeping (")?

Since special characters are inevitable while working data from excel.
There are so many links to eradicate special characters but when trying to remove \, we got to use \" which will eliminate both
Remove quotes ("") from a data.frame in R
Here, they remove both quotes but quotes at the end needs to be present.
> abc = c("Hi\"","Hello\\")
> abc
[1] "Hi\"" "Hello\\"
> str_replace_all(abc, "\"","")
[1] "Hi " "Hello\\"
But can we have
Hi" as an output ?
#Ronak Shah, #Chelmy88 and #Konrad Rudolph
helped me to understand where I was wrong in interpretation.
basically, it has to do with the way R renders the string in console.
Solution using cat() can resolve the confusion.

Paste "25 \%" in R for further processing in LaTeX

I want a character variable in R taking the value from, lets say "a", and adding " \%", to create a %-sign later in LaTeX.
Usually I'd do something like:
a <- 5
paste(a,"\%")
but this fails.
Error: '\%' is an unrecognized escape in character string starting "\%"
Any ideas? A workaround would be to define another command giving the %-sign in LaTeX, but I'd prefer a solution within R.
As many other languages, certain characters in strings have a different meaning when they're escaped. One example for that is \n, which means newline instead of n. When you write \%, R tries to interpret % as a special character and fails doing so. You might want to try to escape the backslash, so that it is just a backslash:
paste(a, "\\%")
You can read on escape sequences here.
You can also look at the latexTranslate function from the Hmisc package, which will escape special characters from strings to make them LaTeX-compatible :
R> latexTranslate("You want to give me 100$ ? I agree 100% !")
[1] "You want to give me 100\\$ ? I agree 100\\% !"

How do I strip dollar signs ($) from data/ escape special characters in R?

I've been using gsub("toreplace","replacement", myvector) to clean out data in R. While this works for commas and the like, removing "$" has no effect. So if I do gsub("$","",myvector) all the dollar signs remain in place.
I think this is because $ is a special character in R. I tried escaping it "\$" but that yields the same result (no effect). And I couldn't find a resource on escaping special characters in R.
Obviously I should do this in preprocessing. But I was wondering if anyone out there knew how to either a) escape special characters in R b) get rid of pesky $ in R directly. For science.
You have to escape it twice, first for R, second for the regex.
gsub('\\$', '', c("a$a", "bb$"))
[1] "aa" "bb"
See ?Quotes for details on quoting and escaping.
Use fixed = TRUE:
gsub('$', '', c("a$a", "bb$"), fixed = TRUE)
Then you don't need to worry about any special characters. In stringr, this is implemented a little differently:
library(stringr)
str_replace_all(c("$100","ta$ty"), fixed("$"), "")
Thanks to DiggyF and James for the examples!
Escaping characters can be a pain some times, but just putting it in square brackets (make it a character class) helps with this:
> gsub("[$]","",c("$100","ta$ty"))
[1] "100" "taty"
if you have $ followed by number in set of data columns (e.g. $400,000) there is an easier way that worked like charm for me.
data%>%
mutate_at(5:6, parse_number)
where 5:6 are the data column numbers.

Resources