Is there a way to store the results of for loop in a excel file

Hi all, For the below for loop is there way to save the results of for loop in an excel file. In other words, save the results only after the for loop is executed.

for(i in 1:5)
{
  for(j in 1:2)
{
        print(i*j);
}
}

if you want to save the results of output printed to the console you could use capture.output().
but I caution you, it would be extremely rare to have a programming requirement for which this is the best, or even a good solution :smiley:

Perhaps you want to talk more generally about what you want to acheive, and we can talk strategies for doing that in an efficent / 'R' kind of way

Hi Nir,

Thanks. Actually i got below output

> capture.output()
character(0)

myprints <- capture.output({
for(i in 1:5)
{
  for(j in 1:2)
  {
    cat(i*j,"\n");
  }
}})

myprints

cat(paste0(myprints,collapse = "\n"))

probably a better way to think about such things >

mydata1 <- expand.grid(i=1:5,j=1:2)  
mydata2 <-  dplyr::mutate(mydata1, result=i*j)

print(dplyr::select(mydata2,result))

Thanks capture.output is working. But just want to know for curiosity. Why class(myprints) is null. Is it something we can convert to dataframe (myprints)? I tried but could not

try again ?
class(myprints) is not null, rather its a character vector so could become a dataframe.
if you want a dataframe, why wouldnt you use the expand.grid approach ?

Got it thanks​:blush::blush::blush:....

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.