36 Importing a CSV file into an R data.frame variable
############################################################################################################################################################ importing a CSV file into an R data.frame variable########################################################################################################################################################### CSV stands for "comma separated values". ## A CSV file contains data that is is intended to be arranged# in rows and columns (similar to an Excel file). Hoewver, in the # CSV file itself, the data is not lined up in columns. Rather# commas separate the data that should go in different columns.## Each row of the data is a line in the CSV file.# Each value in a line is separated from the other values by commas. ## EXAMPLE: The following could be the contents of a CSV file.## student,year,gender,test1,test2,final,honors# joe,so,m,100,100,89,TRUE# sam,so,m,95,93,missing,FALSE# sue,fr,f,80,66,68,FALSE# al,fr,m,59,52,42.5,FALSE# alice,fr,f,85,missing,missing,TRUE# anne,se,f,75,65,76,FALSE# bertha,se,f,65,58,62.5,FALSE# charlie,so,m,86,84,93,FALSE# david,so,m,78,82,88,TRUE# edgar,fr,m,64,68,60,FALSE# lou,ju,m,83,78,92.5,FALSE# francine,ju,f,90,91,79.5,FALSE# dan,ju,m,83,69,93,TRUE# daniella,se,f,96,100,100,FALSE# sarah,ju,f,80,68,78,FALSE# rebecca,so,f,77,83,75,FALSE# rachel,ju,f,80,82,86,TRUE# deborah,fr,f,95,100,100,FALSE# import the file grades.csv# - press "Import Dataset" button in Environment window# - choose "From Text (base)"# - choose the file# - fill in the following values:# o Name : the name of the variable that will hold your data# o Heading: choose "yes" if the data has column heading (otherwise, choose "no")# o Separator: for csv files choose "comma" (you can choose other types of separators based on the data in the file)# o na.strings: choose the value in the file that indicates NA data# o Strings as factors: for now make sure to UNcheck this - we will learn more about this later# - read.csv## This will run the read.csv function and assign the result to the variable# that you specified in the "Name" box. By default this will be the same name# as the name of the file.## RStudio will then run the View command to show the data in a tab in the # "source window" in RStudio.# Result of following the instructions above is that the following two # commands will be excuted.# - The 1st command creates a variable to hold the data.# - The 2nd command displays the data in the source window.## grades <- read.csv("C:/Users/Home/Desktop/grades.csv", header=FALSE, stringsAsFactors=FALSE)# View(grades2)## You can type these commands yourself but the RStudio interface makes it# easier to remember exactly how to type the commands.# Read the information from the file into the variable, grades.#grades <- read.csv("C:/Users/Home/Desktop/grades.csv", header=TRUE, stringsAsFactors=FALSE)grades
Error: object 'grades' not found
# To view the data in RStudio's source window use the View function## View(grades)# To view the data in the Console window, just type the name of the variablegrades