R: non-numeric argument to binary operator
When debugging R code, given my Java background, I often find myself trying to print out the state of variables along with an appropriate piece of text like this:
names = c(1,2,3,4,5,6)
> print("names: " + names)
Error in "names: " + names : non-numeric argument to binary operator
We might try this next:
> print("names: ", names)
[1] "names: "
which doesn’t actually print the names variable - only the first argument to the print function is printed.
We’ll find more success with the https://stat.ethz.ch/R-manual/R-devel/library/base/html/paste.html function:
> print(paste("names: ", names))
[1] "names: 1" "names: 2" "names: 3" "names: 4" "names: 5" "names: 6"
This is an improvement but it repeats the 'names:' prefix multiple times which isn’t what we want. Introducing the https://stat.ethz.ch/R-manual/R-devel/library/base/html/toString.html function gets us over the line:
> print(paste("names: ", toString(names)))
[1] "names: 1, 2, 3, 4, 5, 6"
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.