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!!
Here's another possibility. This one adds quotes to any two words that begin a string
With regard to @akrun's comment about countries containing more than two words, I think this will work:
It also occurred to me that the country names might be the row names for the data frame. If that's the case, then you could do