[R] grided files
jim holtman
jholtman at gmail.com
Fri Oct 31 22:33:23 CET 2014
Here is an example of how you might do it:
> # read in all the data so you can break it apart.
> # it looks like there is head and then 17 lines of data
> data_in <-
readLines("C:\\Users\\jh52822\\Downloads\\analysis_mly_avg_1998.txt")
> indx <- 1L # start at the first line
> all_data <- NULL
> while (indx < length(data_in)){
+ header <- data_in[indx]
+ # replace tabs in header with spaces
+ header <- gsub("\t+", " ", header)
+ # create a dataframe from the next 17 lines
+ new_data <- read.table(text = data_in[(indx + 1L):(indx + 17L)])
+ new_data$header <- header # add to the file
+ all_data <- rbind(all_data, new_data)
+ indx <- indx + 18L
+ }
>
>
> str(all_data)
'data.frame': 51 obs. of 9 variables:
$ V1 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V2 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V3 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V4 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V5 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V6 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V7 : int 0 0 0 0 0 0 0 0 0 0 ...
$ V8 : int 0 0 0 0 0 0 0 0 0 0 ...
$ header: chr " 1998 8 " " 1998 8 " " 1998 8 " " 1998 8 " ...
> all_data
V1 V2 V3 V4 V5 V6 V7 V8 header
1 0 0 0 0 0 0 0 0 1998 8
2 0 0 0 0 0 0 0 0 1998 8
3 0 0 0 0 0 0 0 0 1998 8
4 0 0 0 0 0 0 0 0 1998 8
5 0 0 0 0 0 0 0 0 1998 8
6 0 0 0 0 0 0 0 0 1998 8
7 0 0 0 0 0 0 0 0 1998 8
8 0 0 0 0 0 0 0 0 1998 8
9 0 0 0 0 0 0 0 0 1998 8
10 0 0 0 0 0 0 0 0 1998 8
11 0 0 0 0 0 0 0 0 1998 8
12 0 0 0 0 0 0 0 0 1998 8
13 0 0 0 0 0 0 0 0 1998 8
14 0 0 0 0 0 0 0 0 1998 8
15 0 0 0 0 0 0 0 0 1998 8
16 0 0 0 0 0 0 0 0 1998 8
17 0 0 0 0 0 0 0 0 1998 8
18 0 0 0 0 0 0 0 0 1998 9
19 0 0 0 0 0 0 0 0 1998 9
20 0 0 0 0 0 0 0 0 1998 9
21 0 0 0 0 0 0 0 0 1998 9
22 0 0 0 0 0 0 0 0 1998 9
23 0 0 0 0 0 0 0 0 1998 9
24 0 0 0 0 0 0 0 0 1998 9
25 0 0 0 0 0 0 0 0 1998 9
26 0 0 0 0 0 0 0 0 1998 9
27 0 0 0 0 0 0 0 0 1998 9
28 0 0 0 0 0 0 0 0 1998 9
29 0 0 0 0 0 0 0 0 1998 9
30 0 0 0 0 0 0 0 0 1998 9
31 0 0 0 0 0 0 0 0 1998 9
32 0 0 0 0 0 0 0 0 1998 9
33 0 0 0 0 0 0 0 0 1998 9
34 0 0 0 0 0 0 0 0 1998 9
35 0 0 0 0 0 0 0 0 1998 10
36 0 0 0 0 0 0 0 0 1998 10
37 0 0 0 0 0 0 0 0 1998 10
38 0 0 0 0 0 0 0 0 1998 10
39 0 0 0 0 0 0 0 0 1998 10
40 0 0 0 0 0 0 0 0 1998 10
41 0 0 0 0 0 0 0 0 1998 10
42 0 0 0 0 0 0 0 0 1998 10
43 0 0 0 0 0 0 0 0 1998 10
44 0 0 0 0 0 0 0 0 1998 10
45 0 0 0 0 0 0 0 0 1998 10
46 0 0 0 0 0 0 0 0 1998 10
47 0 0 0 0 0 0 0 0 1998 10
48 0 0 0 0 0 0 0 0 1998 10
49 0 0 0 0 0 0 0 0 1998 10
50 0 0 0 0 0 0 0 0 1998 10
51 0 0 0 0 0 0 0 0 1998 10
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Fri, Oct 31, 2014 at 3:12 PM, Alemu Tadesse <alemu.tadesse at gmail.com>
wrote:
> Dear All,
>
> I have a file the sample of which is attached. I was trying to read the
> data and put it in a data frame. For example in this file, I have 1998 snow
> depth data and each block of data belongs to one month. Each data point
> belongs to a given latitude and longitude (which is in another file). If I
> can read this data and put it in some kind of table where I can provided
> the index of the latitude and longitude and get the the data that would be
> awesome, but it is taking time. I am wondering if have read such a file.
>
> Best,
>
> Alemu
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list