# Create vectors
elevations <- c(100, 5280, 2)
cities <- c('Seattle', 'Denver', 'New Orleans')
# Convert to miles
feet_to_miles <- function(feet) {
return(feet/5280)
}
# Vecotrized operation
elevation_miles <- feet_to_miles(elevations)
# Get Boolean indicies
mile_high <- elevation_in_miles >= 1
mile_high_cities <- cities[mile_high]
# Or....
cities[elevation_in_miles >= 1]
# Create a list
person1 <- list(name="Miriam", salary=40000)
# Create list
person1 <- list(name="Miriam", salary=40000)
# $ syntax
salary <- person1$salary
# Double braches [[
salary <- person1[['salary']]
# List index (not advised)
salary <- person1[[2]]
# These both return another list (likely not what you want)
salary_list <- person1['salary']
salary_list <- person1[2]
# Create person
person1 <- list(name="Miriam", salary=40000)
# $ syntax
person1$height <- 63
# Double braches
person1[['favorite_book']] <- "Infinite Jest"
# List index (not advised)
person1[[5]] <- 'what is this...'
# Create vectors
elevations <- c(100, 5000, 2)
cities <- c('Seattle', 'Denver', 'New Orleans')
city_df <- data.frame(cities, elevations)
print(city_df)
cities elevations
1 Seattle 100
2 Denver 5000
3 New Orleans 2
nrow(df)
ncol(df)
dim(df)
head(df)
tail(df)
View(df)
rownames(df)
colnames(df)
# $ syntax
cities <- city_df$cities
# Double brackets [[
elevations <- city_df[['elevations']]
# Index (not recommended)
elevations <- city_df[[2]]
# Add a column
city_df$lived_in <- c(TRUE, FALSE, TRUE)
> print(city_df)
cities elevations lived_in
1 Seattle 100 TRUE
2 Denver 5000 FALSE
3 New Orleans 2 TRUE
# Using row/column position in square brackets
lived_in_seattle <- city_df[1,3]
# Using row/column names
lived_in_denver <- city_df[2, 'lived_in']
# Get all columns for first row
seattle <- city_df[1,]
# Get all rows for the elevations column
all_elevations <- city_df[,'elevations']
# Create a factor variable
x <- factor(c('Jane', 'Ella', 'Mario'))
print(x)
[1] Jane Ella Mario
Levels: Ella Jane Mario
# Look at structure with str function
str(x)
Factor w/ 3 levels "Ella","Jane",..: 2 1 3
x[1] <- 'Mario'
str(x)
Factor w/ 3 levels "Ella","Jane",..: 3 1 3
x[1] <- 'Mike'
Warning message:
In `[<-.factor`(`*tmp*`, 1, value = "Mike") :
invalid factor level, NA generated
# Create vectors
elevations <- c(100, 5000, 2)
cities <- c('Seattle', 'Denver', 'New Orleans')
city_df <- data.frame(elevations, cities, stringsAsFactors=FALSE)
city_df$cities
[1] "Seattle" "Denver" "New Orleans"
# Get Boolean indicies
city_df <- data.frame(cities, elevations)
cities <- as.character(city_df$cities)