-2

I want to create a table for the output of an sqlite command that I run repetitively in a for loop.

These

are the pictures of my code and the intended output.

I use PrettyTable when I code in Python. Is there a similar option in R?

r2evans
  • 141,215
  • 6
  • 77
  • 149
SampyKIshan
  • 29
  • 1
  • 7
  • Outputting in the console or in a knitted document? – Elin Nov 02 '18 at 21:20
  • @paoloeusebi, I rolled back your edit on the question that removed the only context we have for the question and the accepted answer. Granted, images are strongly discouraged (https://meta.stackoverflow.com/a/285557/3358272), but without it the answer makes no sense. SampyKIshan, in future questions, please provide copy-able data/code, not an image of it; see my link for more details, but briefly: copy-ability; search engines skip it; screen-readers can not. – r2evans Nov 02 '18 at 22:20

2 Answers2

-1

Why not preallocate a dataframe and [over]write values with each iteration of the for loop?

before the loop:

output <- data.frame('region' = regions)
output$good_traffic <- 0
output$bad_traffic <- 0

within the loop:

output[output$region == region, 'good_traffic'] <- good_percent
output[output$region == region, 'bad_traffic'] <- bad_percent

after the loop:

print(output)
12b345b6b78
  • 995
  • 5
  • 16
-1

If I understand what you means, you should try with kable() from knitr package

library(knitr)
t <- table(mtcars$cyl,
       mtcars$am)
kable(t)

summary(lm(hp ~ cyl, data = mtcars))
coef.lmfit <- coef(summary(lm(hp ~ cyl, data = mtcars)))
kable(coef.lmfit)
paoloeusebi
  • 1,056
  • 8
  • 19