I have a tibble with more than 20 columns, so when I view it in the console, only 20 columns will be shown and one will be summarized below the head of the tibble even when I maximize the window.
Is there a function like tail() for columns that shows only the last n columns?
CodePudding user response:
I believe you are looking for tidy select helper last_col()
which by itself brings the last column however per the dplyr site https://tidyselect.r-lib.org/reference/everything.html you can also have an offset in this to count backwards from the last column.
This is used with select()
#returns second to last column
mtcars%>% select(last_col(1))
#pulls in last col and second to last col
## interesting that it is base 0
mtcars%>% select(last_col(0:1))
CodePudding user response:
You can adjust the parameters for print()
for a tibble to control how much is being printed to the console. So, for example, you can control the number of rows by setting n
in print and the number of columns with width
. If you were to set width
to Inf
, then it will print all columns.
print(mtcars_tibble, n = 15, width = Inf)
Output
# A tibble: 32 × 22
mpg...1 cyl...2 disp...3 hp...4 drat...5 wt...6 qsec...7 vs...8 am...9 gear...10 carb...11 mpg...12 cyl...13
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 21 6
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 21 6
3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 22.8 4
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 21.4 6
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 18.7 8
disp...14 hp...15 drat...16 wt...17 qsec...18 vs...19 am...20 gear...21 carb...22
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 160 110 3.9 2.62 16.5 0 1 4 4
2 160 110 3.9 2.88 17.0 0 1 4 4
3 108 93 3.85 2.32 18.6 1 1 4 1
4 258 110 3.08 3.22 19.4 1 0 3 1
5 360 175 3.15 3.44 17.0 0 0 3 2
# … with 27 more rows
However, you could still subset this with normal base R notation to get the last n columns.
n <- 10
print(mtcars_tibble, n = 5, width = Inf)[,(ncol(mtcars_tibble)-n 1):ncol(mtcars_tibble)]
Output
# A tibble: 32 × 10
cyl...13 disp...14 hp...15 drat...16 wt...17 qsec...18 vs...19 am...20 gear...21 carb...22
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 6 160 110 3.9 2.62 16.5 0 1 4 4
2 6 160 110 3.9 2.88 17.0 0 1 4 4
3 4 108 93 3.85 2.32 18.6 1 1 4 1
4 6 258 110 3.08 3.22 19.4 1 0 3 1
5 8 360 175 3.15 3.44 17.0 0 0 3 2
6 6 225 105 2.76 3.46 20.2 1 0 3 1
7 8 360 245 3.21 3.57 15.8 0 0 3 4
8 4 147. 62 3.69 3.19 20 1 0 4 2
9 4 141. 95 3.92 3.15 22.9 1 0 4 2
10 6 168. 123 3.92 3.44 18.3 1 0 4 4
# … with 22 more rows
To simplify, we could create our own print function to determine the number of rows and last columns.
print_tibble <- function(x, y, z) {
print(x, n = y, width = Inf)[,(ncol(x)-z 1):ncol(x)]
}
print_tibble(mtcars_tibble, 5, 10)