15.3 Basic functions and packages

  • read.table(): Function to import most common files (.txt, .csv)
    • read.table("mydata.csv", header = TRUE, sep = ",", dec = ".")
      • header = TRUE: First row equals variable names
      • sep = ",": Columns are separted by a comma
      • dec = ".": Specfiy character for decimal points (. is default)
      • See other possible function arguments with ?read.table
  • Mostly you save files as .csv or .txt and then import them (e.g.¬†EXCEL files)
  • Other formats such as STATA, SPSS, SAS
    • foreign: Classic package with functions for data import
    • haven: New package!!
      • Imports STATA [.dta] (including 13), SPSS [.por or .sav] and SAS files (Description on Github)
      • Install, load it and check out the functions which it includes: ls("package:haven")
      • Contains multiple functions for reading and writing various formats
      • See example below!


  • EXPORT DATA
    • Ideally, as simple comma-separated .txt file
    • write.table(swiss, "swiss.txt", sep=","): Save as :.txt file
    • Check ?write.table for various arguments you can add to the function
    • See also export of other Quick R