Scanning text files
In previous recipes, we introduced how to use read.table
and read.csv
to load data into an R session. However, read.table
and read.csv
only work if the number of columns is fixed and the data size is small. To be more flexible in data processing, we will demonstrate how to use the scan
function to read data from the file.
Getting ready
In this recipe, you need to have completed the previous recipes and have snp500.csv
downloaded in the current directory.
How to do it…
Please perform the following steps to scan data from the CSV file:
- First, you can use the
scan
function to read data fromsnp500.csv
:> stock_data3 <- scan('snp500.csv',sep=',', what=list(Date = '', Open = 0, High = 0, Low = 0,Close = 0, Volume = 0, Adj_Close = 0), skip=1, fill=T) Read 16481 records
- You can then examine loaded data with
mode
andstr
:> mode(stock_data3) [1] "list" > str(stock_data3) List of 7 $ Date : chr [1:16481] "2015-07...