# To access on-line manuals, references, and other material, you can type:
help.start()
x <- 10 # This creates an object called *x* that contains the value 10
# Typing the object name returns the information stored inside the object
# by printing it in the console
x
y <- 5.3 # This created another object called *y* that contains the value 5.3
y
# # You can store the ouput of a function in an object. Then, these output can be
# used for other purposes: (more on functions below)
seq(from = 4, to = 10, by = 2) # This just prints the result in the screen
x <- seq(from=4, to=10, by=2) # This RE-WRITES the object *x* with the sequence
x
# You now can do things with the values stored in 'x'.
#For example you can do basic arithmetics:
# the arithmetic operators for sum '+', substraction '-', multiplication '*', and
# division '/'.
#we can multiply the values in 'x' by a constant:
x*2
#add pi to each value of x
x+pi
#other arithmetic function: "is x equal to, greater than, less than  ...?"
x
x==6
x>6
x>=6
#or calculate other statistics
# function 'mean' to calculate the mean of the values in 'x':
mean(x)
# You can calculate other statistics, like the standard deviation, or just create
# a summary of the values in x:
sd(x)
summary(x)
# You can change the order of the values:
sort(x)
sort(x,decreasing=T)
# We can also write a single line of code that performs multiple actions and
# saves the output, for exmple
y <- rnorm(4)*2
#To see the values in 'y', just type:
y
plot(x,y, xlim=c(0,12))
seq(from = 5, to = 20, by = 0.5)
rep(x = 8, times = 3) # *rep* has *x*, *times*
rnorm(n = 10, mean = 2, sd = 1) # *rnorm* has *n*, *mean*, *sd*
help(topic="seq")
seq(from = 5, to = 20, by = 0.5)
seq(from = 5, to = 20, by = 0.5) # Most explicit (all with names)
seq(5, 20, 0.5) # Quickest, no argument names
# But they are different from this:
seq(0.5, 5, 20)
help(topic="seq") # See this info in the help page of the function *seq*
seq(from = 5, to = 20, by = 0.5)
seq(by = 0.5, from = 5, to = 20)
rnorm(n=10)
help(c) # Also could have been written as help(topic="c")
z<-c(9, 5, 3, 5)
z
# RULE 9. R is case sensitive, so the function *seq* exist, but the
# function *Seq* does not:
seq(5,20,0.5)
Seq(from = 5, to = 20, by = 0.5)
seq(from=5, to=20, by=0.5)
seq(from     =     5,      to     =     20,      by     =     0.5)
seq(from=5,
to=20,
by=
0.5)
# *rep*
rep(x = "R", times = 10)
rep(times = 10, x = "R")
rep("R", 10)
rep(10, "R") # Why doesn't this work?
getwd()
getwd()
c4c <- read.csv("c4c_FB.csv",stringsAsFactors = F,header = T)
?read.csv
View(c4c)
head(c4c)
c4c
dim(c4c)
names(c4c)
c4c$Forearm
c4c$Id[c4c$Forearm < 56]
length(c4c$Id[c4c$Forearm < 56])
range(c4c$Forearm)
length(c4c$Id[c4c$Forearm > 70 & c4c$Forearm < 75])
str(c4c)
as.factor(c4c$Sex)
levels(as.factor(c4c$Sex))
as.numeric(c4c$Age)
as.Date(c4c$Date,format="%m/%d/%Y")
str(as.factor(c4c$Sex))
levels(as.factor(c4c$Sex))
c4c$Age[is.na(as.numeric(c4c$Age))]
c4c$Sex[c4c$Sex != "f" & c4c$Sex !="m"]
length(c4c$Sex[c4c$Sex != "f" & c4c$Sex !="m"])
str(c4c)
c4c$Sex[c4c$Sex=="F"] <- "f"
c4c$Sex[c4c$Sex=="f "] <- "f"
as.factor(c4c$Sex)
str(as.factor(c4c$Sex))
levels(as.factor(c4c$Sex))
c4c$Age[c4c$Age=="one"] <- "1"
c4c$Age[c4c$Age=="two"] <- "2"
c4c$Age[c4c$Age=="four"] <- "4"
as.numeric(c4c$Age)
c4c$Sex <- as.factor(c4c$Sex)
c4c$Prev <- as.factor(c4c$Prev)
c4c$Age <- as.numeric(c4c$Age)
c4c$Date <- as.Date(c4c$Date, format="%m/%d/%Y")
str(c4c)
c4c$Size[c4c$Forearm < 60] <- "Small"
c4c$Size[c4c$Forearm >= 60 & c4c$Forearm < 75] <- "Medium"
c4c$Size[c4c$Forearm >= 75] <- "Large"
c4c$Size <- as.factor(c4c$Size)
View(c4c)
getwd()
homewd = "/Users/carabrook/Developer/coding4conservation/tutorials/R-Intro"
save.image(paste0(homewd, "/Cleaned-C4C.Rdata"))
write.csv(c4c,paste0(homewd, "/Cleaned-C4C.csv"),row.names=F)
rm(list=ls())
load("Cleaned-C4C.Rdata")
View(c4c)
head(c4c)
str(c4c)
load("Cleaned-C4C.Rdata")
hist(c4c$Para)
?hist
hist(c4c$Para,probability = T)
## Polished graph
hist(c4c$Para,probability = T,
main="Parasite load distribution",
xlab="Parasite load")
boxplot(c4c$Forearm)
?boxplot
boxplot(c4c$Forearm~c4c$Sex)
attach(c4c)
boxplot(Forearm~Sex)
## Add more features to the graph, so it will look nicer.
# Add the full names to the Sex group.
boxplot(Forearm~Sex,
names=c("Females","Males"))
boxplot(Forearm~Sex,
names=c("Females","Males"),
col=c(2,8))
boxplot(Forearm~Sex,
names=c("Females","Males"),
col=c(2:3),
main="Forearm by Sex",
xlab="Sex",
ylab="Forearm (mm)",
ylim=c(0,100))
boxplot(Forearm~Sex*Prev,
col=c(2:3),
main="Forearm by Sex",
xlab="Sex",
ylab="Forearm (mm)",
ylim=c(0,100))
detach(c4c)
PLoad <- tapply(c4c$Para,factor(format(c4c$Date,"%m")),mean)
barplot(PLoad)
pie(PLoad)
gp_pload <- group_by(c4c,Months = factor(format(Date,"%m")))
library(dplyr)
gp_pload <- group_by(c4c,Months = factor(format(Date,"%m")))
st_pload <- summarise(gp_pload,
infected = length(Prev[Prev == 1]) / length(Prev[Prev == 0]),
Count=n())
View(st_pload)
barplot(st_pload$infected,names.arg = st_pload$Months)
rm(gp_pload,st_pload,PLoad)
attach(c4c)
?plot()
plot(Weight~Forearm)
plot(Weight~Forearm,
main = "Weight/Forearm",
ylab ="Weight (g)",
xlab = "Forearm (mm)",
col=Sex)
plot(Weight~Sex)
plot(Forearm~Age,
main = "Forearm/Age",
ylab ="Forearm (mm)",
xlab = "Age (year)",
col=Sex,
cex=0.5)
plot(Forearm~Age,
main = "Weight/Forearm",
ylab ="Weight (g)",
xlab = "Forearm (mm)",
col=Sex,
pch=5,
cex=.2)
fitcurv <- lowess(Forearm~Age)
plot(Forearm~Age,
main="Forearm/Age, lowess fitting",
xlab="Age (year)",
ylab="Forearm (mm)",
type="p",
pch=3,
cex=0.7)
lines(fitcurv,col="red",cex=6)
detach(c4c)
datmal <- filter(c4c,Sex=="m")
datfem <- filter(c4c,Sex=="f")
fitfem <- lowess(datfem$Forearm~datfem$Age)
fitmal <- lowess(datmal$Forearm~datmal$Age)
plot(c4c$Forearm~c4c$Age,
main="Forearm/Age, lowess fitting",
xlab="Age (year)",
ylab="Forearm (mm)",
type="p",
pch=3,
cex=0.7,
col=c4c$Sex)
## Now add the two lines with different color
lines(fitfem,col="black",lwd=3)
lines(fitmal,col="red",lwd=3)
legend(x=0, y=95,
legend=c("Males","Females"),
col=c("red","black"),
title="Lowess fit",
lty=1,
x.intersp = .5,
y.intersp = .8)
rm(list=ls())
require(ggplot2)
?ggplot
load("Cleaned-C4C.Rdata")
ggplot(c4c,aes(Age,Forearm))  ##Try this
ggplot(c4c,aes(Age,Forearm)) +
geom_point()
ggplot(c4c,aes(Age,Forearm)) +
geom_line()
ggplot(c4c,aes(Age,Forearm)) +
geom_point() +
geom_line()
## Add a smooth to the point plot
ggplot(c4c,aes(Age,Forearm)) +
geom_point() +
geom_smooth()
############  Mapping aesthetic vs Fixed Value   ##################
ggplot(c4c,aes(Age,Forearm)) +
geom_point()
ggplot(c4c,aes(Age,Forearm)) +
geom_point(color="red", shape=3)
ggplot(c4c,aes(Age,Forearm)) +
geom_point(aes(color=Sex, shape=Sex))
ggplot(c4c,aes(Age,Forearm)) +
geom_point(aes(color=Sex, shape=Sex)) +
ggtitle("Forearm by Age with ggplot2")+
scale_x_continuous(name="Age (Year)",
limits=c(0,5))+
scale_y_continuous(name="Forearm (mm)",
limits=c(40,100))+
scale_color_discrete(name="Sex",
breaks=c("f","m"),
label=c("female","male"))+
scale_shape_discrete(name="Sex",
breaks=c("f","m"),
label=c("female","male")) +
geom_smooth(aes(color=Sex),method = "loess") +
theme_classic()
