Data Frames
Working with Data Frames
# What is a data frame?
mtcars
## Creating a data frame from scratch
# First let's create some vectors containing information
name <- c("Louis", "Miles", "Charlie", "Joe", "Elvin")
instrument <- c("trumpet", "trumpet", "sax", "keyboard", "drums")
years <- c(15, 5, 8, 7, 14)
# Then put those vectors together as columns in a table (a data frame)
df <- data.frame(name, instrument, years)
Accessing Data Frame Components
df[,1] # get column 1 all rows - returns vector df[1,] # get row 1, all columns - returns dataframe df[1,2] # get row 1, column 2 - returns value df[, "name"] # get "name" column # Access elements by specifying ranges of rows or columns df[2:4, ] df[2:4, 2:3] # Accessing columns in different ways will return different values (vector vs. data frame) df$name # vector df[["instrument"]] # vector df["instrument"] # dataframe
Filtering Rows Based on Value
df[df$instrument == "trumpet",] # The equals operator in R is == df[df$years < 10,] # return the rows where years < 10 df[df$years < 10, ]["name"] # return as a df the names of players with less 10 years
Adding and Removing Rows and Columns
# Use rbind function to bind new rows to the dataframe
newdata <- list("Robert", "guitar", 8)
df <- rbind(df, newdata)
# Add columns with cbind
nrow(df) # how many rows do we have?
records <- c(8, 5, 4, 3, 7, 2)
df <- cbind(df, records)
# Or add column by specifying it with $
df$records2 <- records
# Removing columns
df$records2 <- NULL
df[, c(2, 4)] <- NULL
# Removing rows
df <- df[-6, ]
df[-c(1, 3), ]
