dplyr

http://genomicsclass.github.io/book/pages/dplyr_tutorial.html

Table of Contents

msleep
                        name      genus  vore        order conservation
1                    Cheetah   Acinonyx carni    Carnivora           lc
2                 Owl monkey      Aotus  omni     Primates         <NA>
3            Mountain beaver Aplodontia herbi     Rodentia           nt
4 Greater short-tailed shrew    Blarina  omni Soricomorpha           lc
5                        Cow        Bos herbi Artiodactyla domesticated
  sleep_total sleep_rem sleep_cycle awake brainwt  bodywt
1        12.1        NA          NA  11.9      NA  50.000
2        17.0       1.8          NA   7.0 0.01550   0.480
3        14.4       2.4          NA   9.6      NA   1.350
4        14.9       2.3   0.1333333   9.1 0.00029   0.019
5         4.0       0.7   0.6666667  20.0 0.42300 600.000

dplyr::select() reference

select(msleep, name, sleep_total)
                        name sleep_total
1                    Cheetah        12.1
2                 Owl monkey        17.0
3            Mountain beaver        14.4
4 Greater short-tailed shrew        14.9
5                        Cow         4.0
select(msleep, -name)
       genus  vore        order conservation sleep_total sleep_rem sleep_cycle
1   Acinonyx carni    Carnivora           lc        12.1        NA          NA
2      Aotus  omni     Primates         <NA>        17.0       1.8          NA
3 Aplodontia herbi     Rodentia           nt        14.4       2.4          NA
4    Blarina  omni Soricomorpha           lc        14.9       2.3   0.1333333
5        Bos herbi Artiodactyla domesticated         4.0       0.7   0.6666667
  awake brainwt  bodywt
1  11.9      NA  50.000
2   7.0 0.01550   0.480
3   9.6      NA   1.350
4   9.1 0.00029   0.019
5  20.0 0.42300 600.000
select(msleep, name:order)
                        name      genus  vore        order
1                    Cheetah   Acinonyx carni    Carnivora
2                 Owl monkey      Aotus  omni     Primates
3            Mountain beaver Aplodontia herbi     Rodentia
4 Greater short-tailed shrew    Blarina  omni Soricomorpha
5                        Cow        Bos herbi Artiodactyla
Also endswith(), contains(), matches(), one_of() are available
  sleep_total sleep_rem sleep_cycle
1        12.1        NA          NA
2        17.0       1.8          NA
3        14.4       2.4          NA
4        14.9       2.3   0.1333333
5         4.0       0.7   0.6666667

dplyr::filter() reference

filter(msleep, sleep_total >= 16)
        name genus vore    order conservation sleep_total sleep_rem sleep_cycle
1 Owl monkey Aotus omni Primates         <NA>          17       1.8          NA
  awake brainwt bodywt
1     7  0.0155   0.48
Multiple conditions are joined with and.
 [1] name         genus        vore         order        conservation
 [6] sleep_total  sleep_rem    sleep_cycle  awake        brainwt     
[11] bodywt      
<0 rows> (or 0-length row.names)

dplyr::mutate() reference

msleep %>% 
  mutate(rem_proportion = sleep_rem / sleep_total) %>%
  head
                        name      genus  vore        order conservation
1                    Cheetah   Acinonyx carni    Carnivora           lc
2                 Owl monkey      Aotus  omni     Primates         <NA>
3            Mountain beaver Aplodontia herbi     Rodentia           nt
4 Greater short-tailed shrew    Blarina  omni Soricomorpha           lc
5                        Cow        Bos herbi Artiodactyla domesticated
  sleep_total sleep_rem sleep_cycle awake brainwt  bodywt rem_proportion
1        12.1        NA          NA  11.9      NA  50.000             NA
2        17.0       1.8          NA   7.0 0.01550   0.480      0.1058824
3        14.4       2.4          NA   9.6      NA   1.350      0.1666667
4        14.9       2.3   0.1333333   9.1 0.00029   0.019      0.1543624
5         4.0       0.7   0.6666667  20.0 0.42300 600.000      0.1750000

dplyr::transmute() reference

Similar to mutate() but it keeps only the variables you create

msleep %>% 
  transmute(rem_proportion = sleep_rem / sleep_total) %>%
  head
  rem_proportion
1             NA
2      0.1058824
3      0.1666667
4      0.1543624
5      0.1750000

dplyr::%>% reference

msleep %>% 
    select(name, sleep_total) %>% 
    head
                        name sleep_total
1                    Cheetah        12.1
2                 Owl monkey        17.0
3            Mountain beaver        14.4
4 Greater short-tailed shrew        14.9
5                        Cow         4.0