Tutorial sobre como Seleccionar datos en R usando comandos de la librería dplyr.

Puedes seguir el tutorial por vídeo en https://youtu.be/LgemMkeLE9Y

Establezco el directorio de trabajo.

setwd("~/Expression/Expression Encoder/Output/25 Filtrar datos III")

Importo los datos.

Datos = read.table("Potato.csv", header=T, sep="," , dec=".") 

Verifico los datos cargados.

Visualizo el encabezado.

head (Datos)
##   Tratamiento Variedad Peso40 Peso41a45 Peso46a60 PesoMas61 Numero40
## 1     Testigo    Krone    380      1120     16180      2360        9
## 2     Testigo    Krone   2000      1480     15600      2340       42
## 3     Testigo    Krone    820       920     14840      6580       16
## 4     Testigo    Krone    130      1120     17180     11780        3
## 5     Testigo    Krone   1840      1240     12580      7040       22
## 6     Tratado    Krone    700       880     19120      3060       13
##   Numero41a45 Numero46a60 NumeroMas61
## 1          23         130          16
## 2          25         124          14
## 3          13         129          28
## 4          16         125          44
## 5          16         101          30
## 6          11         138          12

Compruebo la estructura.

str(Datos)
## 'data.frame':    18 obs. of  10 variables:
##  $ Tratamiento: Factor w/ 2 levels "Testigo","Tratado": 1 1 1 1 1 2 2 2 2 2 ...
##  $ Variedad   : Factor w/ 2 levels "Krone","Nicola": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Peso40     : int  380 2000 820 130 1840 700 440 1900 1300 620 ...
##  $ Peso41a45  : int  1120 1480 920 1120 1240 880 680 3620 2100 1620 ...
##  $ Peso46a60  : int  16180 15600 14840 17180 12580 19120 5880 12380 12780 12940 ...
##  $ PesoMas61  : int  2360 2340 6580 11780 7040 3060 740 620 1240 840 ...
##  $ Numero40   : int  9 42 16 3 22 13 7 37 25 12 ...
##  $ Numero41a45: int  23 25 13 16 16 11 8 38 31 22 ...
##  $ Numero46a60: int  130 124 129 125 101 138 44 107 112 113 ...
##  $ NumeroMas61: int  16 14 28 44 30 12 3 3 5 4 ...

Veo resumen de datos.

summary(Datos)
##   Tratamiento   Variedad      Peso40         Peso41a45      Peso46a60    
##  Testigo:9    Krone :10   Min.   : 130.0   Min.   : 300   Min.   : 5880  
##  Tratado:9    Nicola: 8   1st Qu.: 647.5   1st Qu.:1120   1st Qu.:12430  
##                           Median :1025.0   Median :1550   Median :13395  
##                           Mean   :1350.0   Mean   :2032   Mean   :13475  
##                           3rd Qu.:1975.0   3rd Qu.:2542   3rd Qu.:15410  
##                           Max.   :3340.0   Max.   :6720   Max.   :19120  
##    PesoMas61        Numero40      Numero41a45     Numero46a60   
##  Min.   :    0   Min.   : 3.00   Min.   : 5.00   Min.   : 44.0  
##  1st Qu.:  500   1st Qu.:12.25   1st Qu.:16.00   1st Qu.:101.0  
##  Median :  790   Median :19.50   Median :24.00   Median :112.5  
##  Mean   : 2278   Mean   :27.00   Mean   :26.22   Mean   :107.4  
##  3rd Qu.: 2355   3rd Qu.:36.25   3rd Qu.:36.25   3rd Qu.:122.5  
##  Max.   :11780   Max.   :71.00   Max.   :60.00   Max.   :138.0  
##   NumeroMas61    
##  Min.   : 0.000  
##  1st Qu.: 2.000  
##  Median : 3.500  
##  Mean   : 9.889  
##  3rd Qu.:13.500  
##  Max.   :44.000

Seleccionar datos del dataframe, usando libreria - dplyr -

Si es la primera vez que la usas, antes tendras que instalar el paquete.

#install.packages("dplyr") #O desde el menún Tools de RStudio.
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

La estructura es igual que subset.

filter (Datos, Variedad=="Krone")
##    Tratamiento Variedad Peso40 Peso41a45 Peso46a60 PesoMas61 Numero40
## 1      Testigo    Krone    380      1120     16180      2360        9
## 2      Testigo    Krone   2000      1480     15600      2340       42
## 3      Testigo    Krone    820       920     14840      6580       16
## 4      Testigo    Krone    130      1120     17180     11780        3
## 5      Testigo    Krone   1840      1240     12580      7040       22
## 6      Tratado    Krone    700       880     19120      3060       13
## 7      Tratado    Krone    440       680      5880       740        7
## 8      Tratado    Krone   1900      3620     12380       620       37
## 9      Tratado    Krone   1300      2100     12780      1240       25
## 10     Tratado    Krone    620      1620     12940       840       12
##    Numero41a45 Numero46a60 NumeroMas61
## 1           23         130          16
## 2           25         124          14
## 3           13         129          28
## 4           16         125          44
## 5           16         101          30
## 6           11         138          12
## 7            8          44           3
## 8           38         107           3
## 9           31         112           5
## 10          22         113           4

