Skip to content

mean of each month in data

7 messages · arun, David L Carlson, Eliza Botto +1 more

#
Dear R users,
[in case the format of email is changed or you dont finf it easy to understand, i have attached a text file of my question]
i have the data in the following format and i want to convert it in the 
format given at the end.
Ta ans Sa are the names of certain cities. there are 69 cities in my 
data.
column 1 is representing station name (i am writing the data of only 
two cities for simplicity but actually, as i wrote, i have 69 cities 
and the actuall table runs down very deep.) 
Column 2 represnts the year for which the data is given (Actuall data 
for each station is of different length but atleast of 24 years).
Column 3 and 4 reprent the month and the day of the data. obviously 
each year has 12 months and each month as different number of days, but 
to make table easily understable only 3 months and 3 days of each month 
are considered. febrary for leap years should also be considered.
col5 represents population of that city
 
st. year month day population in million
Ta 1966 1 1  2.4
Ta 1966 1 2  2.4
Ta 1966 1 3  2.4
Ta 1966 2 1  2.4
Ta 1966 2 2  2.4
Ta 1966 2 3  2.4
Ta 1966 3 1  2.3
Ta 1966 3 2  2.2
Ta 1966 3 3  2.3 
Ta 1967 1 1  2.4
Ta 1967 1 2  2.4
Ta 1967 1 3  2.4
Ta 1967 2 1  2.4
Ta 1967 2 2  2.4
Ta 1967 2 3  2.4
Ta 1967 3 1  2.4
Ta 1967 3 2  2.4
Ta 1967 3 3  2.4
Ta 1968 1 1  2.4
Ta 1968 1 2  2.4
Ta 1968 1 3  2.4
Ta 1968 2 1  2.4
Ta 1968 2 2  2.4
Ta 1968 2 3  2.4
Ta 1968 3 1  2.3
Ta 1968 3 2  2.2
Ta 1968 3 3  2.3 
Ta 1969 1 1  2.4
Ta 1969 1 2  2.4
Ta 1969 1 3  2.4
Ta 1969 2 1  2.4
Ta 1969 2 2  2.4
Ta 1969 2 3  2.4
Ta 1969 3 1  2.4
Ta 1969 3 2  2.4
Ta 1969 3 3  2.4
Sa 1955 1 1  2.4
Sa 1955 1 2  2.4
Sa 1955 1 3  2.4
Sa 1955 2 1  2.4
Sa 1955 2 2  2.4
Sa 1955 2 3  2.4
Sa 1955 3 1  2.3
Sa 1955 3 2  2.2
Sa 1955 3 3  2.3 
Sa 1956 4 1  2.4
Sa 1956 4 2  2.4
Sa 1956 4 3  2.4
Sa 1956 5 1  2.4
Sa 1956 5 2  2.4
Sa 1956 5 3  2.4
Sa 1956 6 1  2.4
Sa 1956 6 2  2.4
Sa 1956 6 3  2.4
Sa 1957 7 1  2.4
Sa 1957 7 2  2.4
Sa 1957 7 3  2.4
Sa 1957 8 1  2.4
Sa 1957 8 2  2.4
Sa 1957 8 3  2.4
Sa 1957 9 1  2.3
Sa 1957 9 2  2.2
Sa 1957 9 3  2.3 
Sa 1957 10 1  2.4
Sa 1958 10 2  2.4
Sa 1958 10 3  2.4
Sa 1958 11 1  2.4
Sa 1958 11 2  2.4
Sa 1958 11 3  2.4
Sa 1958 12 1  2.4
Sa 1958 12 2  2.4
Sa 1958 12 3  2.4
...
...
uptill 69th station
 
i want to convert the data in following format
1966   1967   1968   1969
AVERAGE OF MONTH 1 AVERAGE OF MONTH 1 AVERAGE OF MONTH 1 AVERAGE OF MONTH 1
AVERAGE OF MONTH 2 AVERAGE OF MONTH 2 AVERAGE OF MONTH 2 AVERAGE OF MONTH 2
AVERAGE OF MONTH 3 AVERAGE OF MONTH 3 AVERAGE OF MONTH 3 AVERAGE OF MONTH 3
........
........
AVERAGE OF MONTH 12 AVERAGE OF MONTH 12 AVERAGE OF MONTH 12 AVERAGE OF MONTH 12
similar operation are to be done for "Sa" and the remaining 67 
stations...
which means i want to have 69 matrices, in which each column (number of 
columns should be equal to number of years of data)  should contain 12 
mean monthly values of population of each year. 

