Use the raw data files from Project 5a: states-files.zip.
This zip file contains files state1.txt,
state2.txt, ... , state50.txt, one for
each state. Each file contains the ten largest cities and populations for that
state. Write an R function named readStateData that inputs the name of the state input file as a
parameter, reads the data from that file into a dataframe, and returns a list with the data from
that file. Then write a loop that calls your function for the fifty states (n =
1:50) and creates a big list containing the lists of the data
from each state file. You don't need to write the big list out to a file.
Hints:
Use the readLines function with n = 1
to read the state name from a state file.
Use the read.table function with skip = 1
and sep="," to
read the city data from a state file and create a data frame.
You can also use read.csv with skip = 1.
Use header=F because there is no header line to create a
data frame.
Add the column names in the data frame with the col.names argument.
Use the list function to create a list, consisting of the state
name and the data frame of the city data.
The for statement of the loop looks like this:
biglist = NULL
for(i in 1:50) {
# Create statename using the paste function
biglist[[i]] <- readStateData(statename)
}
Construct the state names state1.txt, state2.txt, ... ,
state50.txt with the paste function.
Use sep="" to avoid inserting extra spaces.