R语言

R | R入门の想法

2021-10-31  本文已影响0人  shwzhao

之前一直不知道该如何学R,一直用awk等命令进行数据处理。

最开始看的《R语言实战》,了解了工作目录,数据类型,数据结构等;

后来看《R数据科学》(网上可搜中文版pdf,英文网页版);

练习题,搜到了张敬信老师的 玩转数据处理120题之P1-P20(R语言tidyverse版本),有更新版,《R语言编程—基于tidyverse》新书信息汇总

教程?网上搜tidyversereadrtibbledplyrstringrtidyrpurrrforcats......应有尽有!


数据处理,学好tidyverse的几个核心包的核心函数,应该就差不多了吧 ~ ~ ~


补充:老板让学的 quick-R


补充:tidyverse的几个核心包的主要函数

> library(tidyverse)
-- Attaching packages ----------------------- tidyverse 1.3.1 --
√ ggplot2 3.3.5     √ purrr   0.3.4
√ tibble  3.1.5     √ dplyr   1.0.7
√ tidyr   1.1.4     √ stringr 1.4.0
√ readr   2.0.2     √ forcats 0.5.1
-- Conflicts -------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()

1. tibble

data.framedata.tabletibble

library(tibble)
# library(tidyverse)
df <- tibble(
  `>` = 1:5,
  `100` = 1,
  a = c("one", "two", "three", "four", "five"),
  `:)` = `>` * `100` + str_length(a),
)
df

# # A tibble: 5 x 4
#     `>` `100` a      `:)`
#   <int> <dbl> <chr> <dbl>
# 1     1     1 one       4
# 2     2     1 two       5
# 3     3     1 three     8
# 4     4     1 four      8
# 5     5     1 five      9
> tribble(
  ~`<`, ~`100`,
  "a", 1,
  "b", 1,
)

# # A tibble: 2 x 2
#   `<`   `100`
#   <chr> <dbl>
# 1 a         1
# 2 b         1
> df$`100` # 特殊字符,加反引号
[1] 1 1 1 1 1
> df[[">"]] # 虽然是特殊字符,但仍加双引号
[1] 1 2 3 4 5
> mpg %>% colnames() # tibble不换行显示,所以会显示不全,要输出tibble行名,可以直接使用基础函数 colnames()
 [1] "manufacturer" "model"        "displ"        "year"         "cyl"
 [6] "trans"        "drv"          "cty"          "hwy"          "fl"
[11] "class"
> mtcars %>% has_rownames()
[1] TRUE
> mtcars %>% remove_rownames() %>% has_rownames()
[1] FALSE
> mtcars %>% rownames_to_column("car") %>% head()
                car  mpg cyl disp  hp drat    wt  qsec vs am gear carb
1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

http://blog.fens.me/r-tibble/

2. readr

R基础包 read.csv()read.tsv()read.delim()
readr包,更快,生成tibble,不使用行名称......

library(readr)
# library(tidyverse)

3. dplyr

3.1 汇总操作

> mpg %>% group_by(manufacturer,model) %>% count()
# A tibble: 38 × 3
# Groups:   manufacturer, model [38]
   manufacturer model                  n
   <chr>        <chr>              <int>
 1 audi         a4                     7
 2 audi         a4 quattro             8
 3 audi         a6 quattro             3
 4 chevrolet    c1500 suburban 2wd     5
 5 chevrolet    corvette               5
 6 chevrolet    k1500 tahoe 4wd        4
 7 chevrolet    malibu                 5
 8 dodge        caravan 2wd           11
 9 dodge        dakota pickup 4wd      9
10 dodge        durango 4wd            7
# … with 28 more rows
> mtcars %>% select(cyl, vs) %>% add_count(cyl, sort=TRUE)  # `sort`: 等于`TRUE`时,按次数多少从大到小排序;mtcars %>% add_count(cyl) 用于添加一列次数
# A tibble: 32 x 3
     cyl    vs     n
   <dbl> <dbl> <int>
 1     8     0    14
 2     8     0    14
 3     8     0    14
 4     8     0    14
 5     8     0    14
 6     8     0    14
 7     8     0    14
 8     8     0    14
 9     8     0    14
