Skip to content

Instantly share code, notes, and snippets.

@jsta
Created March 18, 2016 22:26
Show Gist options
  • Save jsta/5a8f9ee03be680176ae6 to your computer and use it in GitHub Desktop.
Save jsta/5a8f9ee03be680176ae6 to your computer and use it in GitHub Desktop.
1 + 1
# assign b to 2
b <- 2
a <- 3
b <- a
a <- 6
# exploring sqrt function
b <- 4
sqrt(b)
sqrt(x = b)
# exploring round function
b <- 3.14343
round(b, 2)
round(x = b, digits = 2)
round(b)
# math with variables
a <- 1
b <- 2
a + b
# data types
a <- 1
b <- 1.3
name <- "joe"
# vectors
name <- c("joe", "stachelek")
weights <- c(10, 4, 6)
length(name)
weights <- c(weights, 8)
# subsetting
weights[2]
weights[2:3]
2:3
weights[c(1,3)]
# load and inspect csv file
# read.csv("data/portal_data_joined.csv")
surveys <- read.csv("data/portal_data_joined.csv")
head(surveys)
str(surveys)
str(weights)
# factors
sex <- c("male", "female", "male", "female")
factor(sex)
prey_availability <- factor(c("low", "high", "medium", "high"))
prey_availability <- factor(c("low", "high", "medium", "high"), levels = c("low", "medium", "high"))
age <- factor(c(10, 5, 3, 4))
levels(age)
# sum(age)
as.numeric(age)
as.numeric(as.character(age))
# data.frames
dim(surveys)
ncol(surveys)
nrow(surveys)
head(surveys)
names(surveys)
summary(surveys)
example_data <- data.frame(animal = c("dog", "sea cucumber", "sea urchin"), feel = (c("furry", "furry", "squishy")))
head(example_data)
class(example_data)
head(surveys)
class(surveys)
dim(surveys)
nrow(surveys)
ncol(surveys)
head(surveys)
names(surveys)
summary(surveys)
# subsetting data.frames
surveys[2, 2]
surveys[2:3, 2]
surveys[c(1, 4), 2]
surveys[1, 2:4]
surveys[1,]
# surveys[,1]
head(surveys[,1])
# dplyr
# install.packages("dplyr")
library(dplyr)
head(surveys)
head(surveys[,"taxa"])
head(select(surveys, taxa))
head(select(surveys, taxa, species, genus))
head(filter(surveys, year == 1995)) #note double equals
head(filter(select(surveys, taxa, year), year == 1995))
head(surveys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment