Dear R users, I have fifty two (52) text files with the same dimensions (ie 31
by 13). Three sample of such data files are attached. I want to compute the
rowMeans for each separate file for;(i) all the months
(ii) For January and February
(iii) For March, April and May
(iv) For June, July and august
(v) For October, November and December
(vi) Plot the single mass curve for each file and season ie. plot(Year,
cumsum(rowMeans([])))
(vii) Plot Time series graphs for each file and per each season.
The code I was trying to use is given below, and I investigated it and find
that it does just for one only one file.How can I loop through all files?
I kindly need your help.
Thanks in advance!!
rm(list = ls())
setwd("/run/media/nwp-tma/+255767090047/analysis/R/R_sessions/R_sessions_prec/Rain_stn_data")#
Import text filesprec_files <- list.files(pattern="*.txt")# Reading my files
prec_files <- list.files(pattern = ".txt")
for (i in 1:length(prec_files)){
prec_data = read.delim(prec_files[i], sep="\t",
header = TRUE) }
#prec_data <- as.numeric(prec_data[, 2:13])
# All years assignments
all_yr <- prec_data[, 2:13]
# Season assignment
jf <- prec_data[, 2:3]
mam <- prec_data[, 4:6]
jja <- prec_data[, 7:9]
ond <- prec_data[, 11:13]
jf_means <- apply(jf, 1, mean)
mam_means <- apply(mam, 1, mean)
jja_means <- apply(jja, 1, mean)
ond_means <-apply(ond, 1, mean)
yr_mean <- apply(all_yr, 1, mean)
_____________
Peter E. Tuju
Dar es Salaam
T A N Z A N I A
----------------------
Year Jan Feb Mar Apr May Jun Jul Aug Sep
Oct Nov Dec
1982 6.3 3.6 123.6 191.5 148.7 91.9 20.5 15.6 44.3
291 196.4 147.2
1983 11.7 13.9 170.7 225.8 221.7 17.2 11 0.4 10
73 27.6 118.3
1984 73.4 0.2 202.6 368.6 131.4 47.1 20.4 1.8 6.4
52 128.4 84.4
1985 30.8 151.2 139.2 122.6 76 4.7 70.8 4.4 17.1
26.1 74.8 103.3
1986 26.8 4.5 191.6 256.5 199.9 20.6 1 18.5 15.5
59.3 154 43.4
1987 50.2 38.3 87.7 159.5 143.9 1 5.1 57.6 0.2
9 10.6 101
1988 77.5 18.3 121.9 204.3 43.4 63.6 9.4 6.2 26.4
7.2 51.1 186.4
1989 320.2 0.2 80.9 175.2 190.1 19.9 1.6 34.3 3.9
71.2 73.1 141.1
1990 48.8 173.2 268.6 104.1 75.3 3.6 4.6 3.9 19.6
55 181.8 24.2
1991 137.4 0.1 64.3 64.4 208.8 2.1 12.7 8.7 7.5
10.9 55.6 72.4
1992 3.9 74.3 99.2 350.6 148.6 28.5 18 0.1 40.3
2.6 139.5 49.9
1993 77.6 89.6 92.2 306.2 147.7 4.3 35 7 12.1
61.8 36.9 5.2
1994 2.8 131.3 243.4 285.9 101.4 40 17.6 8.6 1.8
69.6 140.5 96
1995 13.1 71.7 302.5 302.3 307.1 0 3.5 61.1 22.7
31.3 11.2 75.3
1996 111.4 25.4 367.6 202.8 217.5 1.7 3.1 1.5 0
138.4 50.4 0
1997 0.4 28.4 292.4 146.1 104.5 89.5 1.1 3.6 2.6
275.6 141.2 233.3
1998 74.1 85.4 77.8 169.3 185.8 9.3 2 1.5 32.5
71.6 67.1 1.2
1999 70.3 25.2 130.5 254.9 136.8 59.4 15.5 13.4 4.1
14.5 96.1 104.5
2000 16.7 7.4 159.4 169.9 52.6 71.9 14.6 9.7 2.3
0 175.8 121.4
2001 114.2 59.4 205.4 294.9 110 11.6 0.6 4.5 0.2
7.4 2.2 54.2
2002 103.9 60 257.3 422.6 22.9 15 23.3 64.4 21.7
72.5 69.3 145.6
2003 10.5 2.2 41.7 40.7 90.5 8.2 1.4 2.1 1.5
24.2 37.3 30.9
2004 10.4 24.2 141.7 232.5 23.9 17.5 0.5 1.5 0.8
164.7 110.5 141.2
2005 24.4 24.2 80.3 172.6 240.5 2.8 11.2 10.2 2.1
9.7 29.5 67.4
2006 72.5 19.9 181.6 316.3 160.6 92.1 14.1 15.3 10.9
51.7 139.4 266
2007 38.5 36.9 124.8 183.4 70 16.7 7.4 16.5 6.3
26.9 168.5 73.2
2008 96.3 42.8 272 340.2 32.2 3.7 2.2 12.1 7.7
93.6 118.5 48.6
2009 29.1 70.7 134.6 113.8 52.6 22.3 1.4 0.7 0
29.4 25.5 159.4
2010 40.2 54.1 70.2 255.7 141.7 10 2.1 2.3 11.4
4.6 59.4 42
2011 20 25.6 203.6 244.2 74.6 7.4 0 5 41.1
28.5 139.3 247.7
2012 7.5 30.7 121.7 210.1 103.3 7.4 1.1 11.1 0
14 112 32.5
Year Jan Feb Mar Apr May Jun Jul Aug Sep
Oct Nov Dec
1982 277.7 181.3 241.7 55.4 62.3 0 0 0 0
24.5 147 281.6
1983 275.4 132.7 242.2 76.1 27.9 0 0 0 0
26.5 42 231.3
1984 451.4 196.5 213.5 52.3 4.6 1.4 6.8 0 0
0 127.3 130
1985 150.2 254.6 116.1 74.8 6.3 1.4 5.7 1 3.1
0 142.7 166.8
1986 427.1 170.5 244.5 86.5 2.7 2.7 0 0 0
11.3 59 342.9
1987 283.3 219.2 258.4 57.2 0 0 0 0 0
8.8 1.5 106.2
1988 226.8 138.2 129 42.5 1.1 0 0 0 0
1.6 42.9 133.2
1989 361 138.1 368.6 168.6 19.1 0 0 0 0
0 93.9 136.1
1990 151.8 140 140.9 100.8 15.4 0 3.4 0.2 0.8
7.2 74.1 210.6
1991 504.1 82.9 325.5 167.6 8 0 3.9 2.6 3.2
0 59.1 216
1992 162.1 193.9 225.9 60 12.3 3.9 0 0 0
0 134.2 105.6
1993 174.4 337.7 304.6 115.9 21.6 0 0 0.7 0
9 18.8 34.5
1994 339.8 226 359.7 44.2 0.2 1.4 0 1.7 0
5.7 27.5 133.6
1995 296.8 268.8 242.6 54.2 4.8 0 0 4.3 0
0 1.1 130.9
1996 140.7 451.1 274.3 70.9 22.8 0 0.2 0 0
0.3 9.2 210.3
1997 190.4 317.5 51.2 66.6 0 10.7 0 0 0
2.6 9.8 223.8
1998 336.9 191.2 207.1 66.5 1.4 0 0 0.4 19.2
1.2 43.6 29.2
1999 185.4 191.7 294.5 175.9 15.3 0 0 2 2.7
2.8 10 121.3
2000 191.4 162.7 189.3 120.6 11.4 1 0 3.1 0
0 293.9 618.5
2001 269.4 118.5 290.5 52.4 20.4 0 0 0 1.3
0 10.4 128.1
2002 360 192.8 298 116.3 1.7 2 0 0 0.2
1.1 87.2 198.3
2003 160.3 226.1 254.6 67.2 12.8 0 0 0.7 0
0 22.9 217.7
2004 218.5 214.4 160.6 128.2 0 1.6 0 0 0
2.7 125 246.4
2005 152.5 112 257.7 51.9 38 6.1 0 0 0
0.6 0 97.3
2006 140 170.9 177.7 194.6 2.1 0 3.3 2.4 0.7
0 92.2 390.1
2007 301.8 229 211.4 69.3 22.7 0 1.9 1 1.1
43 13.6 285.4
2008 222.1 298.6 168.8 58.5 14.8 0 0 5.1 0
1.3 73.6 162.8
2009 202.3 195.7 204.4 53.2 0.3 0 0 0 0
0.3 91.2 132.3
2010 326.1 230.9 151.1 67 15.4 2.3 0 1.4 0
2.1 0.3 159
2011 223.4 339 243.6 97.7 11.2 0.5 0 0.5 8.8
40 52.2 144
2012 286.7 111.4 172.2 58.6 12.3 0 0.2 0 0
0 82.7 171.9
Year Jan Feb Mar Apr May Jun Jul Aug Sep
Oct Nov Dec
1982 153.5 60.3 50.6 179.8 107.7 8.7 0 0 1.2
162.3 337.9 105.8
1983 92.9 99.2 99.1 147.4 21.3 0.5 0 67.1 0
139.1 63.1 113.8
1984 63.2 73.6 79 117.7 28.5 0 0 0 4.1
121.3 206.8 110.1
1985 101.7 110.2 104.4 131.1 11.2 27.6 0 0 2.1
22.3 228.6 241.9
1986 206.3 174.2 60.4 82.1 47.6 0 0 0 21
90.3 189.1 268.1
1987 55.6 67 74.1 219.9 17.7 4.1 0 0 95.9
91.2 104.8 78.4
1988 92.7 47.5 133.1 208.3 0.6 0 1.2 31.9 7.2
114.9 109.4 303.5
1989 149.4 114.3 141.7 130.2 24.3 0.3 2.2 0 19
77.4 138.8 121.1
1990 140.1 184.5 260.6 132.7 88.8 0 0 0.3 27.8
29.4 58.4 198.4
1991 90.7 118.5 35.5 86.6 147.1 0 0 3.6 82.4
117.4 183.3 208.5
1992 130.1 49.8 88.5 125.7 60.4 9.2 0 0 2.2
66.9 257.5 137.4
1993 154.3 67.3 157 67.1 28.6 0 0 5.9 0
21.7 118.2 171.9
1994 120.2 82.8 118.6 169.3 28.6 0 4.3 0 15.6
105.2 92.5 203.7
1995 224.2 89.5 146.9 120.1 23.7 56.3 0 0 7.5
160.6 100.9 78.2
1996 135.9 225.4 180.5 108.6 14.5 2.1 0 0 33.3
77.4 98.6 144
1997 193.1 15.6 107.2 136.4 86.9 0.5 0 0 0
227.6 122.4 155.4
1998 172.4 73.8 212 75.4 81.3 0 31.3 0 11
60.7 126.6 121.4
1999 125.1 30.5 208.2 141 0 0 0 23.7 21.2
71.6 280.7 129.8
2000 63.5 88.4 146.7 60.7 32.8 0 0 0.3 2
53.5 186.1 150.2
2001 161.5 59.6 228.3 31 37 17.7 0.5 22 30.6
64.1 71.4 91
2002 229.7 26.3 167.8 233 4.3 0 0 0 0
29 186.4 106.3
2003 164.7 153.1 161 170.9 6.5 8.3 0 3.8 22
73 91.8 134.9
2004 178.6 29.6 160 54.4 0 0 0 0 47.9
76.6 89.9 183.9
2005 264.8 29.6 89.3 35.1 81.5 5.5 0 0.2 0
22.7 153.7 59.8
2006 105.3 132.7 95.6 178.9 107.7 0 4.1 0.7 20.6
31 220.2 241.1
2007 96 22.2 132.3 123.5 53.1 12.2 0 0.3 12.2
56.4 191.9 95.8
2008 234.8 191.5 135.4 120.9 0 13.2 0.2 0 4
78.3 142.2 150.7
2009 62 86.8 190.2 167 23.9 18.4 0 0.1 2
23.2 148.7 87.4
2010 75.4 56.1 198.4 82.9 111.9 5.6 0.5 0 16
60.2 100.5 158.1
2011 74.9 148 230 81.2 19.6 0 10.6 2.6 91.6
89.8 175.5 154.4
2012 33 43.3 100.3 69.1 41.7 15.7 0 36.3 8.6
60.7 190.7 135.9
______________________________________________
R-help@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.