I am trying to write following function in R: it will construct vector of trimmed string parts from a string which contains comma-separated parts.
# parse string into vector of trimmed comma-separated parts
parseLine<-function(str)
{
inputVector = strsplit(str, ",")
outputVector = c()
for(s in inputVector)
{
s = gsub("^\\s+|\\s+$", "", s)
if(nchar(s) > 0)
outputVector = c(outputVector, s)
}
return(outputVector)
}
This function definition is parsed successfully. But when I am executing it like this:
parseLine("a, b, c, d")
I get result but as well a strange warning:
[1] "a" "b" "c" "d"
Warning message:
In if (nchar(s) > 0) outputVector = c(outputVector, s) :
the condition has length > 1 and only the first element will be used
And my questions are:
- What does it mean?
- What can I do to get rid of it?