thanks in advance
 
eliza 		 	   		  
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: index.G2.txt
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121217/c4835bd2/attachment.txt>
#
HI,

May be this helps:
dat1<-read.table("Eliza.txt",sep="",header=TRUE,stringsAsFactors=FALSE)
library(reshape2)
?res<-lapply(split(dat1,dat1$st.),function(x) dcast(x,month~year,mean,value.var="population_in_million"))
res
$Sa
#?? month???? 1955 1956???? 1957 1958
#1????? 1 2.400000? NaN????? NaN? NaN
#2????? 2 2.400000? NaN????? NaN? NaN
#3????? 3 2.266667? NaN????? NaN? NaN
#4????? 4????? NaN? 2.4????? NaN? NaN
#5????? 5????? NaN? 2.4????? NaN? NaN
#6????? 6????? NaN? 2.4????? NaN? NaN
#7 ???? 7????? NaN? NaN 2.400000? NaN
#8????? 8????? NaN? NaN 2.400000? NaN
#9????? 9????? NaN? NaN 2.266667? NaN
#10??? 10????? NaN? NaN 2.400000? 2.4
#11??? 11????? NaN? NaN????? NaN? 2.4
#12??? 12????? NaN? NaN????? NaN? 2.4

#$Ta
?# month???? 1966 1967???? 1968 1969
#1???? 1 2.400000? 2.4 2.400000? 2.4
#2???? 2 2.400000? 2.4 2.400000? 2.4
#3???? 3 2.266667? 2.4 2.266667? 2.4
A.K.




----- Original Message -----
From: eliza botto <eliza_botto at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Monday, December 17, 2012 12:11 PM
Subject: [R] mean of each month in data


Dear R users,
[in case the format of email is changed or you dont finf it easy to understand, i have attached a text file of my question]
i have the data in the following format and i want to convert it in the 
format given at the end.
Ta ans Sa are the names of certain cities. there are 69 cities in my 
data.
column 1 is representing station name (i am writing the data of only 
two cities for simplicity but actually, as i wrote, i have 69 cities 
and the actuall table runs down very deep.) 
Column 2 represnts the year for which the data is given (Actuall data 
for each station is of different length but atleast of 24 years).
Column 3 and 4 reprent the month and the day of the data. obviously 
each year has 12 months and each month as different number of days, but 
to make table easily understable only 3 months and 3 days of each month 
are considered. febrary for leap years should also be considered.
col5 represents population of that city

st. year month day population in million
Ta 1966 1 1? 2.4
Ta 1966 1 2? 2.4
Ta 1966 1 3? 2.4
Ta 1966 2 1? 2.4
Ta 1966 2 2? 2.4
Ta 1966 2 3? 2.4
Ta 1966 3 1? 2.3
Ta 1966 3 2? 2.2
Ta 1966 3 3? 2.3 
Ta 1967 1 1? 2.4
Ta 1967 1 2? 2.4
Ta 1967 1 3? 2.4
Ta 1967 2 1? 2.4
Ta 1967 2 2? 2.4
Ta 1967 2 3? 2.4
Ta 1967 3 1? 2.4
Ta 1967 3 2? 2.4
Ta 1967 3 3? 2.4
Ta 1968 1 1? 2.4
Ta 1968 1 2? 2.4
Ta 1968 1 3? 2.4
Ta 1968 2 1? 2.4
Ta 1968 2 2? 2.4
Ta 1968 2 3? 2.4
Ta 1968 3 1? 2.3
Ta 1968 3 2? 2.2
Ta 1968 3 3? 2.3 
Ta 1969 1 1? 2.4
Ta 1969 1 2? 2.4
Ta 1969 1 3? 2.4
Ta 1969 2 1? 2.4
Ta 1969 2 2? 2.4
Ta 1969 2 3? 2.4
Ta 1969 3 1? 2.4
Ta 1969 3 2? 2.4
Ta 1969 3 3? 2.4
Sa 1955 1 1? 2.4
Sa 1955 1 2? 2.4
Sa 1955 1 3? 2.4
Sa 1955 2 1? 2.4
Sa 1955 2 2? 2.4
Sa 1955 2 3? 2.4
Sa 1955 3 1? 2.3
Sa 1955 3 2? 2.2
Sa 1955 3 3? 2.3 
Sa 1956 4 1? 2.4
Sa 1956 4 2? 2.4
Sa 1956 4 3? 2.4
Sa 1956 5 1? 2.4
Sa 1956 5 2? 2.4
Sa 1956 5 3? 2.4
Sa 1956 6 1? 2.4
Sa 1956 6 2? 2.4
Sa 1956 6 3? 2.4
Sa 1957 7 1? 2.4
Sa 1957 7 2? 2.4
Sa 1957 7 3? 2.4
Sa 1957 8 1? 2.4
Sa 1957 8 2? 2.4
Sa 1957 8 3? 2.4
Sa 1957 9 1? 2.3
Sa 1957 9 2? 2.2
Sa 1957 9 3? 2.3 
Sa 1957 10 1? 2.4
Sa 1958 10 2? 2.4
Sa 1958 10 3? 2.4
Sa 1958 11 1? 2.4
Sa 1958 11 2? 2.4
Sa 1958 11 3? 2.4
Sa 1958 12 1? 2.4
Sa 1958 12 2? 2.4
Sa 1958 12 3? 2.4
...
...
uptill 69th station

