0

I was trying out this code. I want to print the first six values of both the dataframes. But this isn't working. Where am I going wrong?

d1 = mtcars
d2 = Arthritis
d = list(d1,d2)
for(i in 1:2)
{
  head(d[[i]])
  }

I tried it without the for loop and it works. I mean if I put in head(d[[1]]) I get the first six values of mtcars. But with the loop it is not printing anything

th_cy
  • 51
  • 1
  • 4

1 Answers1

0

Automatic printing is turned off inside loops and functions (explained here). To print inside of a loop or function, explicitly call the print() function.

d1 <- mtcars
d2 <- data(iris)
d  <- list(d1,d2)
for(i in 1:2) {
  print(head(d[[i]]))
}
tim
  • 879
  • 1
  • 8
  • 26