10     8     0    14
# ... with 22 more rows
> mtcars %>% group_by(cyl) %>% summarise(count=n(),mean_disp=mean(disp))
# A tibble: 3 x 3
    cyl count mean_disp
  <dbl> <int>     <dbl>
1     4    11      105.
2     6     7      183.
3     8    14      353.

base包的几个累积计算函数,dplyr扩充了几个。

> tibble(a=1:5, b=6:10) %>%
  pivot_longer(everything(), names_to="Type",values_to="Num") %>%
  arrange(Type) %>%
  group_by(Type) %>%
  mutate(Cumsum=cumsum(Num))
# A tibble: 10 × 3
# Groups:   Type [2]
   Type    Num Cumsum
   <chr> <int>  <int>
 1 a         1      1
 2 a         2      3
 3 a         3      6
 4 a         4     10
 5 a         5     15
 6 b         6      6
 7 b         7     13
 8 b         8     21
 9 b         9     30
10 b        10     40

3.2 行操作

> a=c("audi","dodge","honda")
> mpg %>% filter(manufacturer %in% a) %>% group_by(manufacturer) %>% count()
# A tibble: 3 × 2
# Groups:   manufacturer [3]
  manufacturer     n
  <chr>        <int>
1 audi            18
2 dodge           37
3 honda            9
> mpg %>% select(manufacturer) %>% distinct()
# A tibble: 15 × 1
   manufacturer
   <chr>
 1 audi
 2 chevrolet
 3 dodge
 4 ford
 5 honda
 6 hyundai
 7 jeep
 8 land rover
 9 lincoln
10 mercury
11 nissan
12 pontiac
13 subaru
14 toyota
15 volkswagen
> mpg %>% select(manufacturer) %>% distinct() %>% slice(c(1,1,3,5)) %>% .[[1]]
[1] "audi"  "audi"  "dodge" "honda"
> mpg %>% select(manufacturer) %>% distinct() %>% sample_n(3,replace = F) %>% .[[1]]
[1] "audi"   "toyota" "dodge"
> mpg %>% slice_sample()
# A tibble: 1 × 11
  manufacturer model      displ  year   cyl trans  drv     cty   hwy fl    class
  <chr>        <chr>      <dbl> <int> <int> <chr>  <chr> <int> <int> <chr> <chr>
