13.9 select(): Subsetting and renaming

  • Classic way
    • names(dataframe) <- charactervector
    • or rename() function in different packages (e.g.┬ápackage reshape)
    • better with dplyr
library(dplyr)
select(swiss, Fertility, Agriculture, Education) # Select columns by name
select(swiss, Agriculture:Education) # Variables from:to
select(swiss, -(Agriculture:Education)) # Variables without (from:to)
select(swiss, Geburtsrate = Fertility) # Select and rename
rename(swiss, Geburtsrate = Fertility) # Alternative because select drops non-mentioned variables!



# Q: How can I save that in a new data frame?
select(swiss, Agriculture:Education)

# Q: What do I have to type to extract the variables Catholic and Infant.Mortality
# from the swiss dataset and save them in a new object?


# CLASSIC WAY
swiss2 <- swiss
names(swiss2)[names(swiss2)=="Agriculture"] <- "test"
# Q: How would you go about to understand this term?