On top of the topics discussed in janos' answer, the coding style of your code could also be improved:
Running lintr on your code produces two notes:
- line 5: lines should not be more than 80 characters.
- line 12: Put spaces around all infix operators.
Although lintr sometimes produces questionable notes, both of the conventions mentioned above are supported by both Hadley Wickham's R style guide (also available in a different version) and Google's R style guide.
In line 7 there is a space before the left parenthesis. This doesn't belong there and should be deleted. Again, both of the mentioned R style guides agree on this.
The naming of your parameter, full.name
, is a bit more controversial. Google's R style guide fully supports the naming style used in your code:
Don't use underscores ( _
) or hyphens ( -
) in identifiers. Identifiers should be named according to the following conventions. The preferred form for variable names is all lower case letters and words separated with dots (variable.name
), but variableName
is also accepted; function names have initial capital letters and no dots (FunctionName
); constants are named like functions but with an initial k
.
Hadley Wickham's R style guide however explicitely discourages it:
Variable and function names should be lowercase. Use an underscore (_
) to separate words within a name. [...]
Although standard R uses dots extensively in function names (contrib.url()
), methods (all.equal
), or class names (data.frame
), it’s better to use underscores. For example, the basic S3 scheme to define a method for a class, using a generic function, would be to concatenate them with a dot, like this generic.class
. This can lead to confusing methods like as.data.frame.data.frame()
whereas something like print.my_class()
is unambiguous.
Personally, I would recommend using underscores, exactly for the reason stated by Hadley Wickham and because I am not aware of any benefits of using dots over using underscores.
Incorporating this in the code posted by janos yields:
initials <- function(full_name) {
# Returns initials of a full name
# Input will contain only letters (uppercase and/or lowercase) plus
# single spaces between words. Folks like Joseph Gordon-Levitt,
# Conan O’Brien, and David J. Malan won’t be using your program.
# (If only!)
if (nchar(full_name) == 0) {
stop("Valid name please")
}
isspace <- integer(0)
fn.split <- unlist(strsplit(full_name, fixed = TRUE, split = ""))
isspace <- which(fn.split == " ")
init <- toupper(fn.split[c(1, (isspace + 1))])
paste(init, collapse = "")
}
toupper(gsub("\\W*\\b(\\w)\\w*?\\b\\W*", "\\1", full.name))
. And there must be nicer variations. \$\endgroup\$ – flodel Dec 22 '16 at 23:23gsubfn
packge it's even shorter:gsubfn("([A-Z])[a-z]*[ ]*", ~x, full.name)
. \$\endgroup\$ – Iaroslav Domin Feb 3 '17 at 12:06