r语言 - 覆盖 dplyr 中的"Variables not shown",以显示 df 中的所有列



当我在本地数据帧中有一列时,有时我会得到消息Variables not shown,例如这个(荒谬的)示例只是需要足够的列。

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 !我如何看到所有的列,最好是默认的

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

options(dplyr.width = Inf)

我写在这里了

你可能喜欢glimpse:

> 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

dplyrdplyr对象有自己的打印功能。在本例中,操作的结果对象是tbl_df。匹配的打印函数是dplyr:::print.tbl_df。这表明trunc_mat是负责打印内容的函数,而不是负责打印内容的函数,包括哪些变量。

遗憾的是,dplyr:::print.tbl_df没有将任何参数传递给trunc_mat, trunc_mat也不支持选择显示哪些变量(只有多少行)。一种解决方法是将dplyr的结果强制转换为data.frame,并使用head:

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) %>%
    as.data.frame %>%
    head

movies %.% group_by(year) %.% ....... %.% print.default

dplyr使用dplyr:::print.tbl_df来代替默认的打印选项,以确保您的屏幕不会因大量数据集而过载。当你最终把你的东西精简到你想要的,不想再从你自己的错误中被拯救的时候,只要把print.default放在最后,把所有的东西都吐出来。


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

相关内容

  • 没有找到相关文章

最新更新