R comes with a number of example data sets. You can view these data sets in RStudio by typing ‘data()’.
summary(mtcars) # R has automatically loaded the mtcars data frame for us
## mpg cyl disp hp
## Min. :10.4 Min. :4.00 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.4 1st Qu.:4.00 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.2 Median :6.00 Median :196.3 Median :123.0
## Mean :20.1 Mean :6.19 Mean :230.7 Mean :146.7
## 3rd Qu.:22.8 3rd Qu.:8.00 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.9 Max. :8.00 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.76 Min. :1.51 Min. :14.5 Min. :0.000
## 1st Qu.:3.08 1st Qu.:2.58 1st Qu.:16.9 1st Qu.:0.000
## Median :3.69 Median :3.33 Median :17.7 Median :0.000
## Mean :3.60 Mean :3.22 Mean :17.8 Mean :0.438
## 3rd Qu.:3.92 3rd Qu.:3.61 3rd Qu.:18.9 3rd Qu.:1.000
## Max. :4.93 Max. :5.42 Max. :22.9 Max. :1.000
## am gear carb
## Min. :0.000 Min. :3.00 Min. :1.00
## 1st Qu.:0.000 1st Qu.:3.00 1st Qu.:2.00
## Median :0.000 Median :4.00 Median :2.00
## Mean :0.406 Mean :3.69 Mean :2.81
## 3rd Qu.:1.000 3rd Qu.:4.00 3rd Qu.:4.00
## Max. :1.000 Max. :5.00 Max. :8.00
If you want to analyze other data in R there are several options for reading your data into R-Studio. Among the most common are…
From a .csv file:
# use the read.csv commmand
cars <- read.csv('filepath/filename.csv', row.names=1)
# row.names tells R that the first row of our data contains variable names
From a Stata data file:
# use the read.dta command that is part of the foreign package
# if you have not already installed the foreign package you can type install.packages("foreign") to install it
library(foreign)
mydata <- read.dta("filepath/filename.dta")
From an Excel data file:
# use the read.xlsx command that is part of the xlsx package
# if you have not already installed the xlsx package you can type install.packages("xlsx") to install it
# alternatively you can save your file in .csv format in Excel
library(xlsx)
mydata <- read.xlsx("filepath/filename.xlsx", 2) # the 2 tells R to read in the second page in the Excel workbook