aRrgh
aRrgh copied to clipboard
String manipulation
I don't understand how it was a good idea to ship this language without any ability to concatenate strings other than paste
and its counterparts like paste0
. It means that normal code in another language like
return "apple says, " + logMessage;
Now has to be wrapped inside a function call. Inside the return call, which inexplicably also has brackets, generating return(paste("apple says,", log_message))
, with omission of the white space because paste isn't just a joining system, it's also a joining one, adding an invisible space.
But lets say you want to deal with a string. Strings can have all sorts of things in them. Lets say ours has backslashes I want to make normal slashes. Java, Python below:
"C:\\blah\\blah\\blah".replace("\\", "/");
r'C:\blah\blah\blah'.replace('\\', '/')
And even if there weren't something of the sort, a sensible design for argument parameters would be something like string_replace(string, pattern, replacement)
. But no, in R, we don't use normal orders. You instead use gsub(pattern, replacement, string)
. Why is the order messed up? Who knows.
Next, you want to find out whether your string has your extra special characters in it? You want to only match on files which end in, say, csv
? Java can just call a String#contains
method. Python can just ask whether it is the case pattern in string
. R? grepl(pattern, chars)
.
Why are these names so unhelpful? Probably because they're all derived from grep
. But fortunately, with R, you can have these useful tools hidden from you without having to look them up or perfect knowledge of the library. PRogRess.
The authors of S were already Unix tool proficient. They probably would have written a very terse awk program to do any string manipulation of data. Yes, the argument order came from Unix grep.
You could trivially write a %+%
infix function to do pasting of 2 vectors:
"%+%" <- function(a,b) paste(a,b)
"This will be" %+% "pasted" [1] "This will be pasted"`