[R] How could I restrict and reordered data.frames?

jim holtman jholtman at gmail.com
Thu May 20 14:54:20 CEST 2010


? merge

> obs <- read.table(textConnection("12866   14.4
+
+ 12844   14.1
+
+ 12843   16.5
+ 12860   14.9
+ 12851    9.8
+
+ 12846   15.3"), col.names=c('station', 'obs'))
> fore <- read.table(textConnection("12830   12.808611
+ 12836   12.725081
+ 12843   15.241580
+ 12844   15.185887
+ 12846   13.723515
+ 12851    8.498717
+ 12860   15.715260
+ 12866   14.262023
+ 12870   12.968392"), col.names=c('station', 'fore'))
> closeAllConnections()
>
> # use 'merge' to group them
>
> x <- merge(obs, fore, by="station", all=TRUE)
>
> x
  station  obs      fore
1   12830   NA 12.808611
2   12836   NA 12.725081
3   12843 16.5 15.241580
4   12844 14.1 15.185887
5   12846 15.3 13.723515
6   12851  9.8  8.498717
7   12860 14.9 15.715260
8   12866 14.4 14.262023
9   12870   NA 12.968392
>

You can then order by the first column.

On Thu, May 20, 2010 at 6:22 AM, Csima Gabriella <csima.g at met.hu> wrote:
> Dear Everyone,
>
>
>
> I 've just begun to use the library ncdf and I would like to compare meteorological observational data with forecast data, so to make verification. The netcdf files I'm using contain data of many different parameters in many different stations.  I could read easily that I needed, but naturally I do not need the data of all the stations. On the other hand, the order of the stations is not the same in the observation files and in the forecast files.
>
> Let's take that I have a list of those stations (with station numbers) where I would like to make the verification. I read the observations in all the possible stations and I receive a data.frame (first column with the station numbers, second column with - let' say - the temperature data...and naturally we can have more columns with different parameters). I make the same with the forecast data, as I wrote the orders of the station numbers in the two dataset are different, and naturally there are some stations that you can find in one data.frame but not in the other.
>
>
>
> How could I make (or rewrite) my two data.frames (observation and forecast), where the first coulumn is totally the same as in the station list (even the order of the stations)??
>
>
>
> For example, I have this data.frame as obsesrvation:
>
>
>
> 12866   14.4
>
> 12844   14.1
>
> 12843   16.5
> 12860   14.9
> 12851    9.8
>
> 12846   15.3
>
>
>
>
>
> ...and  have this data.frame as forecast:
>
>
>
> 12830   12.808611
> 12836   12.725081
> 12843   15.241580
> 12844   15.185887
> 12846   13.723515
> 12851    8.498717
> 12860   15.715260
> 12866   14.262023
> 12870   12.968392
>
>
> ...and my list of the stations I needed
>
>
>
> 12836
> 12843
> 12846
> 12860
> 12870
>
>
>
>
> Thank you very much for your help or suggestions in advance!
>
> Gabriella Csima
>
> csima.g at met.hu
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list