I'm trying to do read.table in R. My data (txt file) is like the following:
a b c d e
Australia 1 2 4 3 2
United States 1 2 4 2 2
The problems with reading this table are that:
1) Line 1 only has 5 elements (a~e), as opposed to 6 elements in all rows below that. It's supposed to have the column name like "Country". Then, a corresponds to the first number 1, b corresponds to 2,..and e corresponds to 2 (in the case of Australia.) How do I add a column name to the first column so that R won't show an error that says "line 1 did not have 6 elements"?
2) In United States case, United States are two words instead of one, so when R reads the data, it puts "States" into the second column instead of reading "United States" as one element name.
(i've been advised by my friend to use rownames. Does anyone know how to go about using rownames??)
How can I fix these issues and correctly read my data?
Thank you very much!!
Assuming that the example data mimics the content in the file, we could read it using
readLinesand then useregexto separate thecountry namesfrom the rest. The separated country names can be added as a new column.In the above code, we are replacing the
characterelements followed by space. ie. the country names with''.Similarly, here we are replacing the
spacefollowed by number (\\d+) followed by one or more characters.*with''.