2

I have a for loop in an R file and I need the output of for loop to be stored in a table for LaTeX. I know about the table package, but I can't figure it out. My code goes as follows:

for(i in seq(2000,2016, by=1)) {
  cat("In year ",i, "the mortality rate for males is",greece$rate$male[i])
}
for(i in seq(1999,2015,by=1)) {
1

You can use xtable:

library(xtable)

M <- matrix(NA, nrow = length(1999:2015), ncol = 2)
ict <- 1
for(i in seq(1999,2015,by=1)) {
 M[ict,1] <-  i
 M[ict,2] <- runif(1)
 ict <- ict + 1
}

xtable(M)

And you can also write seq(1999,2015,by=1) as 1999:2015

  • That works perfectly with my code M <- matrix(NA, nrow = length(1999:2015), ncol = 2) ict <- 1 for(i in seq(1999,2015,by=1)) { M[ict,1] <- i M[ict,2] <- round(greece$rate$male[i],8) ict <- ict + 1 } but when i run xtable(M) i get zeros at the second column althoug the matrix M contains all my data – spyros Jul 5 '18 at 15:33
  • fixed it. It needs xtable(M, digits=4) – spyros Jul 5 '18 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.