I would like to read a file in R as a table for a file that contains information in an odd format.
The file data.txt has the data written as:
04001400 HI 34.50 118.27 19480701 08 LST
0 0 0 0 0 0 0 0 0 0 0 0
MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS
04001400 HI 34.50 118.27 19480801 08 LST
0 0 0 0 0 0 0 0 0 0 0 0
MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS
04001400 HI 34.50 118.27 19480901 08 LST
0 0 0 0 0 0 0 0 0 0 0 0
MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS
the first number is a station number, HI is a case, the third and fourth numbers are lat and long coordinates, the other number is year,month,day,, and the other number (08) is time zone, followed by LST meaning time frame. The following 24 numbers or in the above example the 0’s and MIS are values for a particular region and time. I am trying to store the contents of the file as a table in this type of format of dimension [n x 31] (where 31 is the number of columns and n is the amount of rows total in the file):
04001400 HI 34.50 118.27 19480701 08 LST 0 0 0 0 0 0 0 0 0 0 0 0 MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS
04001400 HI 34.50 118.27 19480801 08 LST 0 0 0 0 0 0 0 0 0 0 0 0 MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS
04001400 HI 34.50 118.27 19480901 08 LST 0 0 0 0 0 0 0 0 0 0 0 0 MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS MIS
I have tried coding it this way based on the function read.table:
data <- read.table("data.txt", sep = c("\b", "\t", "\n"))
But it does not work as I have described above. Is there a way that I can do that? Thank you for your help.
You can use
scanto read multi-line data, especially since it is a specific format.which gives
You could, of course, give more informative names to the columns.
EDIT:
As Josh points out in the comments, my
whatargument was of the wrong format and caused all columns to be imported as character rather than some as character and some as numeric. It should have read:which gives the more appropriate: