Reading and Accessing Data
The purpose of this project is to give you practice with reading data and working with variables in R. A variable is a named location in memory that can store information/data.
Project tasks
- Open R Studio or a text editor and Create a new R file called project1.R
- Put your name, Project 1, and the date at the top of the file. No credit will be given for an R file without your name at the top of the file. Create a project1 working directory and save your file there. Then select the menu option Session::Set Working Directory::To Source File Location.
Part 1: Setup
Get ready to write some R code.
-
Assign to the variable
valuesAthe numbers from 1 to 10 using the slice notation. Assign to a second variablevaluesBthe numbers from 11 to 20, also using the slice notation. Assign to a third variablevaluesCthe numbers from 21 to 30 using the c() function. The arguments to the c function should be the numbers 21 to 30 separated by commas. -
Print out the results of adding, subtracting, multiplying, and dividing
valuesAbyvaluesB. Repeat withvaluesAandvaluesC.To print a value, use the function
print()and make the thing you want to print the argument. For example:print( valuesA + valuesB )
-
Assign to the variable
resultsa double vector of length four. You can use the functiondouble(). The parameter for thedouble()is the length of the vector you want R to create. -
Using the
sumorprodfunction, assign toresults[1]the sum ofvaluesAplusvaluesB, assign toresults[2]the sum ofvaluesAminusvaluesB, assign toresults[3]the product of the elements ofvaluesA, and assign toresults[4]the sum ofvaluesAtimesvaluesB. Have R print outresults, which should be the values [210 -100 3628800 935[.
Part 2: Creating and Working with Variables
Assign some values to variables, manipulate them using basic arithmetic, and use built-in R functions to calculate simple statistics.
-
Download the Comma-Separated-Values [CSV] test file test1.csv and save it in the same directory as your variables.R file. In your project1.R file assign to the variable
dfthe result of calling the read.csv function with the argument test1.csv. Be sure to put quotes around the filename in the function call. Note, you may need to use the Session::Set Working Directory::To Source File Location, for R Studio to look in the right place for the file.Run your source file and make sure that df shows up in your workspace as 6 observations of 3 variables.
-
Have your code print the names of the columns of the data frame by printing the result of calling the
namesfunction withdfas the argument. -
Have your code print out the first column of data using three methods.
- Using the $ notation and the name of the first column.
- Using the [] notation and the index of the first column.
- Using the [[]] notation and the index of the first column.
Which notation returns the result as a vector? (Hint: a vector should look like what happens when you print valuesA.)
- Have your code print out the value in the the third row and second column of the data.
-
Have your code print out the first row of data using the expression
df[1,]. Have your code print out the second row of data the same way. Then use theas.numeric()function to convert the row of data to a vector before printing it. - Have your code print out the sum and mean of each column of data. Note that you will want to use the $ or [[]] notation to access the column as a vector.
- Create or find an Excel or CSV file with at least two columns of data of numeric data. If it is an Excel file, save it as a CSV file. Write code to read in the file and print out the mean of each of the first two columns of numeric data in the CSV file.
Part 3: Reading Data from a File
Read a comma separated values [CSV] file and figure out how to access the column names and data.
Report
Answer the following questions. Submit your answers as a plain text file or PDF in your handin directory. Put your name at the top of the file. No credit will be given for any other format or for a file without a name.
- What is a variable?
- What is a vector?
- What is a data frame?
- What is a CSV file?
- What are the steps you have to take to read a CSV file and compute the mean of the first column of data?
Handin
Create a project1 folder inside the Private folder in your Courses directory. Put your project1.R file and your text/PDF file with your answers into the project1 folder. They should be the only documents in the project1 folder.
Fall 2019