Inputting Data into R

There are many ways to input data into R. Below are examples of the most common ways.

By hand

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))
#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))) 

R does have some minimal user interface features. One is a spreadsheet-like interface to enter or modify data in data.frames. To bring up this interface use the function edit(), where the data.frame of interest is the argument. For example, let's modify the data in my.frame.

(my.frame.v2 <- edit(my.frame)) #Change the 1st value to 13

It is important to remember that you must assign a name to edit, or the changes are lost.

If you want to enter your data from scratch with this interface, use the following code.

ben.frame.v3 <- edit(data.frame())
If you have a Mac, the function edit() might not work because Apple got rid of X11 devices. You can download X11 at the following website, which should fix the problem.

http://xquartz.macosforge.org/landing/

Import Data

Copy and paste data with scan()

Typically we don't want to enter data by hand, especially if it is already saved in Excel or another spreadsheet program. Below are a couple of common ways to import data.

The function scan() is a quick way to import data as vectors. Copy a row or column of numbers from Excel. Then go to R and type the following.

my.import <- scan()

Hit return, then "ctr v" to paste the values, and lastly hit return. Your data are now saved as my.import.

Auto import with read.csv()

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"))

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)
summary(ben.data)
head(ben.data)
tail(ben.data)

Look at the help file for read.csv for other built-in functions, like read.table.

Auto import Excel files

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
#View the names of the sheets in an excel file
excel_sheets("../inst/extdata/lengthdata.xlsx")

You can see that there is just one sheet, which is named "Data", in the Excel file lengthdata.xlsx.

Retrieving Data

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
ben.data[, -2] #All columns except the second
ben.data[1:3, 1] #Rows 1 through 3 in column 1
ben.data["Salinity"]
ben.data[["Salinity"]]

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

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
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"]
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"]

Data already in R

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
carData <- mtcars #Assigns the mtcars dataset to the name carData
head(carData)


mzinkgraf/BiometricsWWU documentation built on Dec. 9, 2023, 1:07 a.m.