美文网首页
R语言笔记Day1(七 dplyr包)

R语言笔记Day1(七 dplyr包)

作者: 养猪场小老板 | 来源:发表于2020-01-14 17:40 被阅读0次

    1、dplyr 基础包

    dplyr包函数 函数名称 说明
    函数1 filter() 按值筛选观测
    函数2 arrange() 对行进行重新排序
    函数3 select() 按名称选取列变量
    函数4 mutate() 使用现有变量的函数创建新变量
    函数5 summarize() 将多个值总结为一个摘要统计量
    函数6 group_by() 以上函数与group_by()函数连用
    group_by()可以改变函数的作用范围;让其从整个数据集上的操作变为在每个分组上的分别操作 group_by()

    1.1 数据准备

    • nycflights13::flights, 2013年从纽约出发的336 776次航班的信息
    > install.packages("nycflights13")
    > library(dplyr)
    > library(nycflights13)
    > head(flights)
    # A tibble: 6 x 19
       year month   day dep_time sched_dep_time dep_delay arr_time
      <int> <int> <int>    <int>          <int>     <dbl>    <int>
    1  2013     1     1      517            515         2      830
    2  2013     1     1      533            529         4      850
    3  2013     1     1      542            540         2      923
    4  2013     1     1      544            545        -1     1004
    5  2013     1     1      554            600        -6      812
    6  2013     1     1      554            558        -4      740
    # … with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,....
    

    2.1 filter()筛选行

    • 格式:filter(.data, ...)
    • .data是数据框;
    • ...:后面可以是一个或多个参数,但必须是逻辑值,从而保证为TRUE的行才保留。
    筛选出1月1号的数据
    > flights%>% filter(month==1,day==1)
    # A tibble: 842 x 19
        year month   day dep_time sched_dep_time dep_delay arr_time
       <int> <int> <int>    <int>          <int>     <dbl>    <int>
     1  2013     1     1      517            515         2      830
     2  2013     1     1      533            529         4      850
     3  2013     1     1      542            540         2      923
     4  2013     1     1      544            545        -1     1004
     5  2013     1     1      554            600        -6      812
     6  2013     1     1      554            558        -4      740
     7  2013     1     1      555            600        -5      913
     8  2013     1     1      557            600        -3      709
     9  2013     1     1      557            600        -3      838
    10  2013     1     1      558            600        -2      753
    # … with 832 more rows, and 12 more variables: sched_arr_time <int>,
    
    • 比较运算符 >, >=, <, <=, !=, ==

    • 逻辑运算符
      & 表示“与”, | 表示“或”, !表示“非”, xor(x,y) 表示“异或”


    2.2 arrange()排列行

    • 格式 arrange(.data, ...)
    • .data:数据框
    • ...:变量名,多个变量名使用逗号隔开; arrange()将根据这些列对数据框进行排序。
    > arrange(flights,year,month,day)
    # A tibble: 336,776 x 19
        year month   day dep_time sched_dep_time dep_delay arr_time
       <int> <int> <int>    <int>          <int>     <dbl>    <int>
     1  2013     1     1      517            515         2      830
     2  2013     1     1      533            529         4      850
     3  2013     1     1      542            540         2      923
     4  2013     1     1      544            545        -1     1004
     5  2013     1     1      554            600        -6      812
     6  2013     1     1      554            558        -4      740
     7  2013     1     1      555            600        -5      913
     8  2013     1     1      557            600        -3      709
     9  2013     1     1      557            600        -3      838
    10  2013     1     1      558            600        -2      753
    # … with 336,766 more rows, and 12 more variables:....
    > arrange(flights,desc(arr_delay))#降序排列desc()函数
    # A tibble: 336,776 x 19
        year month   day dep_time sched_dep_time dep_delay arr_time
       <int> <int> <int>    <int>          <int>     <dbl>    <int>
     1  2013     1     9      641            900      1301     1242
     2  2013     6    15     1432           1935      1137     1607
     3  2013     1    10     1121           1635      1126     1239
     4  2013     9    20     1139           1845      1014     1457
     5  2013     7    22      845           1600      1005     1044
     6  2013     4    10     1100           1900       960     1342
     7  2013     3    17     2321            810       911      135
     8  2013     7    22     2257            759       898      121
     9  2013    12     5      756           1700       896     1058
    10  2013     5     3     1133           2055       878     1250
    # … with 336,766 more rows, and 12 more variables:
    

    2.3 select()选择列

    • select(.data, ...):keeps only the variables you mention
    > select(flights,year,month,day)#还可以写成 select(flights,year:day)
    # A tibble: 336,776 x 3
        year month   day
       <int> <int> <int>
     1  2013     1     1
     2  2013     1     1
     3  2013     1     1
     4  2013     1     1
     5  2013     1     1
     6  2013     1     1
     7  2013     1     1
     8  2013     1     1
     9  2013     1     1
    10  2013     1     1
    # … with 336,766 more rows
    > select(flights,-(year:day))  #删除列
    # A tibble: 336,776 x 16
       dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay
          <int>          <int>     <dbl>    <int>          <int>     <dbl>
     1      517            515         2      830            819        11
     2      533            529         4      850            830        20
     3      542            540         2      923            850        33
     4      544            545        -1     1004           1022       -18
     5      554            600        -6      812            837       -25
     6      554            558        -4      740            728        12
     7      555            600        -5      913            854        19
     8      557            600        -3      709            723       -14
     9      557            600        -3      838            846        -8
    10      558            600        -2      753            745         8
    # … with 336,766 more rows, and 10 more variables: carrier <chr>,
    
    • 小技巧

    select()函数可以结合一些辅助函数使用:
    1)start_with("abc"): 匹配“abc”开头
    2)ends_with(): 匹配“xyz” 结尾的名称
    3)contains("ijk"): 匹配包含“ijk”的名称
    4)mathces("(.)\1"): 匹配名称中有重复字符的变量;(.)匹配单个字符;\1指定第一个子匹配项, 第二个匹配模式也是(.)。
    5)num_range("x",1:3): 匹配x1、x2和x3
    6)rename(): 可以对数据框中变量名重命名


    2.4 使用mutate添加新变量

    • mutate()总是将新列添加在数据集的最后
    > head(mutate(iris,newCol1=iris$Sepal.Length*3))
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species newCol1
    1          5.1         3.5          1.4         0.2  setosa    15.3
    2          4.9         3.0          1.4         0.2  setosa    14.7
    3          4.7         3.2          1.3         0.2  setosa    14.1
    4          4.6         3.1          1.5         0.2  setosa    13.8
    5          5.0         3.6          1.4         0.2  setosa    15.0
    6          5.4         3.9          1.7         0.4  setosa    16.2
    

    2.5 transmute()只保留新变量

    > head(transmute(iris,newCol1=iris$Sepal.Length*4))
      newCol1
    1    20.4
    2    19.6
    3    18.8
    4    18.4
    5    20.0
    6    21.6
    

    2.6 summarize()进行分组摘要

    >  bySpecies<-group_by(iris,Species)
    >  summarize(bySpecies, meanCol=mean(iris$Sepal.Length,na.rm = T))
    # A tibble: 3 x 2
      Species    meanCol
      <fct>        <dbl>
    1 setosa        5.84
    2 versicolor    5.84
    3 virginica     5.84
    #by_day<-group_by(flights,year,month,day)  #flights中year,month,day各水平一一组合
    > by_day<-group_by(flights,day)
    > summarise(by_day,meanCol2=mean(flights$day))
    # A tibble: 31 x 2
         day meanCol2
       <int>    <dbl>
     1     1     15.7
     2     2     15.7
     3     3     15.7
     4     4     15.7
     5     5     15.7
     6     6     15.7
     7     7     15.7
     8     8     15.7
     9     9     15.7
    10    10     15.7
    # … with 21 more rows
    
    • 练习6

    1. 将iris数据框的前4列gather,然后还原

    > install.packages("tidyr")
    > library(tidyr)
    > x=iris[,1:4]
    > y=x %>% gather(lengthwidth,exp,1:4)
    > head(y)
       lengthwidth exp
    1 Sepal.Length 5.1
    2 Sepal.Length 4.9
    3 Sepal.Length 4.7
    4 Sepal.Length 4.6
    5 Sepal.Length 5.0
    6 Sepal.Length 5.4
    

    2.将第三列分成两列(以小数点为分隔符)然后合并

    > head(iris)
      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1          5.1         3.5          1.4         0.2  setosa
    2          4.9         3.0          1.4         0.2  setosa
    3          4.7         3.2          1.3         0.2  setosa
    4          4.6         3.1          1.5         0.2  setosa
    5          5.0         3.6          1.4         0.2  setosa
    6          5.4         3.9          1.7         0.4  setosa
    > x1 <- iris %>% separate(Petal.Length,c("zhengshu","xiaoshu"),sep = ".")
    Warning message:
    Expected 2 pieces. Additional pieces discarded in 137 rows [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...]. 
    > x1#并没有出现拆分的数字,怎么解决?试一下\\.
        Sepal.Length Sepal.Width zhengshu xiaoshu Petal.Width    Species
    1            5.1         3.5                          0.2     setosa
    2            4.9         3.0                          0.2     setosa
    3            4.7         3.2                          0.2     setosa
    4            4.6         3.1                          0.2     setosa
    5            5.0         3.6                          0.2     setosa
    > x2 <- iris %>% separate(Petal.Length,c("zhengshu","xiaoshu"),sep = "\\.",remove = TRUE)#\\.
    > x2#结果中出现NA,怎么解决?
        Sepal.Length Sepal.Width zhengshu xiaoshu Petal.Width    Species
    1            5.1         3.5        1       4         0.2     setosa
    #...此处省略
    21           5.4         3.4        1       7         0.2     setosa
    22           5.1         3.7        1       5         0.4     setosa
    23           4.6         3.6        1    <NA>         0.2     setosa
    #...此处省略
    53           6.9         3.1        4       9         1.5 versicolor
    54           5.5         2.3        4    <NA>         1.3 versicolor
    55           6.5         2.8        4       6         1.5 versicolor
    #...此处省略
    62           5.9         3.0        4       2         1.5 versicolor
    63           6.0         2.2        4    <NA>         1.0 versicolor
    64           6.1         2.9        4       7         1.4 versicolor
    #这个时候想到缺失值的处理中replace_na()函数
    ~~~R
    > x3 <- replace_na(x2,replace = data.frame(xiaoshu=0))
    > x3
        Sepal.Length Sepal.Width zhengshu xiaoshu Petal.Width    Species
    1            5.1         3.5        1       4         0.2     setosa
    #...此处省略
    21           5.4         3.4        1       7         0.2     setosa
    22           5.1         3.7        1       5         0.4     setosa
    23           4.6         3.6        1       0         0.2     setosa
    #...此处省略
    53           6.9         3.1        4       9         1.5 versicolor
    54           5.5         2.3        4       0         1.3 versicolor
    55           6.5         2.8        4       6         1.5 versicolor
    #...此处省略
    62           5.9         3.0        4       2         1.5 versicolor
    63           6.0         2.2        4       0         1.0 versicolor
    64           6.1         2.9        4       7         1.4 versicolor
    

    3.加载test1.Rdata,将deg数据框按照pvalue从小到大排序

    4. 将两个数据框按照probe_id列连接在一起

    merge() 根据某一相同列合并

    相关文章

      网友评论

          本文标题:R语言笔记Day1(七 dplyr包)

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