压倒一切;“未显示变量”;在dplyr中,显示来自df的所有列

压倒一切;“未显示变量”;在dplyr中,显示来自df的所有列,r,dplyr,options,output-formatting,displayformat,R,Dplyr,Options,Output Formatting,Displayformat,当我在本地数据帧中有一列时,有时我会得到消息变量未显示,比如这个(荒谬的)示例只需要足够的列 library(dplyr) library(ggplot2) # for movies movies %.% group_by(year) %.% summarise(Length = mean(length), Title = max(title), Dramaz = sum(Drama), Actionz = sum(Action), Action = sum(Action)

当我在本地数据帧中有一列时,有时我会得到消息
变量未显示
,比如这个(荒谬的)示例只需要足够的列

library(dplyr)
library(ggplot2) # for movies

movies %.% 
 group_by(year) %.% 
 summarise(Length = mean(length), Title = max(title), 
  Dramaz = sum(Drama), Actionz = sum(Action), 
  Action = sum(Action), Comedyz = sum(Comedy)) %.% 
 mutate(Year1 = year + 1)

   year    Length                       Title Dramaz Actionz Action Comedyz
1  1898  1.000000 Pack Train at Chilkoot Pass      1       0      0       2
2  1894  1.000000           Sioux Ghost Dance      0       0      0       0
3  1902  3.555556     Voyage dans la lune, Le      1       0      0       2
4  1893  1.000000            Blacksmith Scene      0       0      0       0
5  1912 24.382353            Unseen Enemy, An     22       0      0       4
6  1922 74.192308      Trapped by the Mormons     20       0      0      16
7  1895  1.000000                 Photographe      0       0      0       0
8  1909  9.266667              What Drink Did     14       0      0       7
9  1900  1.437500      Uncle Josh's Nightmare      2       0      0       5
10 1919 53.461538     When the Clouds Roll by     17       2      2      29
..  ...       ...                         ...    ...     ...    ...     ...
Variables not shown: Year1 (dbl)

我想看
Year1
!如何查看所有列,最好是默认设置。

dplyr
有自己的打印功能,用于
dplyr
对象。在这种情况下,作为操作结果的对象是
tbl\u df
。然后,匹配的打印功能是dplyr:::print.tbl_df。这表明
trunc_mat
是负责打印内容和未打印内容的函数,包括哪些变量

遗憾的是,
dplyr:::print.tbl_df
不向
trunc_mat
传递任何参数,
trunc_mat
也不支持选择显示哪些变量(仅显示多少行)。解决方法是将dplyr的结果强制转换为
数据帧
,并使用

res = movies %.% 
 group_by(year) %.% 
 summarise(Length = mean(length), Title = max(title), 
  Dramaz = sum(Drama), Actionz = sum(Action), 
  Action = sum(Action), Comedyz = sum(Comedy)) %.% 
 mutate(Year1 = year + 1)

head(data.frame(res))
  year    Length                       Title Dramaz Actionz Action Comedyz
1 1898  1.000000 Pack Train at Chilkoot Pass      1       0      0       2
2 1894  1.000000           Sioux Ghost Dance      0       0      0       0
3 1902  3.555556     Voyage dans la lune, Le      1       0      0       2
4 1893  1.000000            Blacksmith Scene      0       0      0       0
5 1912 24.382353            Unseen Enemy, An     22       0      0       4
6 1922 74.192308      Trapped by the Mormons     20       0      0      16
  Year1
1  1899
2  1895
3  1903
4  1894
5  1913
6  1923

您可能想一瞥:

> movies %>%
+  group_by(year) %>%
+  summarise(Length = mean(length), Title = max(title),
+   Dramaz = sum(Drama), Actionz = sum(Action),
+   Action = sum(Action), Comedyz = sum(Comedy)) %>%
+  mutate(Year1 = year + 1) %>% glimpse()
Variables:
$ year    (int) 1893, 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1901, 1902,...
$ Length  (dbl) 1.000000, 1.000000, 1.000000, 1.307692, 1.000000, 1.000000,...
$ Title   (chr) "Blacksmith Scene", "Sioux Ghost Dance", "Photographe", "Ve...
$ Dramaz  (int) 0, 0, 0, 1, 0, 1, 2, 2, 5, 1, 2, 3, 4, 5, 1, 8, 14, 14, 14,...
$ Actionz (int) 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 3, 0, 0, 1, 0,...
$ Action  (int) 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 3, 0, 0, 0, 0, 3, 0, 0, 1, 0,...
$ Comedyz (int) 0, 0, 0, 1, 2, 2, 1, 5, 8, 2, 8, 10, 6, 2, 6, 8, 7, 2, 2, 4...
$ Year1   (dbl) 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1901, 1902, 1903,...NULL

所以,这有点旧,但我在寻找相同问题的答案时发现了这一点。我提出了这个解决方案,它坚持管道的精神,但在功能上与公认的答案相同(请注意,管道符号
%.%
被弃用,取而代之的是
%>%

现在有一种方法可以覆盖打印出来的列的宽度。如果你运行这个命令,一切都会好起来

options(dplyr.width = Inf)
我写的。

movies%.%groupby(year)%.%打印。默认值

dplyr
使用而不是默认的打印选项,
dplyr:::print.tbl_df
来确保您的屏幕不会因庞大的数据集而过载。当你最终将你的东西缩减到你想要的,并且不想再被保存在你自己的错误中时,只需在末尾粘贴
print.default
,就可以把所有东西都吐出来


顺便说一句,
methods(print)
显示了有多少软件包需要编写自己的
print
函数(想想,例如,
igraph
xts
——这些都是新的数据类型,所以你需要告诉它们如何在屏幕上显示)


下一个谷歌人。

+1用于发现
一瞥
。就我个人而言,我喜欢查看所有专栏的主要原因是为了方便地检查我添加的专栏(通过总结或修改)是否真的达到了我的目的。因此,
scape
不太适合这种情况。对于最新的dplyr版本,使用%>%而不是%。%拉取请求总是受欢迎的:)但是
print.tbl_df
可能需要一个
所有列
参数。我认为应该是带有“s”的
选项
。我无法编辑,因为编辑必须是10个字符。这是一个不错的选项,但在列太多的情况下不太有用。我碰巧在一个df中显示了大约200列,但行和列之间的顺序丢失了。此外,由于字符太多,大多数行在某个时候被截断。我想共享命令以恢复默认行为,即:“options(dplyr.width=NULL)”
options(dplyr.width = Inf)