[R] What is "print print print" ?

Jim Lemon jim at bitwrit.com.au
Wed Dec 5 23:31:40 CET 2012


On 12/06/2012 12:03 AM, Vladimir eremeev wrote:
> Hi all.
>
> What is "print print print"?
>
> I don't see output of the print command in for loop and have found this
> link:
> http://stackoverflow.com/questions/1816200/chisq-test-doesnt-print-results-when-in-a-loop
>
> It describes a problem, similar to mine.
> My problem. I want to execute print command in for loop.
>
> If I copy for loop body with print() and paste it to console, I don't see
> any output.
>
> If I type the command, I do see the output.
> If I paste the command by parts I also see the output.
>
> If I construct the command by picking parts of the history with the up
> arrow, I also don't see any output.
>
> Search for "print print print" still didn't bring anything useful.
>
Hi Vladimir,
Your problem is twofold. First, the print command simply tries to 
display whatever is passed as the first argument to it. Invoking:

print()

prints nothing, as there is nothing passed to display. Second, the print 
command is often used in R scripts because most functions will display 
their return values when invoked directly in the R console, but not when 
invoked within a "for" loop. The problem that you mentioned was due to 
the chisq.test function displaying its return value on its own, but not 
within the loop. The "print print print" comment was intended to mean:

print(rownames(cl.vs.Onerall)[i])
print(chisq.test(observed, p=expected.fr))
print("------------------------------")

in the loop so that the successive return values would be displayed. Try 
it for yourself with this simple example:

x<-1:5
length(x)
for(i in 1:3) length(x)
for(i in 1:3) print(length(x))

Jim




More information about the R-help mailing list