Created
February 28, 2014 12:21
-
-
Save jmmateoshggm/9270168 to your computer and use it in GitHub Desktop.
Clase R @ LIM 2014
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a <- c(4, 6, 3, 7, 3, 6, 3, 0) | |
mean(a) | |
sd(a) | |
median(a) | |
var(a) | |
a[1] | |
a[1:4] | |
a[c(1, 3, 8)] | |
# Comentario. Esto no se ejecuta. | |
a[1] <- 5 | |
a | |
a[2] <- NA | |
a | |
mean(a) | |
mean(a, na.rm = TRUE) | |
class(a) | |
b <- c("a", "c", "d") | |
class(b) | |
class(iris) | |
length(a) | |
length(iris) | |
dim(iris) | |
nrow(iris) | |
ncol(iris) | |
head(iris) | |
?head | |
head(iris, n = 10) | |
summary(iris) | |
mean(iris$Sepal.Length) | |
class(iris$Species) | |
# Factores | |
factorEx <- c(rep(1, 10), rep(2, 10), rep(3, 10)) | |
factorEx2 <- factor(factorEx, labels = c("grupo1", | |
"grupo2", | |
"grupo3")) | |
head(iris) | |
iris[1, ] | |
iris[1, c(1, 3)] | |
iris[1, -5] | |
iris[1, -c(1, 5)] | |
testIris <- iris[iris$Species == "versicolor", ] | |
index1 <- iris$Species == "versicolor" | |
testIris <- iris[index1, ] | |
testIris <- iris[!index1, ] | |
TRUE | FALSE | |
TRUE & FALSE | |
TRUE & !FALSE | |
which(index1) | |
# Bucles y estructuras de control | |
for (i in a) { | |
print(i) | |
} | |
for (i in 1:length(a)) { | |
print(a[i]) | |
} | |
for (i in 1:length(a)) { | |
if (a[i] < 7) { | |
print(a[i]) | |
} | |
} | |
# Funciones | |
f1 <- function(x, y = 5) { | |
ret <- list(orig = x, | |
mod = x - y, | |
m = mean(x, na.rm = TRUE)) | |
return(ret) | |
} | |
b <- f1(1:10) | |
class(b) | |
b$orig | |
b$mod | |
b2 <- f1(1:10, 2) | |
iris2 <- iris[, -5] | |
summary(iris2) | |
colMeans(iris2) | |
rowMeans(iris2) | |
colSums(iris2) | |
apply(iris2, 2, sd) | |
sapply(iris2, sd) | |
iris2[20, 3] <- NA | |
apply(iris2, 2, sd, na.rm = TRUE) | |
apply(iris2, 2, function(x) sd(x, na.rm = TRUE)) | |
for (i in 1:ncol(iris2)) { | |
print(sd(iris2[, i], na.rm = TRUE)) | |
} | |
aggregate(Sepal.Length ~ Species, data = iris, mean) | |
a <- read.table(url("http://core2.gsfc.nasa.gov/research/purucker/mgs_gridded.data")) | |
names(a) | |
names(a)[1] <- "Variable1" | |
names(a) | |
a <- read.csv(url("http://pims.grc.nasa.gov/plots/user/mike/2013_05_sensor_gmt_excelt_medfreq_medrms.csv")) | |
install.packages("xlsx") | |
library(xlsx) | |
install.packages("foreign") | |
library(foreign) | |
# T tests, regresiones, etc | |
x <- rnorm(100) | |
y <- rnorm(100) | |
t1 <- t.test(x, y) | |
t.test(x, y, paired = TRUE, var.equal = TRUE) | |
x <- 1:100 | |
y <- x + rnorm(100, sd = 20) | |
plot(x, y) | |
lm1 <- lm(y ~ x) | |
lm1 | |
abline(lm1) | |
s1 <- summary(lm1) | |
for(i in 1:100) { | |
x <- rnorm(100) | |
y <- rnorm(100) | |
lm1 <- lm(y ~ x) | |
s1 <- summary(lm1) | |
p1 <- s1$coefficients[2, 4] | |
if (p1 < 0.05) { | |
print(p1) | |
} | |
} | |
x1 <- rnorm(100) | |
x2 <- rnorm(100) | |
y <- x1 + x2 + rnorm(100) | |
lm2 <- lm(y ~ x1 + x2) | |
summary(lm2) | |
# ANOVA y relacionados | |
lm3 <- lm(Sepal.Length ~ Species, data = iris) | |
anova(lm3) | |
TukeyHSD(aov(Sepal.Length ~ Species, data = iris)) | |
pairwise.t.test(iris$Sepal.Length, iris$Species, p.adjust.method = "bonferroni") | |
shapiro.test(iris$Sepal.Length) | |
pairwise.wilcox.test(iris$Sepal.Length, iris$Species) | |
kruskal.test(Sepal.Length ~ Species, data = iris) | |
plot(iris$Petal.Length ~ iris$Species) | |
plot(density(iris$Petal.Length)) | |
plot(iris) | |
with(iris, plot(Sepal.Length, Sepal.Width, col = as.numeric(Species))) | |
library(ggplot2) | |
theme_set(theme_bw()) | |
ggplot(iris) + geom_point(aes(x = Sepal.Width, y = Sepal.Length, size = Petal.Length, color = Species)) | |
ggplot(iris) + geom_point(aes(x = Sepal.Width, y = Sepal.Length)) + facet_wrap( ~ Species) | |
# Para multiplicar matrices: %*% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment