Chapter 2 Interacting With RStudio

2.1 updates

  • R.version.string checks for r version

2.2 working directory

  • setwd("~/WORKING_DIRECTORY_PATH") to load and save documents

2.3 packages

  • install.packages("PACKAGE") installs new package (required once)
  • library(LIBRARY_NAME) attaches (opens) a package (required every time a session opened)
  • search() lists packages currently attached
  • .libPaths lists where packages are saved in computer

2.4 import

2.4.1 csv

  • DF <- read.csv("FILENAME.csv", header = TRUE) reads the .csv file within the working directory
  • for files with multiple headers rows (often the case with .csv files from quatrics) use:
    • DF <- read.csv("FILENAME", header = TRUE)[-1:-2,] to import the .csv without 2nd and 3rd rows
    • write.csv(DF, "NEW_FILENAME.csv") # writes a new csv to write a new .csv on the working directory
    • DF <- read.csv("NEW_FILENAME.csv", header = TRUE) to import the new .csv
    • note that the file actions take place within the working directory, follows the code chunk
DF <- read.csv("FILENAME", header = TRUE)[-1:-2,] # cleans the 2nd and 3rd 
write.csv(DF, "FILENAME.csv") # writes a new csv
DF <- read.csv("FILENAME.csv", header = TRUE) # writes a new DF

2.4.2 excel

  • DF <- read.xlsx2("FILENAME.xlsx", 1) requires the xlxs package, reads the .xls file within the working directory

2.4.3 SPSS

DF <- spss.get("FILENAME.sav", use.value.labels=TRUE) requires the Hmisc package, reads the .sav file within the working directory

2.5 objects in the environment

  • ls() to list all objects
  • rm(OBJECT) to remove an object (for several objects separete each object name by a coma)