“La variedad de sintaxis R brinda muchas formas de ‘decir’ lo
mismo” (Amelia McNamara)
Les comparto los resultados de utilizar los codigos de la primera
pagina este cheat sheet

1. Dollar sign syntax
library(datasets)
mtcars = mtcars
str(mtcars)
## 'data.frame': 32 obs. of 11 variables:
## $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
## $ cyl : num 6 6 4 6 8 6 8 4 4 6 ...
## $ disp: num 160 160 108 258 360 ...
## $ hp : num 110 110 93 110 175 105 245 62 95 123 ...
## $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
## $ wt : num 2.62 2.88 2.32 3.21 3.44 ...
## $ qsec: num 16.5 17 18.6 19.4 17 ...
## $ vs : num 0 0 1 1 0 1 0 1 1 1 ...
## $ am : num 1 1 1 0 0 0 0 0 0 0 ...
## $ gear: num 4 4 4 3 3 3 3 4 4 4 ...
## $ carb: num 4 4 1 1 2 1 4 2 2 4 ...
summary(mtcars)
## mpg cyl disp hp
## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000
1.1 Summary statistics:
# one continuous variable:
mean(mtcars$mpg)
## [1] 20.09062
# one categorical variable:
table(mtcars$cyl)
##
## 4 6 8
## 11 7 14
# two categorical variables: (forma de tabla cruzada)
table(mtcars$cyl, mtcars$am)
##
## 0 1
## 4 3 8
## 6 4 3
## 8 12 2
# one continuous, one categorical:
mean(mtcars$mpg[mtcars$cyl==4])
## [1] 26.66364
mean(mtcars$mpg[mtcars$cyl==6])
## [1] 19.74286
mean(mtcars$mpg[mtcars$cyl==8])
## [1] 15.1
1.2 Ploting
# one continuous variable:
hist(mtcars$disp)

boxplot(mtcars$disp)

# one categorical variable:
barplot(table(mtcars$cyl))

#two continuous variables:
plot(mtcars$disp, mtcars$mpg)

#two categorical variables:
mosaicplot(table(mtcars$am, mtcars$cyl))
library(lattice)

#one continuous, one categorical:
histogram(mtcars$disp[mtcars$cyl==4])

histogram(mtcars$disp[mtcars$cyl==6])

histogram(mtcars$disp[mtcars$cyl==8])

boxplot(mtcars$disp[mtcars$cyl==4])

boxplot(mtcars$disp[mtcars$cyl==6])

boxplot(mtcars$disp[mtcars$cyl==8])

1.3 Wrangling
# subsetting:
mtcars[mtcars$mpg>30, ]
## mpg cyl disp hp drat wt qsec vs am gear carb
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
# making a new variable:
mtcars$efficient[mtcars$mpg>30] <- TRUE
mtcars$efficient[mtcars$mpg<30] <- FALSE
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb efficient
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 FALSE
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 FALSE
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 FALSE
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 FALSE
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 FALSE
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 FALSE
mtcars$efficient2 = ifelse(mtcars$mpg>30, T, F)
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb efficient
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 FALSE
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 FALSE
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 FALSE
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 FALSE
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 FALSE
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 FALSE
## efficient2
## Mazda RX4 FALSE
## Mazda RX4 Wag FALSE
## Datsun 710 FALSE
## Hornet 4 Drive FALSE
## Hornet Sportabout FALSE
## Valiant FALSE
table(mtcars$efficient2)
##
## FALSE TRUE
## 28 4
2. Formula syntax
2.1 Summary statistics:
#install.packages("mosaic")
library(mosaic)
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
# one continuous variable:
mean(~mpg, data=mtcars)
## [1] 20.09062
mean(~mpg, data=mtcars)
## [1] 20.09062
# one categorical variable:
tally(~cyl, data=mtcars)
## cyl
## 4 6 8
## 11 7 14
# two categorical variables: (forma de tabla cruzada)
tally(cyl~am, data=mtcars)
## am
## cyl 0 1
## 4 3 8
## 6 4 3
## 8 12 2
# one continuous, one categorical:
mean(mpg~cyl, data=mtcars)
## 4 6 8
## 26.66364 19.74286 15.10000
2.2 Ploting
library(lattice)
# one continuous variable:
histogram(~disp, data=mtcars)

bwplot(~disp, data=mtcars)

# one categorical variable:
bargraph(~cyl, data=mtcars)

# two continuous variables:
xyplot(mpg~disp, data=mtcars)

# two categorical variables:
bargraph(~am, data=mtcars, group=cyl)

# one continuous, one categorical:
histogram(~disp|cyl, data=mtcars)

bwplot(cyl~disp, data=mtcars)

3. Tidyverse syntax
3.1 Summary statistics:
# shortcut pipe Ctrl + Shift + M
library(dplyr)
# one continuous variable:
mtcars %>% summarize(mean(mpg))
## mean(mpg)
## 1 20.09062
# one categorical variable:
mtcars %>% group_by(cyl) %>%
summarize(n())
## # A tibble: 3 × 2
## cyl `n()`
## <dbl> <int>
## 1 4 11
## 2 6 7
## 3 8 14
# two categorical variables: (forma de dataset)
mtcars %>% group_by(cyl, am) %>%
summarize(n())
## `summarise()` has grouped output by 'cyl'. You can override using the `.groups`
## argument.
## # A tibble: 6 × 3
## # Groups: cyl [3]
## cyl am `n()`
## <dbl> <dbl> <int>
## 1 4 0 3
## 2 4 1 8
## 3 6 0 4
## 4 6 1 3
## 5 8 0 12
## 6 8 1 2
# one continuous, one categorical:
mtcars %>% group_by(cyl) %>%
summarize(mean(mpg))
## # A tibble: 3 × 2
## cyl `mean(mpg)`
## <dbl> <dbl>
## 1 4 26.7
## 2 6 19.7
## 3 8 15.1
3.2 Ploting
library(ggplot2)
# one continuous variable:
qplot(x=mpg, data=mtcars, geom = "histogram")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(y=disp, x=1, data=mtcars, geom="boxplot")

# one categorical variable:
qplot(x=cyl, data=mtcars, geom="bar")

# two continuous variables:
qplot(x=disp, y=mpg, data=mtcars, geom="point")

# two categorical variables:
qplot(x=factor(cyl), data=mtcars, geom="bar") +
facet_grid(.~am)

# one continuous, one categorical:
qplot(x=disp, data=mtcars, geom = "histogram") +
facet_grid(.~cyl)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(y=disp, x=factor(cyl), data=mtcars,
geom="boxplot")

3.3 Wrangling
# subsetting:
mtcars %>% filter(mpg>30)
## mpg cyl disp hp drat wt qsec vs am gear carb efficient
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 TRUE
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 TRUE
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 TRUE
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 TRUE
## efficient2
## Fiat 128 TRUE
## Honda Civic TRUE
## Toyota Corolla TRUE
## Lotus Europa TRUE
mtcars$efficient = NULL
mtcars$efficient2 = NULL
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# making a new variable:
mtcars <- mtcars %>%
mutate(efficient = if_else(mpg>30, TRUE, FALSE))
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb efficient
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 FALSE
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 FALSE
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 FALSE
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 FALSE
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 FALSE
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 FALSE