Reading and Writing Data to and from RReading files into RUsually we will be using data already in a file that we need to read into R in order to work on it. R can read data from a variety of file formats—for example, files created as text, or in Excel, SPSS or Stata. Writing Data to a FileAfter working with a dataset, we might like to save it for future use. Before we do this, let's first set up a working directory so we know where we can find all our data sets and files later. Setting up a DirectoryIn the R window, click on 'File' and then on 'Change dir'. You should then see a box pop up titled 'Choose directory'. For this class, choose the directory 'Desktop' by clicking on 'Browse', then select 'Desktop' and click 'OK'.
In the future, you may want to create a directory on your computer where you keep your data sets and codes for this class.Alternatively, you can use the setwd function to assign as working directory. setwd('C:/Desktop')To find out what your current working directory is, type getwdSetting Up Working Directories in R (R Tutorial 1.8) MarinStatsLectures. In R, we can write data frames easily to a file, using the write.table command. write.table(cars1, file=' cars1.txt ', quote=F)The first argument refers to the data frame to be written to the output file, the second is the name of the output file.
By default R will surround each entry in the output file by quotes, so we use quote=F.Now, let's check whether R created the file on the Desktop, by going to the Desktop and clicking to open the file. You should see a file with three columns, the first giving the index (or row number) and the other two the speed and distance. R by default creates a column of row indices. If we wanted to create a file without the row indices, we would use the command: write.table(cars1, file=' cars1.txt ', quote=F, row.names=F) Datasets in RWatch the video below for a concise intoduction to working with the variables in an R datasetWorking with Variables and Data in R (R Tutorial 1.5) MarinStatsLecures. Around 100 datasets are supplied with R (in the package datasets), and others are available.To see the list of datasets currently available use the command:dataWe will first look at a data set on CO2 (carbon dioxide) uptake in grass plants available in R.
CO2 Note: capitalization matters here; also: it's the letter O, not zero. Typing this command should display the entire dataset called CO2, which has 84 observations (in rows) and 5 variables (columns).To get more information on the variables in the dataset, type in help(CO2)Evaluate and report the mean and standard deviation of the variables 'Concentration' and 'Uptake'.Subsetting Data in R With Square Brackets and Logic Statements (R Tutorial 1.6) MarinStatsLecures.
. x: a to be written. file: a character specifying the name of the result file.
sep: the field separator string, e.g., sep = “t” (for tab-separated value). dec: the string to be used as decimal separator. Default is “.”. row.names: either a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written. col.names: either a logical value indicating whether the column names of x are to be written along with x, or a character vector of column names to be written. If col.names = NA and row.names = TRUE a blank column name is added, which is the convention used for CSV files to be read by spreadsheets.It’s also possible to write csv files using the functions write.csv and write.csv2. write.csv uses “.” for the decimal point and a comma (“,”) for the separator.
write.csv2 uses a comma (“,”) for the decimal point and a semicolon (“;”) for the separator.The syntax is as follow: write.csv(mydata, file = 'mydata.csv')write.csv2(mydata, file = 'mydata.csv').
Since JSON file format is text only, which can be sent to and from a server, and used as a data format by any programming language. The data in the JSON file is nested and hierarchical. Let us start reading and writing JSON files in R. Reading JSON files in R. R can read JSON files using the “ rjson ” package.