There are many ways to input data into R. Below are examples of the most common ways.
This method is slow, and will only work for very small datasets. The functions, c(), factor(), gl(), and data.frame() are the most commonly used to enter data, and can be used in combination.
my.data <- c(4.5, 56, 78, 34, 5, 7)
my.fac <- factor(rep(c("Low", "High"), each=3))
my.fac2 <- gl(2, 3, labels = c("Low", "High")) #Same as my.fac
(my.frame <- data.frame(my.data, my.fac))
## my.data my.fac
## 1 4.5 Low
## 2 56.0 Low
## 3 78.0 Low
## 4 34.0 High
## 5 5.0 High
## 6 7.0 High
#Enter data and create the data.frame at once by nesting functions
#rnorm() and gl() are "inside" data.frame()
(my.frame2 <- data.frame(my.data = rnorm(6), my.factor = gl(2, 3)))
## my.data my.factor
## 1 -0.2966516 1
## 2 -0.4549107 1
## 3 -0.4682982 1
## 4 -1.8729634 2
## 5 1.3342171 2
## 6 -1.6658803 2
The most common way people import data into R is with the functions read.table or read.csv. We will use the latter and import data that was entered into Excel, but saved as a csv. Create a data set in Excel. Save the file as a csv text file. Make sure to note where you saved the file. Set the working directory in R to where you just saved the file with the function setwd(), or by going to the “File” menu when the workspace is active and selecting “Change dir…”. If you use the function setwd(), then you need to change the “\” to “\\” or “/”. You can double check to see if the working directory is now set to the correct location with the function getwd().
Below I set the working directory, where R will look for a file in the folder minerb2 that is located in my user folder on the c drive.
setwd("c:/users/minerb2") #or
setwd("c:\\users\\minerb2")
getwd()
Now that the working directory is set, you can read in the file. Use the function read.csv(). The only required argument is the file name with the extension surrounded with quotes. That is it! Below is an example of the file I read in. Notice the first and only argument is the complete file name surrounded by quotes. However, there are other arguments that allow more options when importing a file. See the help ?read.csv for more information. Another helpful function is dir(), which will print out all the file names in the working directory. If you cannot remember the spelling of the name of your file, then you can use dir() to look it up.
#dir()
(ben.data <- read.csv("../inst/extdata/lengthdata.csv"))
## Salinity Length
## 1 Low 3
## 2 Low 7
## 3 Low 5
## 4 Low 5
## 5 High 2
## 6 High 10
## 7 High 3
## 8 High 3
You can now check the structure of your data with the function str() or summarize your data with the function summary(). If you want to just see the first six or last six rows of data in your data.frame, then use the functions head() and tail()
str(ben.data)
## 'data.frame': 8 obs. of 2 variables:
## $ Salinity: chr "Low" "Low" "Low" "Low" ...
## $ Length : int 3 7 5 5 2 10 3 3
summary(ben.data)
## Salinity Length
## Length:8 Min. : 2.00
## Class :character 1st Qu.: 3.00
## Mode :character Median : 4.00
## Mean : 4.75
## 3rd Qu.: 5.50
## Max. :10.00
head(ben.data)
## Salinity Length
## 1 Low 3
## 2 Low 7
## 3 Low 5
## 4 Low 5
## 5 High 2
## 6 High 10
tail(ben.data)
## Salinity Length
## 3 Low 5
## 4 Low 5
## 5 High 2
## 6 High 10
## 7 High 3
## 8 High 3
Look at the help file for read.csv for other built-in functions, like read.table.
The new package readxl allows you to directly read in data from Excel. This package works great, but because it is new you rarely see examples using it. You will find lots of examples of users importing data with read.csv(). However, this is rapidly changing, and you will want to practice reading data directly from Excel. For example, I typically now just read in data directly from Excel–in the past I used the above methods and converted an Excel file to a csv file and imported the csv file. You will need to install and then load the package readxl before you can use the read_excel() or excel_sheets functions. See the page on installing packages for instructions (it is easy).
#install.packages(readxl) #Only needed if the package is not installed
library(readxl)
(ben.data2 <- read_excel("../inst/extdata/lengthdata.xlsx")) #Same as ben.data
## # A tibble: 8 × 2
## Salinity Length
## <chr> <dbl>
## 1 Low 3
## 2 Low 7
## 3 Low 5
## 4 Low 5
## 5 High 2
## 6 High 10
## 7 High 3
## 8 High 3
#View the names of the sheets in an excel file
excel_sheets("../inst/extdata/lengthdata.xlsx")
## [1] "Data"
You can see that there is just one sheet, which is named “Data”, in the Excel file lengthdata.xlsx.
Now that we have imported data into R in the form of a data.frame, we can pull out specific data, like all the data in a column. There are two ways to do this. Use single hard brackets to tell R you want a part of the object and within the bracket type the number (or vector of numbers) for the rows or columns you want to retrieve or type the name of the column surrounded with quotes (assuming it has a name). Double hard brackets strips out just the data and removes the column name. For a data.frame, which has rows and columns, you need to provide information about which rows and columns you want. For example
ben.data[, 2] #No rows were requests so R returns all rows from column 2
## [1] 3 7 5 5 2 10 3 3
ben.data[, -2] #All columns except the second
## [1] "Low" "Low" "Low" "Low" "High" "High" "High" "High"
ben.data[1:3, 1] #Rows 1 through 3 in column 1
## [1] "Low" "Low" "Low"
ben.data["Salinity"]
## Salinity
## 1 Low
## 2 Low
## 3 Low
## 4 Low
## 5 High
## 6 High
## 7 High
## 8 High
ben.data[["Salinity"]]
## [1] "Low" "Low" "Low" "Low" "High" "High" "High" "High"
Alternatively you can use the name of the data.frame followed by a $ and then the name of the column without quotes. For example
ben.data$Salinity
## [1] "Low" "Low" "Low" "Low" "High" "High" "High" "High"
This second option is typically the clearest. I almost always use the dollar sign to pull data out of data.frames.
Here are a few more advanced options for selecting data. Think about what the results should look like before you run the code (the results are below). Remember that my data.frame is called ben.data and the column names are “Length” and “Salinity”.
#Just to remind you of the data in ben.data
ben.data
## Salinity Length
## 1 Low 3
## 2 Low 7
## 3 Low 5
## 4 Low 5
## 5 High 2
## 6 High 10
## 7 High 3
## 8 High 3
ben.data$Length[4:7]
ben.data$Length[ben.data$Length > 5]
ben.data$Salinity[ben.data$Length > 5]
ben.data$Length[ben.data$Salinity == "High"]
ben.data$Length[ben.data$Salinity == "Low"]
ben.data$Length[ben.data$Salinity != "Low"]
## [1] 5 2 10 3
## [1] 7 10
## [1] "Low" "High"
## [1] 2 10 3 3
## [1] 3 7 5 5
## [1] 2 10 3 3
There are lots of datasets already loaded in R. Use the function data(), which requires no arguments, to return a list and descriptions of the all the datasets available. If you want to use one of these datasets, then type in the name of the dataset. I will assign the preloaded datasets to a new object name, so I can modify the data if needed.
data() #Shows you all the pre-loaded datasets
mtcars #Shows the data in the mtcars dataset
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
## Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
## Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
## Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
## Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
## Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
## Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
## Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
## Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
## Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
## Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
## Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
## Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
## Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
## AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
## Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
## Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
## Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
## Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
## Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
## Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
## Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
## Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
## Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
carData <- mtcars #Assigns the mtcars dataset to the name carData
head(carData)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1