小洁详解《R数据科学》--第七章 tibble

作者: 小洁忘了怎么分身 | 来源:发表于2018-09-28 12:21 被阅读136次

    小洁写于2018.9.28。 非常凑巧打开后台看到正好600人,不知道是不是要放假了的缘故,我怎么精力不太集中呢。

    英文版书籍地址:http://r4ds.had.co.nz/
    中文版第7章对应英文版第10章。

    1.简介

    data.frame: R内置的传统数据框
    tibble:简单数据框,用tidyverse中的tibble包实现。

    library(tidyverse)
    

    2.创建tibble

    (1)将dataframe转换为tibble:

    as_tibble(iris)
    

    (2)tibble()新建tibble

    tibble(
      x = 1:5,
      y = 1,
      z = x ^ 2 + y
    )
    
    引用无效变量名-用反引号括起来
    tb <- tibble(
      `:)` = "smile",
      ` ` = "space",
      `2000` = "number"
    )
    tb
    

    (3)tribble()列标题以~开头,数据条目以逗号分割

    tribble(
      ~x, ~y, ~z,
      "a", 2, 3.6,
      "b", 1, 8.5
    )
    

    3.对比tibble与data.frame

    (1)打印

    tb2 <- tibble(
      a = lubridate::now() + runif(1e3) * 86400,
      b = lubridate::today() + runif(1e3) * 30,
      c = 1:1e3,
      d = runif(1e3),
      e = sample(letters, 1e3, replace = TRUE)
    )
    

    新建了一个5列1000行的tibble(1e3的运算结果是1000)

    默认:

    只显示前十行,列显示情况适配屏幕,显示列数据类型。

    自定义调整:
    ①print()
    nycflights13::flights %>%
      print(n = 10, width = Inf)
    #width = Inf输出所有列
    
    ② options
    options(tibble.print_max = 1001, tibble.print_min = 7) #中文版此处有误
    #如果多于m 行,则只打印出n行
    options(tibble.print_min = Inf) #打印所有行
    options(tibble.width = Inf) #打印所有列
    tb2
    
    ③ 查看整个数据集
    class(nycflights13::flights)
    nycflights13::flights %>%
      View()
    

    3.2取子集

    df <- tibble(
      x = runif(5),
      y = rnorm(5)
    )
    
    df$x # 按名称提取
    df[["x"]]# 按名称提取
    df[[1]] # 按位置提取
    
    #要想在管道中使用这些提取操作,需要使用特殊的占位符.(英文句点):
    df %>% .$x
    df %>% .[["x"]]
    df %>% .$x
    

    4.与旧代码进行交互

    as.data.frame(tb) #将tibble 转换回data.frame
    class(as.data.frame(tb)) #查看转换后的类型

    5.练习

    (1) 如何识别一个对象是否为tibble ?

    用is.tibble()
    is.tibble(mtcars)
    is.tibble(as.tibble(mtcars))
    class(as.tibble(mtcars))
    #[1] "tbl_df"     "tbl"        "data.frame"
    

    (2) 对比在data.frame 和等价的tibble 上进行的以下操作。有何区别?为什么默认的数据框操作会让人感到沮丧?

    df <- data.frame(abc = 1, xyz = "a")
    df$x (#列名部分匹配也会被输出)
    df[, "xyz"]
    df[, c("abc", "xyz")]
    
    tb <- as.tibble(df)
    tb$x (#列名不完全匹配则报错)
    tb[, "xyz"]
    tb[, c("abc", "xyz")]
    

    (3) 如果将一个变量的名称保存在一个对象中,如var <- "mpg",如何从tibble 中提取出这个变量?
    var <- "mpg"
    用df[[var]],不能用$。
    (4) 在以下的数据框中练习如何引用不符合语法规则的变量名。

    annoying <- tibble(
      `1` = 1:10,
      `2` = `1` * 2 + rnorm(length(`1`))
    )
    

    a. 提取名称为1 的变量。

    annoying$`1`
    annoying[["1"]]
    

    b. 绘制表示变量1 和变量2 关系的散点图。

    plot(annoying$`1`,annoying$`2`)
    ggplot(annoying, aes(x = `1`, y = `2`)) +
      geom_point()
    

    c. 创建一个名称为3 的新列,其值为列2 除以列1。

    annoying[["3"]] <- annoying$`2` / annoying$`1`
    annoying[["3"]] <- annoying[["2"]] / annoying[["1"]]
    annoying <- tibble(
      `1` = 1:10,
      `2` = `1` * 2 + rnorm(length(`1`)),
      `3` =`2`/`1`
    )
    

    d. 将这些列重新命名为one、two 和three。

    annoying <- rename(annoying, one = `1`, two = `2`, three = `3`)
    glimpse(annoying)
    
    names(annoying)=c("one","two","three")
    

    (5) tibble::enframe() 函数的功能是什么?什么时候可以使用这个函数?

    ?enframe #查看帮助文档
    #enframe() converts named atomic vectors or lists to two-column data frames. #For unnamed vectors, the natural sequence is used as name column.
    #即将命名向量转换为具有名称和值的数据框
    enframe(c(a = 5, b = 7))
    

    (6) 哪个选项控制在tibble 底部打印的额外列名称的数量?

    ?print.tbl_df  #查看帮助文档
    #n_extra
    #Number of extra columns to print abbreviated information for, if the width is #too small for the entire tibble. If NULL, the default, will print information about #at most tibble.max_extra_cols extra columns.
    

    友情链接:
    生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
    B站链接:https://m.bilibili.com/space/338686099
    YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists
    生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA
    学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw

    相关文章

      网友评论

        本文标题:小洁详解《R数据科学》--第七章 tibble

        本文链接:https://www.haomeiwen.com/subject/tpnyoftx.html