简体   繁体   English

as_tibble()无法正常工作

[英]as_tibble() not working as expected

I am attempting the exercise in R for data science (7.5.2.1, #2): Use geom_tile() together with dplyr to explore how average flight delays vary by destination and month of year. 我正在尝试进行R的数据科学练习(7.5.2.1,#2): 将geom_tile()与dplyr一起使用,以探索平均航班延误如何随目的地和一年中的月份而变化。 What makes the plot difficult to read? 是什么使剧情难以阅读? How could you improve it? 您如何改善它?

First, transmute columns. 首先,转换列。

library(nycflights13)
foo <- nycflights13::flights %>%
  transmute(tot_delay = dep_delay + arr_delay, m = month, d = dest) %>%
  filter(!is.na(tot_delay)) %>%
  group_by(m, d) %>%
  summarise(avg_delay = mean(tot_delay))

Now foo appears to be a data frame based on the 'Source' output. 现在, foo似乎是基于“源”输出的数据帧。

> foo
Source: local data frame [1,112 x 3]
Groups: m [?]

       m     d avg_delay
   <int> <chr>     <dbl>
1      1   ALB 76.571429
2      1   ATL  8.567982
3      1   AUS 19.017751
4      1   AVL 49.000000
5      1   BDL 32.081081
6      1   BHM 47.043478
7      1   BNA 25.930233
8      1   BOS  2.698517
9      1   BQN  8.516129
10     1   BTV 18.393665
# ... with 1,102 more rows

It doesn't appear that as_tibble is working, what could I be doing wrong? 似乎as_tibble无效 ,我可能做错了什么?

> as_tibble(foo)
Source: local data frame [1,112 x 3]
Groups: m [?]

       m     d avg_delay
   <int> <chr>     <dbl>
1      1   ALB 76.571429
2      1   ATL  8.567982
3      1   AUS 19.017751
4      1   AVL 49.000000
5      1   BDL 32.081081
6      1   BHM 47.043478
7      1   BNA 25.930233
8      1   BOS  2.698517
9      1   BQN  8.516129
10     1   BTV 18.393665
# ... with 1,102 more rows

Shouldn't the internals be different for a tibble? 小玩意儿内部不应该有所不同吗?

> str(foo)
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 1112 obs. of  3 variables:
 $ m        : int  1 1 1 1 1 1 1 1 1 1 ...
 $ d        : chr  "ALB" "ATL" "AUS" "AVL" ...
 $ avg_delay: num  76.57 8.57 19.02 49 32.08 ...
 - attr(*, "vars")=List of 1
  ..$ : symbol m
 - attr(*, "drop")= logi TRUE
> str(as_tibble(foo))
Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 1112 obs. of  3 variables:
 $ m        : int  1 1 1 1 1 1 1 1 1 1 ...
 $ d        : chr  "ALB" "ATL" "AUS" "AVL" ...
 $ avg_delay: num  76.57 8.57 19.02 49 32.08 ...
 - attr(*, "vars")=List of 1
  ..$ : symbol m
 - attr(*, "drop")= logi TRUE

Note that as_tibble() works as expected 请注意,as_tibble()可以正常工作

> packageDescription("tibble")
Package: tibble
Encoding: UTF-8
Version: 1.3.0

> is_tibble(foo)
[1] TRUE

Works for me - foo is a "tibble" and is announced as "A tibble: 112 x 3" in the print: 对我的作品- foo是一个“tibble”,并宣布为“A tibble:112×3”在打印:

> foo
Source: local data frame [1,112 x 3]
Groups: m [?]

# A tibble: 1,112 x 3
       m     d avg_delay
   <int> <chr>     <dbl>
 1     1   ALB 76.571429
 2     1   ATL  8.567982

So you possibly have an old version of dplyr. 因此,您可能拥有dplyr的旧版本。 Mine is: 我的是:

> packageDescription("dplyr")
Package: dplyr
Type: Package
Version: 0.5.0

And everything else: 还有其他一切:

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.4 LTS

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.5.0  tibble_1.3.1

loaded via a namespace (and not attached):
[1] magrittr_1.5     R6_2.2.0         assertthat_0.2.0 DBI_0.5-1       
[5] tools_3.3.1      Rcpp_0.12.11     rlang_0.1.1     

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 as_tibble控制拼合级别 - as_tibble control level of flattening 避免在 dplyr 中重复 as_tibble() - Avoid repeating as_tibble() in dplyr as_tibble只返回一个变量 - as_tibble only returns a single variable 为什么as_tibble()round浮动到最接近的整数? - Why does as_tibble() round floats to the nearest integer? 如何在 R 中将 as_tibble() 格式的时间序列数据转换为 as_tsibble() 格式? - How to transform an as_tibble() format time series data into an as_tsibble() format in R? 带有Apache箭头的Sparklyr R失败,意外终止:未找到对象&#39;as_tibble&#39; - Sparklyr R with apache arrow fails, terminated unexpectedly: object 'as_tibble' not found 为什么 as_tibble(mtcars,rownames = NA) 不显示行名? 文档说它应该 - Why does as_tibble(mtcars,rownames = NA) not show the row names? The documentation says that it should 我可以控制 as_tibble 或 as.data.frame 如何处理 R 中的一维数据吗? - Can I control how as_tibble or as.data.frame treat one-dimensional data in R? 这两个类有什么区别,为什么我必须在我的代码中使用 as_tibble function? - what's the difference between these two classes and why I must use as_tibble function in my code? 如何修复R中的“错误:&#39;名称空间:dplyr&#39;未导出对象&#39;as_tibble&#39;错误:包&#39;BLANK&#39;的延迟加载失败” - How to fix “Error : object ‘as_tibble’ is not exported by 'namespace:dplyr' ERROR: lazy loading failed for package ‘BLANK’” in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM