(1) Browsing to
find data on a computer and reading it into R.
When trying to find data
and import it into R, the commands from the previous tutorial are used;
the arguments for each are covered there. The only difference will be
the replacement of the file location with the argument 'file.choose()'.
The examples below use the same functions for actually reading the data
file as was done in the previous tutorial.
Finding a text (.txt)
file:
data <- read.table(file.choose(), header = TRUE, sep = " ", dec = ".")
Finding a comma separated
values (.csv) file:
data <- read.csv(file.choose(), header = TRUE, sep = ",", dec = ".")
Finding an SPSS (.sav)
file; note you must load the 'foreign' library in order to import an
SPSS file to R:
library(foreign)
data <- read.spss(file.choose(), use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
In future tutorial notes,
we will be using R console and script files; but remember all scripts
can be copied and pasted into the R Console. The script files can also
be downloaded and then opened with the R Console or in R Commander
using ‘File’, ‘Open script file…’ in the Console or Rcmdr top task bar.
When reading the script
files, you'll notice the common convention of using # to start a
comment line (which is not working code), while lines without # are
working code.
|