Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String manipulation #23

Open
ifly6 opened this issue Apr 25, 2018 · 1 comment
Open

String manipulation #23

ifly6 opened this issue Apr 25, 2018 · 1 comment

Comments

@ifly6
Copy link

ifly6 commented Apr 25, 2018

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.

@dwinsemius
Copy link

dwinsemius commented Oct 26, 2019

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"`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants