I am simply trying to print the output of a statistical process I'm running in R, and I find that the paste function appears to be inverting its inputs. Here's a MWE:
df = data.frame(p_values=c(0.01, 0.001, 0.1, 0.01))
min_index = which.min(df$p_values)
print(paste(str(min_index),
" with p-value ",
str(df$p_values[min_index])))
I'm expecting output more-or-less like this:
2 with p-value 0.001
Instead, I'm getting the highly unintuitive result
int 2
num 0.001
[1] " with p-value "
In addition to the unexpected order of the printing, I'm getting the int and num and [1], as well as not all being on one line.
What's going on here? I had thought paste was nearly a drop-in replacement for Python's concat operator +, but this result has me scratching my head.
structure notstringstr()in R is not the same as the Pythonstr()function, which coerces an object to a string. In R, thestr()function exists to:This means when you do
str(min_index), R tells you that the structure ofmin_indexis that it's an integer vector of length one, and the value of its element is 2.The equivalent R function to Python's
str()would betoString(), or perhapsas.character(). However, in general R is more forgiving about types than Python.paste()and all other string concatenation or printing commands I can think of will coerce numbers to strings for you, so you can just do:Note that I deleted your spaces, as by default
paste()adds one, though that can be changed by supplying a differentsepargument or usingpaste0().Why the output order seems inverted
Accounting for this, you might expect your output to be
"int 2 with p-value num 0.001". However, it is:This is because
str()prints its output and returns nothing:Your command can basically be interpreted as:
This why it prints in the order you see.
A note on displaying output
As Onyambu says in the comments, if you want to display the output without the
[1]you can usecat():Note that you need
fill = TRUEto format this correctly, including a new line at the end. Depending on the purpose of your output, you may also want to look atmessage().