Funcion select.

select (Datos, Tratamiento, Variedad, Peso40, Peso41a45, Peso46a60, PesoMas61)
##    Tratamiento Variedad Peso40 Peso41a45 Peso46a60 PesoMas61
## 1      Testigo    Krone    380      1120     16180      2360
## 2      Testigo    Krone   2000      1480     15600      2340
## 3      Testigo    Krone    820       920     14840      6580
## 4      Testigo    Krone    130      1120     17180     11780
## 5      Testigo    Krone   1840      1240     12580      7040
## 6      Tratado    Krone    700       880     19120      3060
## 7      Tratado    Krone    440       680      5880       740
## 8      Tratado    Krone   1900      3620     12380       620
## 9      Tratado    Krone   1300      2100     12780      1240
## 10     Tratado    Krone    620      1620     12940       840
## 11     Testigo   Nicola    700       300      8810       420
## 12     Testigo   Nicola   3340      3480     12700       680
## 13     Testigo   Nicola   2730      1800     11080       500
## 14     Testigo   Nicola    930      1940     11080       500
## 15     Tratado   Nicola    630      1440     16510      1600
## 16     Tratado   Nicola   1120      3430     14700       490
## 17     Tratado   Nicola   2120      2690     13850       220
## 18     Tratado   Nicola   2600      6720     14340         0

Selecciono las variables Tratamiento y Variedad y además todas las que en su encabezado contengan los caracteres “Peso”.

select (Datos, Tratamiento, Variedad, contains ("Peso"))
##    Tratamiento Variedad Peso40 Peso41a45 Peso46a60 PesoMas61
## 1      Testigo    Krone    380      1120     16180      2360
## 2      Testigo    Krone   2000      1480     15600      2340
## 3      Testigo    Krone    820       920     14840      6580
## 4      Testigo    Krone    130      1120     17180     11780
## 5      Testigo    Krone   1840      1240     12580      7040
## 6      Tratado    Krone    700       880     19120      3060
## 7      Tratado    Krone    440       680      5880       740
## 8      Tratado    Krone   1900      3620     12380       620
## 9      Tratado    Krone   1300      2100     12780      1240
## 10     Tratado    Krone    620      1620     12940       840
## 11     Testigo   Nicola    700       300      8810       420
## 12     Testigo   Nicola   3340      3480     12700       680
## 13     Testigo   Nicola   2730      1800     11080       500
## 14     Testigo   Nicola    930      1940     11080       500
## 15     Tratado   Nicola    630      1440     16510      1600
## 16     Tratado   Nicola   1120      3430     14700       490
## 17     Tratado   Nicola   2120      2690     13850       220
## 18     Tratado   Nicola   2600      6720     14340         0

Selecciono las variables Tratamiento y Variedad y además todas las que en su encabezado finalicen con “Mas61”.

select (Datos, Tratamiento, Variedad, ends_with ("Mas61"))
##    Tratamiento Variedad PesoMas61 NumeroMas61
## 1      Testigo    Krone      2360          16
## 2      Testigo    Krone      2340          14
## 3      Testigo    Krone      6580          28
## 4      Testigo    Krone     11780          44
## 5      Testigo    Krone      7040          30
## 6      Tratado    Krone      3060          12
## 7      Tratado    Krone       740           3
## 8      Tratado    Krone       620           3
## 9      Tratado    Krone      1240           5
## 10     Tratado    Krone       840           4
## 11     Testigo   Nicola       420           2
## 12     Testigo   Nicola       680           3
## 13     Testigo   Nicola       500           2
## 14     Testigo   Nicola       500           2
## 15     Tratado   Nicola      1600           7
## 16     Tratado   Nicola       490           2
## 17     Tratado   Nicola       220           1
## 18     Tratado   Nicola         0           0
?select # Más información sobre esta función.
## starting httpd help server ... done

Encadenando ordenes con - %>% - que se puede leer como “entonces o después”.

Sin encadenar. Filtra seleccionando en el DF Datos, las variables Tratamiento y Variedad, cuando Tratamiento sea igual Testigo.

filter (select (Datos, Tratamiento, Variedad), Tratamiento=="Testigo") 
##   Tratamiento Variedad
## 1     Testigo    Krone
## 2     Testigo    Krone
## 3     Testigo    Krone
## 4     Testigo    Krone
## 5     Testigo    Krone
## 6     Testigo   Nicola
## 7     Testigo   Nicola
## 8     Testigo   Nicola
## 9     Testigo   Nicola

Encadenando. Trabaja con el DF Datos, después seleciona las variables Tratamiento y Variedad, y después las filtras.

Datos %>% select (Tratamiento, Variedad) %>% filter (Tratamiento=="Testigo") 
##   Tratamiento Variedad
## 1     Testigo    Krone
## 2     Testigo    Krone
## 3     Testigo    Krone
## 4     Testigo    Krone
## 5     Testigo    Krone
## 6     Testigo   Nicola
## 7     Testigo   Nicola
## 8     Testigo   Nicola
## 9     Testigo   Nicola