Having trouble viewing more than 10 rows in a tibble [duplicate]
Asked Answered
N

3

26

First off - I am a beginner at programming and R, so excuse me if this is a silly question. I am having trouble viewing more than ten rows in a tibble that is generated from the following code.

The code below is meant to find the most common words in a book. I am getting the results I want, but how do I view more than 10 rows of data. To my knowledge, it is not being saved as a data frame that I can call.

library(dplyr)
tidy_books %>%
    anti_join(stop_words) %>%
    count(word, sort=TRUE)
Joining, by = "word"
# A tibble: 3,397 x 2
   word       n
   <chr>  <int>
 1 alice    820
 2 queen    247
 3 time     141
 4 king     122
 5 head     112
 6 looked   100
 7 white     97
 8 round     96
 9 voice     86
10 tone      81
# ... with 3,387 more rows
Nelly answered 6/3, 2018 at 2:5 Comment(3)
Try View(tidy_boooks) or head(tidy_books, 20)Centennial
tibbles intentionally only print a few rows - you can send the result through %>% print(n = 20) to see more.Izy
The suggestion by @Izy is probably the most elegant, however you can also simply wrap your entire statement with as.data.frame.Needlefish
C
13

What I often do when I want to see the output of a pipe like that is pipe it straight to View()

library(dplyr)
library(tidytext)

tidy_books %>%
    anti_join(stop_words) %>%
    count(word, sort=TRUE) %>%
    View()

If you want to save this to a new object that you can work with later, you can assign it to a new variable name at the beginning of the pipe.

word_counts <- tidy_books %>%
    anti_join(stop_words) %>%
    count(word, sort=TRUE)
Corbeil answered 6/3, 2018 at 3:6 Comment(0)
D
30

Although this question has a perfectly ok answer, the comment from @Marius is much shorter, so:

tidy_books %>% print(n = 100)

As you say you are a beginner you can replace n = 100 with any number you want

Also as you are a beginner, to see the whole table:

tidy_books %>% print(n = nrow(tidy_books))
Darwinism answered 21/2, 2019 at 2:11 Comment(2)
for some reason, print(n = ...) turns on scientific notation in the tibble display. is there away to avoid that?Tobitobiah
You can do options(scipen=10) to penalize the display of scientific notation. Higher numbers penalize (and thus reduce) the chance of scientific notation being displayed, lower numbers (like -10) increase the chance.Jessen
C
13

What I often do when I want to see the output of a pipe like that is pipe it straight to View()

library(dplyr)
library(tidytext)

tidy_books %>%
    anti_join(stop_words) %>%
    count(word, sort=TRUE) %>%
    View()

If you want to save this to a new object that you can work with later, you can assign it to a new variable name at the beginning of the pipe.

word_counts <- tidy_books %>%
    anti_join(stop_words) %>%
    count(word, sort=TRUE)
Corbeil answered 6/3, 2018 at 3:6 Comment(0)
E
1

If you want to stay in the console, then note that tibbles have print S3 methods defined so you can use options such as (see ?print.tbl):

very_long <- as_tibble(seq(1:1000))
print(very_long, n = 3)
# A tibble: 1,000 x 1
  value
  <int>
1     1
2     2
3     3
# ... with 997 more rows

Note, tail doesn't play with tibbles, so if you want to combine tail with tibbles to look at the end of your data, then you have to do something like:

print(tail(very_long, n = 3), n = 3)
# A tibble: 3 x 1
  value
  <int>
1   998
2   999
3  1000
Extrasystole answered 14/12, 2018 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.