Section 10 Operation on Dataframe

10.1 Example

?data.frame

set.seed(1234)
x <- c(1:20)
y <- sample(c('M','F'), size=20, replace=TRUE)
z <- rep(c(T,F), length=20)
  • Create a data.frame

  • Describe the data.frame

  • Add a column to the data.frame: v <- rep(1:4, each=5)

  • Delete a column from the data.frame: Delete the column v

  • Rename columns in the data.frame: Age, Sex, Vac

  • Reorder columns in the data.frame: Age, Vac, Sex

  • Get a subset of data.frame: Age >= 5, only columns Age, Vac

  • Change the reference level of a factor: Change the reference level of Sex as M

  • Change the order of a factor levels: Change the order of Sex as F, M

  • Change the names of a factor levels: Change the names of levels of Sex as Female, Male