i want to convert the data in following format
1966?  1967?  1968?  1969
AVERAGE OF MONTH 1 AVERAGE OF MONTH 1 AVERAGE OF MONTH 1 AVERAGE OF MONTH 1
AVERAGE OF MONTH 2 AVERAGE OF MONTH 2 AVERAGE OF MONTH 2 AVERAGE OF MONTH 2
AVERAGE OF MONTH 3 AVERAGE OF MONTH 3 AVERAGE OF MONTH 3 AVERAGE OF MONTH 3
........
........
AVERAGE OF MONTH 12 AVERAGE OF MONTH 12 AVERAGE OF MONTH 12 AVERAGE OF MONTH 12
similar operation are to be done for "Sa" and the remaining 67 
stations...
which means i want to have 69 matrices, in which each column (number of 
columns should be equal to number of years of data)? should contain 12 
mean monthly values of population of each year. 

thanks in advance

eliza ??? ???  ??? ?  ??? ??? ? 
______________________________________________
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.
#
A slight modification to Rui's answer would give you a list containing
separate matrices for each station:
+      timevar = "year", v.names = "population")
$Sa
   month population.1955 population.1956 population.1957 population.1958
1      1        2.400000              NA              NA              NA
2      2        2.400000              NA              NA              NA
3      3        2.266667              NA              NA              NA
4      4              NA             2.4              NA              NA
5      5              NA             2.4              NA              NA
6      6              NA             2.4              NA              NA
7      7              NA              NA        2.400000              NA
8      8              NA              NA        2.400000              NA
9      9              NA              NA        2.266667              NA
10    10              NA              NA        2.400000             2.4
12    11              NA              NA              NA             2.4
13    12              NA              NA              NA             2.4
   population.1966 population.1967 population.1968 population.1969
1               NA              NA              NA              NA
2               NA              NA              NA              NA
3               NA              NA              NA              NA
4               NA              NA              NA              NA
5               NA              NA              NA              NA
6               NA              NA              NA              NA
7               NA              NA              NA              NA
8               NA              NA              NA              NA
9               NA              NA              NA              NA
10              NA              NA              NA              NA
12              NA              NA              NA              NA
13              NA              NA              NA              NA

$Ta
   month population.1955 population.1956 population.1957 population.1958
14     1              NA              NA              NA              NA
15     2              NA              NA              NA              NA
16     3              NA              NA              NA              NA
   population.1966 population.1967 population.1968 population.1969
14        2.400000             2.4        2.400000             2.4
15        2.400000             2.4        2.400000             2.4
16        2.266667             2.4        2.266667             2.4

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
Hello,

You want to draw 2 + 67 lines in the same graph? The result should be 
confusing...
As for the graph itself, you can use ?matplot, it's meant for that sort 
of problem.

Hope this helps,

Rui Barradas
Em 17-12-2012 20:54, eliza botto escreveu:
#
Hello,

Better yet, with 'agg' as given in my first post:

sp <- lapply(split(agg, agg$st), function(x) x[order(x$year, x$month), ])
plot(agg$year, agg$population, type = "n")
lapply(seq_along(sp), function(i) lines(sp[[i]]$year, 
sp[[i]]$population, col = i))


Hope this helps,

Rui Barradas
Em 17-12-2012 22:00, Rui Barradas escreveu: