Below is an exercise from Datacamp.
Using the cbind() call to include all three sheets. Make sure the first column of urban_sheet2 and urban_sheet3 are removed, so you don't have duplicate columns. Store the result in urban.
Code:
# Add code to import data from all three sheets in urbanpop.xls
path <- "urbanpop.xls"
urban_sheet1 <- read.xls(path, sheet = 1, stringsAsFactors = FALSE)
urban_sheet2 <- read.xls(path, sheet = 2, stringsAsFactors = FALSE)
urban_sheet3 <- read.xls(path, sheet = 3, stringsAsFactors = FALSE)
# Extend the cbind() call to include urban_sheet3: urban
urban <- cbind(urban_sheet1, urban_sheet2[-1],urban_sheet3[-1])
# Remove all rows with NAs from urban: urban_clean
urban_clean<-na.omit(urban)
My question is why using [-1] to remove the first column in cbind. Is it a special use of square brackets inside cbind()? Does that mean that if I want to remove the first two columns the code should be urban_sheet2[-2]? I only know that square brackets are used for selecting certain columns or rows. This confuses me.