-1

For example, I have the following data frame, DF:

> print.data.frame(df)
  age amount      type
1  35      1      term
2  42      3 universal
3  51      2      term
4  38      5     whole
5  19      2 universal
6  45      3      term
7  63      7     whole
> 

I want to use the head() function to print just the first 6 rows of df, but when I run the code I get the following:

head(df)

> head(df)
>

However, if I use print.data.frame(head(df)), it works as intended:

> print.data.frame(head(df))
  age amount      type
1  35      1      term
2  42      3 universal
3  51      2      term
4  38      5     whole
5  19      2 universal
6  45      3      term
> 

Why doesn't head() function work without using print.data.frame?

Edit:

This is the code I used to create the data frame

age <- c(35, 42, 51, 38, 19, 45, 63)
amount <- c(1, 3, 2, 5, 2, 3, 7)
type <- c("term","universal","term","whole","universal","term","whole")

df<-data.frame(age,amount,type)
yobino
  • 1
  • 1
  • 3
  • Can you `dput` your data or how you are creating your data frame? – sm925 Oct 10 '19 at 15:24
  • Does it work with other data.frames? is ```class(df)``` returning a data.frame or something else? – dasds Oct 10 '19 at 15:25
  • Did you overwrite the default `head()` function? What if you run `rm(head)` and try again? – MrFlick Oct 10 '19 at 15:25
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 10 '19 at 15:25
  • It doesn't work with other data.frames either. `class(df)` returns data.frame. I also didn't overwrite the head() function – yobino Oct 10 '19 at 15:32
  • As a test, does ```df %>% head()``` from dplyr work? – Fnguyen Oct 10 '19 at 15:38
  • @Fnguyen - that isn't working either – yobino Oct 10 '19 at 15:43

1 Answers1

0

Figured it out. It's because I'm running the code in a chunk. Duh...

Thanks everyone!

yobino
  • 1
  • 1
  • 3