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

Bryan Hanson hanson at depauw.edu
Wed May 22 23:08:03 CEST 2013


The only permutation you likely didn't try:

>> cat("df(between) is", a[1,1], "\ndf(within) is",  a[2,1])

\n has to be inside the quotes.  HTH. Bryan

On May 22, 2013, at 4:34 PM, jordanbrace <r24jcb at mun.ca> wrote:

> 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.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list