[R] Extracting data from a file containing data

jim holtman jholtman at gmail.com
Thu Jun 25 21:12:57 CEST 2015


try this:

> input <- read.table("C:\\Users\\jh52822\\Downloads\\Nino_indices.txt"
+                     , header = TRUE
+                     , as.is = TRUE
+                     )
> # create factors
> input$season <- factor(c(rep("JF", 2), rep("MAM", 3), rep("JJA", 3)
+         , NA, rep("OND", 3)
+         )[input$MON], levels = c("JF", "MAM", "JJA", "OND"))
>
> # leave off the MON (-2) column from the data
> res <- aggregate(. ~ season + YR, data = input[, -2], FUN = 'mean')
> head(res,10)
   season   YR  NINO1.2       ANOM    NINO3     ANOM.1    NINO4     ANOM.2
 NINO3.4     ANOM.3
1      JF 1982 24.89000 -0.3750000 26.12500  0.1250000 28.25500  0.0550000
26.71000  0.0650000
2     MAM 1982 24.56000 -0.8366667 27.48333  0.2433333 28.94000  0.4466667
27.92000  0.3033333
3     JJA 1982 22.37000  0.6800000 26.68333  1.0033333 29.39333  0.6200000
28.26000  1.0300000
4     OND 1982 24.55333  2.8200000 27.70667  2.6933333 29.25333  0.6600000
28.88667  2.2500000
5      JF 1983 27.75500  2.4900000 28.92000  2.9200000 28.89500  0.6950000
29.24500  2.6000000
6     MAM 1983 28.47667  3.0800000 29.06333  1.8233333 28.89667  0.4033333
28.94333  1.3266667
7     JJA 1983 25.72333  4.0333333 26.88000  1.2000000 28.63667 -0.1366667
27.28000  0.0500000
8     OND 1983 22.25667  0.5233333 24.40667 -0.6066667 27.77667 -0.8166667
25.68000 -0.9566667
9      JF 1984 24.68000 -0.5850000 25.52000 -0.4800000 27.44500 -0.7550000
26.01500 -0.6300000
10    MAM 1984 24.79667 -0.6000000 26.97333 -0.2666667 27.62000 -0.8733333
27.21333 -0.4033333
>



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 Thu, Jun 25, 2015 at 11:40 AM, Peter Tuju <peterenos at ymail.com> wrote:

> I have the data as attached. For each nino region, I want to get the mean
> of the seasons January and February (JF), March, April and may (MAM), June,
> July and August (JJA), and October, November and December (OND) as columns
> of each nino regions. and have my result as;For example;
> For NINO1.2Years     JF      MAMA     JJA      OND 1982 1983    .    .
> .2012
> Any guide please!
>
>
> _____________
> Peter  E. Tuju
> Dar es Salaam
> T A N Z A N I A
> ----------------------
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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