1 ford         f150 pick…   4.2  1999     6 auto(… 4        14    17 r     pick…

3.3 列操作

> mpg %>% pull(year) %>% unique() # 基础函数unique(),用于向量去重
[1] 1999 2008
> mpg %>% select((!starts_with(c("m","d","y")) | matches("m.*er")))
# A tibble: 234 × 7
     cyl trans        cty   hwy fl    class   manufacturer
   <int> <chr>      <int> <int> <chr> <chr>   <chr>
 1     4 auto(l5)      18    29 p     compact audi
 2     4 manual(m5)    21    29 p     compact audi
 3     4 manual(m6)    20    31 p     compact audi
 4     4 auto(av)      21    30 p     compact audi
 5     6 auto(l5)      16    26 p     compact audi
 6     6 manual(m5)    18    26 p     compact audi
 7     6 auto(av)      18    27 p     compact audi
 8     4 manual(m5)    18    26 p     compact audi
 9     4 auto(l5)      16    25 p     compact audi
10     4 manual(m6)    20    28 p     compact audi
# … with 224 more rows
> tibble(a=1:5, b=6:10) %>% mutate(If2=if_else(b/a==2,"T","F"))
# A tibble: 5 × 3
      a     b If2
  <int> <int> <chr>
1     1     6 F
2     2     7 F
3     3     8 F
4     4     9 F
5     5    10 T
> mpg %>% transmute(mm=str_c(manufacturer, "__", model))
# A tibble: 234 × 1
   mm
   <chr>
 1 audi__a4
 2 audi__a4
 3 audi__a4
 4 audi__a4
 5 audi__a4
 6 audi__a4
 7 audi__a4
 8 audi__a4 quattro
 9 audi__a4 quattro
10 audi__a4 quattro
# … with 224 more rows

3.4 多个数据框操作

by = c()

4. tidyr

自我认为pivot_wider()pivot_longer()tidyr最重要的两个函数,其他的函数可以配合使用dplyrstringr达到相同的效果。

> mpg %>% group_by(model, year) %>% count() %>% 
  pivot_wider(id_cols=model, names_from=year, values_from=n, names_prefix = "year_")
# A tibble: 38 × 3
# Groups:   model [38]
   model              year_1999 year_2008
   <chr>                  <int>     <int>
 1 4runner 4wd                4         2
 2 a4                         4         3
 3 a4 quattro                 4         4
 4 a6 quattro                 1         2
 5 altima                     2         4
 6 c1500 suburban 2wd         1         4
 7 camry                      4         3
 8 camry solara               4         3
 9 caravan 2wd                6         5
10 civic                      5         4
# … with 28 more rows
> mpg %>% group_by(model, year) %>% count() %>% 
  pivot_wider(id_cols=model, names_from=year, values_from=n, names_prefix = "year_") %>% 
  pivot_longer(-model, names_to="year",values_to="count")
# A tibble: 76 × 3
# Groups:   model [38]
   model       year      count
   <chr>       <chr>     <int>
 1 4runner 4wd year_1999     4
 2 4runner 4wd year_2008     2
 3 a4          year_1999     4
 4 a4          year_2008     3
 5 a4 quattro  year_1999     4
 6 a4 quattro  year_2008     4
 7 a6 quattro  year_1999     1
 8 a6 quattro  year_2008     2
 9 altima      year_1999     2
10 altima      year_2008     4
# … with 66 more rows
> mtcars %>% expand(cyl)
# A tibble: 3 × 1
    cyl
  <dbl>
1     4
2     6
3     8
> mtcars %>% expand(cyl, vs)
# A tibble: 6 × 2
    cyl    vs
  <dbl> <dbl>
1     4     0
2     4     1
3     6     0
4     6     1
5     8     0
6     8     1

5. stringr

5.1 检测匹配

> mpg %>%
  mutate(newcol=if_else(str_detect(trans, "auto"), "A", "M")) %>%
  group_by(newcol) %>%
  count()
# A tibble: 2 × 2
# Groups:   newcol [2]
  newcol     n
  <chr>  <int>
1 A        157
2 M         77

5.2 字符提取


5.3 字符长度

5.4 字符替换

5.5 组合切割

> mpg %>%
  mutate(a=str_c(manufacturer, model, sep="-")) %>%
  select(a,everything())
# A tibble: 234 × 12
   a        manufacturer model  displ  year   cyl trans  drv     cty   hwy fl
   <chr>    <chr>        <chr>  <dbl> <int> <int> <chr>  <chr> <int> <int> <chr>
 1 audi-a4  audi         a4       1.8  1999     4 auto(… f        18    29 p
 2 audi-a4  audi         a4       1.8  1999     4 manua… f        21    29 p
 3 audi-a4  audi         a4       2    2008     4 manua… f        20    31 p
 4 audi-a4  audi         a4       2    2008     4 auto(… f        21    30 p
 5 audi-a4  audi         a4       2.8  1999     6 auto(… f        16    26 p
 6 audi-a4  audi         a4       2.8  1999     6 manua… f        18    26 p
 7 audi-a4  audi         a4       3.1  2008     6 auto(… f        18    27 p
 8 audi-a4… audi         a4 qu…   1.8  1999     4 manua… 4        18    26 p
 9 audi-a4… audi         a4 qu…   1.8  1999     4 auto(… 4        16    25 p
10 audi-a4… audi         a4 qu…   2    2008     4 manua… 4        20    28 p
# … with 224 more rows, and 1 more variable: class <chr>

感觉下面几个函数对于数据框的操作没有太大的帮助

5.6 字符排序

6. forcats

6.1 base包中关于因子的函数

> qq <- c("d", "g", "a", "e", "f", "a", "e")
> qq <- factor(qq)

6.2 查看

6.3 重新排序

6.4 改变levels值

6.6 添加或删除levels

7. purrr

刚看了张金龙老师的R语言视频课(20220302),他说他目前为止还没用过这个功能。
我平时也就是简单地整理表格数据,purrr更适用于list,我好像也用不到......,等以后接触到再学吧。

上一篇 下一篇

猜你喜欢

热点阅读