install.packages(“dplyr”)
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
getwd()
## [1] "C:/Users/Familia/Desktop/Curso"
setwd("C:/Users/Familia/Desktop/Curso")
edidiv <- read.csv("C:/Users/Familia/Desktop/Curso/edidiv.csv")
head(edidiv) # Muestra las primeras filas
tail(edidiv) # Muestra las últimas filas
str(edidiv) # dice si las variables son continuas,enteros, categóricos o caracteres
## 'data.frame': 25684 obs. of 5 variables:
## $ organisationName: Factor w/ 28 levels "BATS & The Millennium Link",..: 14 14 14 8 8 28 28 28 28 28 ...
## $ gridReference : Factor w/ 1938 levels "NT200701","NT200712",..: 1314 569 569 1412 1412 1671 1671 1671 1671 1671 ...
## $ year : int 2000 2000 2000 2000 2000 2001 2001 2001 2001 2001 ...
## $ taxonName : Factor w/ 1275 levels "Acarospora fuscata",..: 1126 1126 1127 192 193 1202 365 977 472 947 ...
## $ taxonGroup : Factor w/ 11 levels "Beetle","Bird",..: 2 2 2 2 2 2 2 2 2 2 ...
head(edidiv$taxonGroup) #Muestra solo las primeras filas de esta columna
## [1] Bird Bird Bird Bird Bird Bird
## 11 Levels: Beetle Bird Butterfly Dragonfly Flowering.Plants ... Mollusc
class(edidiv$taxonGroup) # indica con que tipo de variable se esta trabajando: es de caracter pero se desea que sea un factor
## [1] "factor"
edidiv$taxonGroup <- as.factor(edidiv$taxonGroup)
dim(edidiv) # Displays number of rows and columns
## [1] 25684 5
summary(edidiv) # Gives you a summary of the data
## organisationName gridReference
## Biological Records Centre :6744 NT2673 : 2741
## RSPB :5809 NT2773 : 2031
## Butterfly Conservation :3000 NT2873 : 1247
## Scottish Wildlife Trust :2070 NT2570 : 1001
## Conchological Society of Great Britain & Ireland:1998 NT27 : 888
## The Wildlife Information Centre :1860 NT2871 : 767
## (Other) :4203 (Other):17009
## year taxonName taxonGroup
## Min. :2000 Maniola jurtina : 1710 Butterfly :9670
## 1st Qu.:2006 Aphantopus hyperantus: 1468 Bird :7366
## Median :2009 Turdus merula : 1112 Flowering.Plants:2625
## Mean :2009 Lycaena phlaeas : 972 Mollusc :2226
## 3rd Qu.:2011 Aglais urticae : 959 Hymenopteran :1391
## Max. :2016 Aglais io : 720 Mammal : 960
## (Other) :18743 (Other) :1446
summary(edidiv$taxonGroup) # Gives you a summary of that particular variable (column) in your dataset
## Beetle Bird Butterfly Dragonfly
## 426 7366 9670 421
## Flowering.Plants Fungus Hymenopteran Lichen
## 2625 334 1391 140
## Liverwort Mammal Mollusc
## 125 960 2226
#calcular la riqueza de las especies # Para saber cuántas especies de aves, plantas, mamíferos, etc. tenemos en Edimburgo, primero
Beetle <- filter(edidiv, taxonGroup == "Beetle")
Bird <- filter(edidiv, taxonGroup == "Bird")
Butterfly <- filter(edidiv, taxonGroup == "Butterfly")
Dragonfly <- filter(edidiv, taxonGroup == "Dragonfly")
Flowering.Plants <- filter(edidiv, taxonGroup == "Flowering.Plants")
Fungus <- filter(edidiv, taxonGroup == "Fungus")
Hymenopteran <- filter(edidiv, taxonGroup == "Hymenopteran")
Lichen <- filter(edidiv, taxonGroup == "Lichen")
Liverwort <- filter(edidiv, taxonGroup == "Liverwort")
Mammal <- filter(edidiv, taxonGroup == "Mammal")
Mollusc <- filter(edidiv, taxonGroup == "Mollusc")
library(dplyr)
a <- length(unique(Beetle$taxonName))
b <- length(unique(Bird$taxonName))
c <- length(unique(Butterfly$taxonName))
d <- length(unique(Dragonfly$taxonName))
e <- length(unique(Flowering.Plants$taxonName))
f <- length(unique(Fungus$taxonName))
g <- length(unique(Hymenopteran$taxonName))
h <- length(unique(Lichen$taxonName))
i <- length(unique(Liverwort$taxonName))
j <- length(unique(Mammal$taxonName))
k <- length(unique(Mollusc$taxonName))
#4. Crear un vector y trazarlo #Hacemos esto usando la c()función
biodiv <- c(a,b,c,d,e,f,g,h,i,j,k) # Estamos encadenando todos los valores; preste atención a los nombres de objetos que ha calculado y su orden
names(biodiv) <- c("Beetle",
"Bird",
"Butterfly",
"Dragonfly",
"Flowering.Plants",
"Fungus",
"Hymenopteran",
"Lichen",
"Liverwort",
"Mammal",
"Mollusc")
#para visualizar la riqueza de especies con la barplot()función. Los gráficos aparecen en la ventana inferior derecha en RStudio.
barplot(biodiv)
help(barplot) # Para obtener ayuda con la función barplot ()
## starting httpd help server ... done
help(par) # Para obtener ayuda con el trazado en general
png("barplot.png", width=1600, height=600) # look up the help for this function: you can customise the size and resolution of the image
barplot(biodiv, xlab="Taxa", ylab="Number of species", ylim=c(0,600), cex.names= 1.5, cex.axis=1.5, cex.lab=1.5)
dev.off()
## png
## 2
#5. Crear un marco de datos y trazarlo
#Utilizamos la data.frame()función, pero primero crearemos un objeto que contenga los nombres de todos los taxones (una columna) y otro objeto con todos los valores para la riqueza de especies de cada taxón (otra columna).
# Creating an object called "taxa" that contains all the taxa names
taxa <- c("Beetle",
"Bird",
"Butterfly",
"Dragonfly",
"Flowering.Plants",
"Fungus",
"Hymenopteran",
"Lichen",
"Liverwort",
"Mammal",
"Mollusc")
# Turning this object into a factor, i.e. a categorical variable
taxa_f <- factor(taxa)
# Combining all the values for the number of species in an object called richness
richness <- c(a,b,c,d,e,f,g,h,i,j,k)
# Creating the data frame from the two vectors
biodata <- data.frame(taxa_f, richness)
# Saving the file
write.csv(biodata, file="biodata.csv") # it will be saved in your working directory
png("barplot2.png", width=1600, height=600)
barplot(biodata$richness, names.arg=c("Beetle",
"Bird",
"Butterfly",
"Dragonfly",
"Flowering.Plants",
"Fungus",
"Hymenopteran",
"Lichen",
"Liverwort",
"Mammal",
"Mollusc"),
xlab="Taxa", ylab="Number of species", ylim=c(0,600))
dev.off()
## png
## 2
#6. Retate a ti mismo # Calcule la envergadura media de cada especie de ave. La función para hacer eso es simplemente: mean ()
sparrow <- mean(22, 24, 21)
kingfisher <- mean(26, 23, 25)
eagle <- mean(195, 201, 185)
hummingbird <- mean(8, 9, 9)
# Encadenarlos juntos en un vector
wingspan <- c(sparrow, kingfisher, eagle, hummingbird)
# Cree un vector de especies de aves (¡tenga cuidado de hacer coincidir el orden del vector anterior!)
bird_sp <- c("sparrow", "kingfisher", "eagle", "hummingbird")
# La especie de ave está actualmente en forma de carácter, pero debería ser un factor.
class(bird_sp) # currently character
## [1] "character"
bird_sp <- as.factor(bird_sp) # transformando a factor
class(bird_sp) # Ahora un factor
## [1] "factor"
# Luego, combine los dos vectores en un marco de datos
wings <- data.frame(bird_sp, wingspan)
# Trazar el diagrama de barras y guardarlo en el archivo
png("wingspan_plot.png", width=800, height=600)
barplot(wings$wingspan, names.arg = wings$bird_sp, # notice how we call the bird_sp column instead of typing all the names
xlab = "Bird species",
ylab = "Average wingspan (cm)", # adding axis titles
ylim = c(0, 200), # setting the limits of the y axis to fit the eagle
col = "gold" # changing the colour because why not!
)
dev.off()
## png
## 2