Problem

You want to add or remove columns from a data frame.

Solution

There are many different ways of adding and removing columns from a data frame.

data <- read.table(header=TRUE, text='
 id weight
  1     20
  2     27
  3     24
')

# Ways to add a column
data$size      <- c("small", "large", "medium")
data[["size"]] <- c("small", "large", "medium")
data[,"size"]  <- c("small", "large", "medium")
data$size      <- 0   # Use the same value (0) for all rows


# Ways to remove the column
data$size      <- NULL
data[["size"]] <- NULL
data[,"size"]  <- NULL
data[[3]]      <- NULL
data[,3]       <- NULL
data           <- subset(data, select=-size)