[R] Linebreaks in cat() functions that call other variables?

jordanbrace r24jcb at mun.ca
Wed May 22 22:34:34 CEST 2013


Hi everyone,

I'm having some difficulty getting the linebreaks I want with the cat()
function.

I currently have the following function:

lab1<-function(iv,dv){
  a<-anova(lm(dv~iv))
  cat("df(between) is",a[1,1])
  cat("df(within) is", a[2,1])
  cat("ss(between) is", a[1,2])
  cat("ss(within) is", a[2,2])
}

And I want my output to be:

df(between) is 4
df(within) is 45
ss(between) is 232
ss(within) is 400

but instead it prints:
df(between) is 4df(within) is 45ss(between) is 232ss(within) is 400


I have seen other people ask about this issue, but only in the context of
character strings. I understand that if i wanted the output:

Happy birthday
to you 

the correct input would be
>cat("Happy birthday\ntoyou")

However applying the same logic to my string returns an error:
> cat("df(between) is",a[1,1],\n"df(within) is", a[2,1])
Error: unexpected input in "cat("df(between) is",a[1,1],\"
> cat("df(between) is",a[1,1]\n"df(within) is", a[2,1])
Error: unexpected input in "cat("df(between) is",a[1,1]\"

Adding the command 'sep="\n"' to the end of each cat() returns:

df(between) is 
4
df(within) is 
45
ss(between) is 
232
ss(within) is 
400

which is not quite as ugly, but I would still prefer the 4 line format I
posted at the beginning.

Can anyone help?



--
View this message in context: http://r.789695.n4.nabble.com/Linebreaks-in-cat-functions-that-call-other-variables-tp4667748.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list