HelpNeeded3
HelpNeeded3

Reputation: 91

How to Print a Tibble in R When it Groups Columns Automatically?

I have a tibble. When I use view(df), I get the following output in a new window:

indicator.id  indicator.value  country.id  country.value  
POP.HI        Population       1A          Arab World
POP.HI        Population       1B          Armenia

But when I use head(df), I get the following output:

          indicator                 country
        <data.frame>            <data.frame>
<data.frame [6 × 2]>    <data.frame [6 × 2]>    
<data.frame [6 × 2]>    <data.frame [6 × 2]>

So, head(df) seems to be combining the two indicator columns into one and the same for country. Is there a way to fix this so that I can print a bit of the tibble (like view(), but not in a new window)?

I've tried print.data.frame(head(df)), which almost works, but the output is quite messy and not in a nice scrollable table. Advice appreciated.

Upvotes: 1

Views: 115

Answers (1)

akrun
akrun

Reputation: 887691

It is because the columns 'indicator', 'country' are both 'data.frame'. An option is to use do.call with data.frame to create a data.frame with 4 columns

df1 <- do.call(data.frame, df)

Another option is unnest_wider

library(dplyr)
library(tidyr)
df %>%
    unnest_wider(c(indicator, country))

Upvotes: 1

Related Questions