Rolling mean with changing window size on a large dataset - r

I want to compute the rolling mean over a vector whereby the window grows with each entry in the vector. Basically, I want to have the mean of all elements up to the i-th, i+1-th, i+2-th, and so forth.
To make it more clear, I'll provide an example and a solution which works for smaller datasets but does not scale up well:
library(zoo)
# data:
x <- 1:100
# solution:
rolling_average <- rollapply(x, seq_along(x), mean, align = "right")
# result:
rolling_average
# [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5
# [27] 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5 21.0 21.5 22.0 22.5 23.0 23.5 24.0 24.5 25.0 25.5 26.0 26.5
# [53] 27.0 27.5 28.0 28.5 29.0 29.5 30.0 30.5 31.0 31.5 32.0 32.5 33.0 33.5 34.0 34.5 35.0 35.5 36.0 36.5 37.0 37.5 38.0 38.5 39.0 39.5
# [79] 40.0 40.5 41.0 41.5 42.0 42.5 43.0 43.5 44.0 44.5 45.0 45.5 46.0 46.5 47.0 47.5 48.0 48.5 49.0 49.5 50.0 50.5
Using this approach for a vector with 500000 entries fills up my memory within seconds and renders my PC unusable. Alternatively, I've tried using roll_mean from RcppRoll, but wasn't able to come up with a solution because RcppRoll::roll_mean only accepts integers as window lengths.
So, what is the best approach to solve this problem on a large scale? Any help is greatly appreciated.

We can do
cumsum(x) / seq_along(x)
# [1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5
# [21] 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5
# [41] 21.0 21.5 22.0 22.5 23.0 23.5 24.0 24.5 25.0 25.5 26.0 26.5 27.0 27.5 28.0 28.5 29.0 29.5 30.0 30.5
# [61] 31.0 31.5 32.0 32.5 33.0 33.5 34.0 34.5 35.0 35.5 36.0 36.5 37.0 37.5 38.0 38.5 39.0 39.5 40.0 40.5
# [81] 41.0 41.5 42.0 42.5 43.0 43.5 44.0 44.5 45.0 45.5 46.0 46.5 47.0 47.5 48.0 48.5 49.0 49.5 50.0 50.5

We can use cummean
library(dplyr)
cummean(x)
#[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0
#[20] 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5
#[39] 20.0 20.5 21.0 21.5 22.0 22.5 23.0 23.5 24.0 24.5 25.0 25.5 26.0 26.5 27.0 27.5 28.0 28.5 29.0
#[58] 29.5 30.0 30.5 31.0 31.5 32.0 32.5 33.0 33.5 34.0 34.5 35.0 35.5 36.0 36.5 37.0 37.5 38.0 38.5
#[77] 39.0 39.5 40.0 40.5 41.0 41.5 42.0 42.5 43.0 43.5 44.0 44.5 45.0 45.5 46.0 46.5 47.0 47.5 48.0
#[96] 48.5 49.0 49.5 50.0 50.5

Related

convert ascii to netcdf format using r language

I have data in .asc format, given below.
ncols 241
nrows 291
xllcenter 91.33
yllcenter 5.00
cellsize 0.1
NODATA_value -999
133.0 136.0 138.0 141.0 144.0 147.0 150.0 152.0 155.0 157.0 159.0 162.0 164.0 167.0 159.0 147.0 135.0 123.0 111.0 98.0 86.0 84.0 81.0 79.0 76.0 74.0 71.0 70.0 70.0 70.0 71.0 71.0 72.0 72.0 84.0 101.0 118.0 134.0 151.0 168.0 185.0 197.0 209.0 222.0 234.0 246.0 258.0 266.0 266.0 266.0 266.0 265.0 265.0 265.0 254.0 237.0 221.0 204.0 188.0 171.0 155.0 156.0 158.0 160.0 161.0 163.0 164.0 165.0 162.0 159.0 156.0 153.0 150.0 147.0 144.0 140.0 137.0 133.0 130.0 126.0 123.0 120.0 117.0 114.0 111.0 108.0 105.0 102.0 99.0 97.0 94.0 92.0 89.0 86.0 85.0 84.0 83.0 82.0 81.0 80.0 79.0 78.0 77.0 76.0 75.0 73.0 72.0 71.0 70.0 70.0 69.0 68.0 67.0 67.0 66.0 65.0 64.0 63.0 62.0 61.0 60.0 59.0 58.0 57.0 56.0 55.0 54.0 53.0 52.0 51.0 51.0 50.0 49.0 48.0 47.0 46.0 45.0 45.0 44.0 43.0 42.0 40.0 39.0 38.0 37.0 35.0 34.0 33.0 32.0 31.0 30.0 29.0 28.0 27.0 27.0 26.0 26.0 25.0 25.0 24.0 24.0 23.0 22.0 21.0 21.0 20.0 19.0 19.0 18.0 17.0 16.0 15.0 15.0 14.0 13.0 12.0 11.0 11.0 10.0 9.0 9.0 8.0 7.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 4.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 8.0 9.0 9.0 10.0 10.0 10.0 10.0 11.0 11.0 12.0 12.0 13.0 13.0 14.0 15.0 15.0
133.0 137.0 140.0 143.0 146.0 149.0 153.0 155.0 158.0 160.0 162.0 164.0 166.0 168.0 161.0 148.0 135.0 123.0 110.0 97.0 85.0 83.0 81.0 78.0 76.0 74.0 72.0 71.0 72.0 72.0 73.0 74.0 74.0 75.0 86.0 103.0 120.0 137.0 154.0 171.0 188.0 199.0 210.0 220.0 231.0 242.0 252.0 259.0 259.0 259.0 258.0 258.0 258.0 257.0 247.0 231.0 215.0 199.0 184.0 168.0 152.0 155.0 158.0 161.0 164.0 167.0 169.0 170.0 166.0 162.0 158.0 154.0 150.0 145.0 142.0 138.0 134.0 130.0 126.0 122.0 118.0 115.0 112.0 110.0 107.0 104.0 101.0 98.0 96.0 93.0 91.0 88.0 86.0 83.0 82.0 81.0 80.0 79.0 78.0 77.0 75.0 74.0 73.0 72.0 71.0 70.0 69.0 68.0 67.0 67.0 66.0 65.0 65.0 64.0 63.0 63.0 62.0 61.0 60.0 59.0 59.0 58.0 57.0 56.0 55.0 54.0 53.0 52.0 52.0 51.0 50.0 49.0 48.0 48.0 47.0 46.0 45.0 44.0 43.0 42.0 42.0 40.0 39.0 38.0 37.0 35.0 34.0 33.0 32.0 31.0 31.0 30.0 29.0 28.0 27.0 27.0 26.0 26.0 25.0 24.0 24.0 23.0 23.0 22.0 21.0 21.0 20.0 19.0 18.0 17.0 17.0 16.0 15.0 14.0 13.0 13.0 12.0 11.0 10.0 10.0 9.0 8.0 8.0 7.0 7.0 6.0 5.0 5.0 4.0 4.0 4.0 3.0 3.0 2.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 8.0 8.0 9.0 9.0 10.0 10.0 11.0 11.0 12.0 12.0 13.0 14.0 14.0 15.0 16.0 16.0
134.0 138.0 141.0 145.0 148.0 152.0 156.0 159.0 161.0 162.0 164.0 166.0 168.0 170.0 162.0 149.0 136.0 123.0 110.0 96.0 83.0 82.0 80.0 78.0 77.0 75.0 73.0 72.0 73.0 74.0 75.0 76.0 77.0 78.0 89.0 106.0 123.0 140.0 157.0 174.0 191.0 200.0 210.0 219.0 228.0 238.0 247.0 253.0 252.0 252.0 251.0 251.0 250.0 250.0 240.0 225.0 210.0 194.0 179.0 164.0 149.0 153.0 158.0 162.0 166.0 170.0 174.0 176.0 170.0 165.0 160.0 154.0 149.0 144.0 139.0 135.0 131.0 126.0 122.0 118.0 114.0 111.0 108.0 105.0 102.0 100.0 97.0 94.0 92.0 90.0 87.0 85.0 83.0 81.0 79.0 78.0 77.0 75.0 74.0 73.0 72.0 71.0 70.0 69.0 68.0 66.0 65.0 65.0 64.0 64.0 63.0 63.0 62.0 62.0 61.0 61.0 60.0 59.0 59.0 58.0 57.0 57.0 56.0 55.0 54.0 53.0 52.0 52.0 51.0 50.0 49.0 49.0 48.0 47.0 46.0 46.0 45.0 44.0 43.0 42.0 41.0 40.0 39.0 38.0 37.0 35.0 34.0 33.0 32.0 32.0 31.0 30.0 29.0 29.0 28.0 27.0 27.0 26.0 25.0 25.0 24.0 24.0 23.0 22.0 22.0 21.0 20.0 20.0 19.0 18.0 17.0 16.0 16.0 15.0 14.0 13.0 12.0 12.0 11.0 10.0 9.0 9.0 8.0 7.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 4.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 8.0 9.0 9.0 10.0 10.0 11.0 12.0 12.0 13.0 14.0 14.0 15.0 16.0 17.0 17.0
135.0 139.0 143.0 147.0 151.0 155.0 159.0 162.0 163.0 165.0 167.0 169.0 170.0 172.0 164.0 150.0 136.0 123.0 109.0 95.0 82.0 81.0 79.0 78.0 77.0 76.0 74.0 74.0 75.0 76.0 77.0 78.0 79.0 80.0 92.0 109.0 126.0 143.0 160.0 177.0 194.0 202.0 210.0 218.0 226.0 233.0 241.0 246.0 246.0 245.0 244.0 244.0 243.0 242.0 232.0 218.0 204.0 190.0 175.0 161.0 147.0 152.0 158.0 163.0 169.0 174.0 180.0 181.0 175.0 168.0 162.0 155.0 149.0 142.0 137.0 132.0 128.0 123.0 118.0 114.0 109.0 106.0 104.0 101.0 98.0 96.0 93.0 90.0 88.0 86.0 84.0 82.0 80.0 78.0 76.0 75.0 73.0 72.0 71.0 69.0 68.0 67.0 66.0 65.0 64.0 63.0 62.0 61.0 61.0 61.0 60.0 60.0 60.0 59.0 59.0 59.0 58.0 58.0 57.0 57.0 56.0 55.0 55.0 54.0 53.0 53.0 52.0 51.0 50.0 50.0 49.0 48.0 47.0 47.0 46.0 45.0 44.0 44.0 43.0 42.0 41.0 40.0 39.0 38.0 37.0 36.0 34.0 33.0 33.0 32.0 31.0 31.0 30.0 29.0 29.0 28.0 27.0 27.0 26.0 25.0 25.0 24.0 23.0 23.0 22.0 21.0 21.0 20.0 19.0 18.0 18.0 17.0 16.0 15.0 14.0 14.0 13.0 12.0 11.0 10.0 10.0 9.0 8.0 8.0 7.0 7.0 6.0 5.0 5.0 5.0 4.0 4.0 3.0 3.0 2.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 4.0 4.0 5.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 8.0 9.0 10.0 11.0 11.0 12.0 13.0 13.0 14.0 15.0 16.0 17.0 18.0 18.0
135.0 140.0 144.0 148.0 153.0 157.0 161.0 165.0 166.0 168.0 169.0 171.0 172.0 174.0 165.0 151.0 137.0 123.0 109.0 94.0 80.0 80.0 79.0 78.0 77.0 76.0 76.0 75.0 77.0 78.0 79.0 80.0 82.0 83.0 95.0 112.0 129.0 146.0 163.0 180.0 197.0 204.0 210.0 216.0 223.0 229.0 236.0 240.0 239.0 238.0 237.0 236.0 235.0 235.0 225.0 212.0 198.0 185.0 171.0 157.0 144.0 151.0 157.0 164.0 171.0 178.0 185.0 186.0 179.0 171.0 163.0 156.0 148.0 140.0 134.0 129.0 124.0 120.0 115.0 110.0 105.0 102.0 99.0 97.0 94.0 92.0 89.0 87.0 85.0 83.0 81.0 79.0 77.0 75.0 73.0 72.0 70.0 69.0 67.0 66.0 65.0 64.0 63.0 62.0 61.0 59.0 58.0 58.0 58.0 58.0 57.0 57.0 57.0 57.0 57.0 56.0 56.0 56.0 56.0 55.0 55.0 54.0 54.0 53.0 52.0 52.0 51.0 51.0 50.0 49.0 48.0 48.0 47.0 46.0 45.0 45.0 44.0 43.0 43.0 42.0 41.0 40.0 39.0 38.0 37.0 36.0 35.0 34.0 33.0 32.0 32.0 31.0 31.0 30.0 29.0 29.0 28.0 27.0 26.0 26.0 25.0 24.0 24.0 23.0 22.0 22.0 21.0 21.0 20.0 19.0 18.0 17.0 17.0 16.0 15.0 14.0 13.0 13.0 12.0 11.0 10.0 9.0 9.0 8.0 8.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 4.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 8.0 9.0 10.0 11.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 19.0
136.0 141.0 145.0 150.0 155.0 160.0 164.0 168.0 169.0 171.0 172.0 173.0 175.0 176.0 167.0 152.0 137.0 123.0 108.0 94.0 79.0 79.0 78.0 78.0 77.0 77.0 77.0 77.0 78.0 80.0 81.0 83.0 84.0 86.0 97.0 115.0 132.0 149.0 166.0 183.0 200.0 205.0 210.0 215.0 220.0 225.0 230.0 233.0 232.0 231.0 230.0 229.0 228.0 227.0 218.0 205.0 192.0 180.0 167.0 154.0 141.0 149.0 157.0 165.0 173.0 181.0 190.0 192.0 183.0 174.0 165.0 156.0 148.0 139.0 132.0 127.0 121.0 116.0 111.0 105.0 100.0 97.0 95.0 93.0 90.0 88.0 85.0 83.0 81.0 79.0 77.0 75.0 74.0 72.0 70.0 69.0 67.0 66.0 64.0 62.0 61.0 60.0 59.0 58.0 57.0 56.0 55.0 54.0 54.0 54.0 55.0 55.0 55.0 55.0 55.0 54.0 54.0 54.0 54.0 54.0 54.0 53.0 53.0 52.0 52.0 51.0 51.0 50.0 49.0 48.0 48.0 47.0 46.0 46.0 45.0 44.0 44.0 43.0 42.0 42.0 41.0 40.0 39.0 38.0 37.0 36.0 35.0 34.0 33.0 33.0 32.0 32.0 31.0 31.0 30.0 29.0 28.0 28.0 27.0 26.0 25.0 25.0 24.0 23.0 23.0 22.0 22.0 21.0 20.0 19.0 19.0 18.0 17.0 16.0 15.0 15.0 14.0 13.0 12.0 11.0 11.0 10.0 9.0 9.0 8.0 7.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 4.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 19.0 20.0
136.0 140.0 145.0 150.0 154.0 159.0 164.0 167.0 168.0 170.0 171.0 172.0 173.0 174.0 165.0 150.0 135.0 121.0 106.0 91.0 77.0 76.0 76.0 76.0 76.0 76.0 76.0 76.0 77.0 79.0 80.0 82.0 83.0 84.0 97.0 114.0 132.0 149.0 167.0 184.0 202.0 207.0 213.0 218.0 224.0 229.0 235.0 238.0 236.0 233.0 231.0 229.0 227.0 225.0 218.0 210.0 201.0 193.0 184.0 176.0 167.0 172.0 177.0 182.0 187.0 192.0 197.0 197.0 188.0 179.0 171.0 162.0 153.0 144.0 136.0 130.0 123.0 117.0 110.0 104.0 97.0 95.0 92.0 90.0 87.0 85.0 82.0 80.0 78.0 77.0 75.0 73.0 71.0 70.0 68.0 67.0 65.0 64.0 63.0 61.0 60.0 59.0 58.0 57.0 57.0 56.0 55.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 53.0 53.0 53.0 53.0 52.0 52.0 52.0 51.0 51.0 50.0 50.0 49.0 48.0 48.0 47.0 47.0 46.0 45.0 45.0 44.0 43.0 43.0 42.0 42.0 41.0 40.0 39.0 38.0 37.0 36.0 35.0 34.0 34.0 33.0 33.0 32.0 32.0 31.0 30.0 29.0 28.0 28.0 27.0 26.0 25.0 25.0 24.0 24.0 23.0 22.0 22.0 21.0 20.0 19.0 18.0 18.0 17.0 16.0 15.0 14.0 13.0 13.0 12.0 11.0 10.0 10.0 9.0 8.0 8.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 4.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0 20.0
136.0 140.0 145.0 149.0 154.0 158.0 163.0 166.0 167.0 169.0 170.0 171.0 172.0 173.0 163.0 148.0 134.0 119.0 104.0 89.0 74.0 74.0 74.0 74.0 74.0 74.0 75.0 75.0 76.0 78.0 79.0 81.0 82.0 83.0 96.0 114.0 132.0 149.0 167.0 185.0 203.0 209.0 215.0 221.0 227.0 233.0 240.0 243.0 239.0 236.0 233.0 229.0 226.0 223.0 219.0 215.0 210.0 206.0 202.0 198.0 194.0 195.0 197.0 199.0 201.0 203.0 204.0 203.0 194.0 185.0 176.0 167.0 158.0 149.0 141.0 133.0 125.0 117.0 110.0 102.0 94.0 92.0 89.0 87.0 84.0 82.0 79.0 77.0 76.0 74.0 72.0 71.0 69.0 67.0 66.0 65.0 64.0 63.0 61.0 60.0 59.0 58.0 58.0 57.0 56.0 55.0 55.0 54.0 54.0 54.0 54.0 54.0 53.0 53.0 53.0 53.0 53.0 53.0 52.0 52.0 52.0 52.0 51.0 51.0 51.0 50.0 50.0 49.0 49.0 48.0 48.0 47.0 47.0 46.0 46.0 45.0 45.0 44.0 43.0 43.0 42.0 41.0 41.0 40.0 39.0 38.0 37.0 36.0 36.0 35.0 34.0 34.0 33.0 32.0 32.0 31.0 30.0 29.0 29.0 28.0 27.0 26.0 26.0 25.0 24.0 24.0 23.0 22.0 22.0 21.0 20.0 19.0 18.0 17.0 16.0 15.0 15.0 14.0 13.0 12.0 11.0 11.0 10.0 9.0 9.0 8.0 7.0 7.0 6.0 6.0 5.0 5.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 4.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 8.0 9.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0
135.0 140.0 144.0 149.0 153.0 158.0 162.0 166.0 166.0 167.0 168.0 169.0 170.0 171.0 162.0 147.0 132.0 117.0 102.0 87.0 72.0 72.0 72.0 73.0 73.0 73.0 74.0 74.0 76.0 77.0 78.0 80.0 81.0 82.0 95.0 113.0 131.0 150.0 168.0 186.0 204.0 211.0 218.0 224.0 231.0 238.0 244.0 247.0 243.0 238.0 234.0 229.0 225.0 220.0 219.0 219.0 219.0 219.0 220.0 220.0 220.0 219.0 217.0 216.0 215.0 213.0 212.0 208.0 199.0 190.0 181.0 172.0 163.0 154.0 145.0 136.0 127.0 118.0 109.0 100.0 91.0 89.0 86.0 84.0 81.0 79.0 77.0 74.0 73.0 71.0 70.0 68.0 67.0 65.0 64.0 63.0 62.0 61.0 60.0 59.0 58.0 58.0 57.0 56.0 56.0 55.0 54.0 54.0 54.0 53.0 53.0 53.0 53.0 53.0 52.0 52.0 52.0 52.0 52.0 51.0 51.0 51.0 51.0 50.0 50.0 50.0 49.0 49.0 49.0 48.0 48.0 48.0 47.0 47.0 46.0 46.0 45.0 45.0 44.0 44.0 43.0 42.0 41.0 41.0 40.0 39.0 38.0 38.0 37.0 36.0 35.0 35.0 34.0 33.0 33.0 32.0 31.0 30.0 30.0 29.0 28.0 27.0 27.0 26.0 25.0 25.0 24.0 23.0 22.0 21.0 20.0 19.0 19.0 18.0 17.0 16.0 15.0 14.0 13.0 13.0 12.0 11.0 10.0 10.0 9.0 8.0 7.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 4.0 4.0 4.0 5.0 5.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 19.0
135.0 140.0 144.0 148.0 153.0 157.0 161.0 165.0 166.0 166.0 167.0 168.0 169.0 170.0 160.0 145.0 130.0 115.0 100.0 84.0 69.0 70.0 70.0 71.0 71.0 72.0 73.0 73.0 75.0 76.0 77.0 79.0 80.0 81.0 94.0 113.0 131.0 150.0 169.0 187.0 206.0 213.0 220.0 228.0 235.0 242.0 249.0 252.0 246.0 241.0 235.0 229.0 224.0 218.0 219.0 224.0 228.0 233.0 237.0 242.0 246.0 242.0 237.0 233.0 228.0 224.0 219.0 213.0 204.0 195.0 186.0 177.0 168.0 159.0 150.0 139.0 129.0 119.0 109.0 98.0 88.0 86.0 83.0 81.0 79.0 76.0 74.0 72.0 70.0 69.0 67.0 66.0 65.0 63.0 62.0 61.0 61.0 60.0 59.0 58.0 57.0 57.0 56.0 56.0 55.0 55.0 54.0 54.0 53.0 53.0 53.0 53.0 52.0 52.0 52.0 52.0 51.0 51.0 51.0 51.0 50.0 50.0 50.0 50.0 50.0 49.0 49.0 49.0 49.0 48.0 48.0 48.0 47.0 47.0 47.0 46.0 46.0 45.0 45.0 44.0 44.0 43.0 42.0 42.0 41.0 40.0 40.0 39.0 38.0 37.0 37.0 36.0 35.0 34.0 33.0 33.0 32.0 31.0 31.0 30.0 29.0 28.0 28.0 27.0 26.0 25.0 25.0 24.0 23.0 22.0 21.0 20.0 19.0 18.0 17.0 16.0 16.0 15.0 14.0 13.0 12.0 11.0 11.0 10.0 9.0 8.0 8.0 7.0 7.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 4.0 4.0 4.0 4.0 5.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0
135.0 139.0 144.0 148.0 152.0 156.0 161.0 164.0 165.0 165.0 166.0 167.0 168.0 168.0 158.0 143.0 128.0 113.0 97.0 82.0 67.0 68.0 68.0 69.0 70.0 71.0 71.0 72.0 74.0 75.0 76.0 78.0 79.0 80.0 93.0 112.0 131.0 150.0 169.0 188.0 207.0 215.0 223.0 231.0 238.0 246.0 254.0 257.0 250.0 243.0 236.0 230.0 223.0 216.0 220.0 228.0 237.0 246.0 255.0 264.0 272.0 265.0 257.0 250.0 242.0 234.0 227.0 219.0 210.0 201.0 192.0 183.0 174.0 165.0 154.0 142.0 131.0 120.0 108.0 97.0 85.0 83.0 80.0 78.0 76.0 73.0 71.0 69.0 68.0 66.0 65.0 64.0 62.0 61.0 60.0 60.0 59.0 58.0 58.0 57.0 56.0 56.0 56.0 55.0 55.0 54.0 54.0 53.0 53.0 53.0 52.0 52.0 52.0 51.0 51.0 51.0 51.0 50.0 50.0 50.0 49.0 49.0 49.0 49.0 49.0 49.0 49.0 49.0 48.0 48.0 48.0 48.0 48.0 48.0 47.0 47.0 46.0 46.0 45.0 45.0 44.0 44.0 43.0 43.0 42.0 41.0 41.0 40.0 39.0 38.0 38.0 37.0 36.0 35.0 34.0 34.0 33.0 32.0 32.0 31.0 30.0 29.0 29.0 28.0 27.0 26.0 25.0 25.0 24.0 23.0 22.0 21.0 20.0 19.0 18.0 17.0 16.0 15.0 14.0 13.0 12.0 12.0 11.0 10.0 10.0 9.0 8.0 7.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 3.0 4.0 4.0 4.0 5.0 6.0 7.0 7.0 8.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 17.0 18.0
135.0 139.0 143.0 148.0 152.0 156.0 160.0 163.0 164.0 164.0 165.0 165.0 166.0 167.0 157.0 142.0 127.0 112.0 97.0 82.0 67.0 68.0 69.0 69.0 70.0 71.0 72.0 73.0 74.0 75.0 76.0 78.0 79.0 80.0 93.0 111.0 129.0 147.0 165.0 184.0 202.0 212.0 223.0 234.0 244.0 255.0 266.0 272.0 269.0 266.0 263.0 260.0 257.0 255.0 257.0 261.0 265.0 269.0 274.0 278.0 282.0 273.0 264.0 255.0 246.0 237.0 228.0 219.0 213.0 206.0 199.0 193.0 186.0 180.0 169.0 156.0 143.0 130.0 117.0 104.0 91.0 88.0 85.0 82.0 78.0 75.0 72.0 70.0 68.0 67.0 66.0 64.0 63.0 62.0 61.0 60.0 59.0 59.0 58.0 57.0 56.0 56.0 56.0 55.0 55.0 55.0 54.0 54.0 54.0 53.0 53.0 53.0 52.0 52.0 52.0 52.0 51.0 51.0 51.0 51.0 51.0 51.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 50.0 49.0 49.0 49.0 49.0 48.0 48.0 47.0 47.0 46.0 46.0 45.0 45.0 44.0 43.0 43.0 42.0 41.0 41.0 40.0 39.0 38.0 37.0 36.0 36.0 35.0 34.0 33.0 33.0 32.0 31.0 30.0 29.0 29.0 28.0 27.0 26.0 25.0 24.0 23.0 22.0 21.0 20.0 19.0 18.0 17.0 16.0 16.0 15.0 14.0 13.0 12.0 11.0 11.0 10.0 9.0 8.0 8.0 7.0 7.0 6.0 6.0 5.0 4.0 4.0 4.0 3.0 3.0 2.0 2.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 3.0 4.0 4.0 4.0 5.0 6.0 7.0 8.0 9.0 9.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0
135.0 139.0 143.0 147.0 151.0 155.0 159.0 162.0 162.0 163.0 164.0 164.0 165.0 165.0 156.0 141.0 126.0 111.0 97.0 82.0 67.0 68.0 69.0 69.0 70.0 71.0 72.0 73.0 74.0 75.0 77.0 78.0 79.0 80.0 92.0 110.0 127.0 144.0 162.0 179.0 196.0 210.0 223.0 237.0 250.0 264.0 277.0 286.0 288.0 289.0 290.0 291.0 292.0 293.0 294.0 293.0 293.0 293.0 293.0 292.0 292.0 281.0 271.0 260.0 250.0 239.0 229.0 220.0 216.0 212.0 207.0 203.0 199.0 194.0 183.0 169.0 154.0 140.0 126.0 111.0 97.0 93.0 89.0 85.0 81.0 77.0 74.0 71.0 69.0 68.0 66.0 65.0 64.0 62.0 61.0 60.0 60.0 59.0 58.0 57.0 57.0 56.0 56.0 56.0 55.0 55.0 55.0 54.0 54.0 54.0 54.0 53.0 53.0 53.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 52.0 51.0 51.0 51.0 51.0 51.0 51.0 50.0 50.0 49.0 49.0 49.0 48.0 48.0 47.0 46.0 46.0 45.0 44.0 44.0 43.0 42.0 41.0 40.0 40.0 39.0 38.0 37.0 36.0 35.0 35.0 34.0 33.0 32.0 31.0 30.0 29.0 28.0 27.0 27.0 26.0 25.0 24.0 23.0 22.0 21.0 20.0 19.0 18.0 17.0 16.0 15.0 14.0 13.0 13.0 12.0 11.0 10.0 10.0 9.0 8.0 8.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 4.0 3.0 3.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 3.0 3.0 4.0 5.0 5.0 6.0 7.0 8.0 8.0 9.0 10.0 11.0 11.0 12.0 13.0 14.0
135.0 139.0 143.0 147.0 151.0 154.0 158.0 161.0 161.0 162.0 162.0 163.0 163.0 164.0 154.0 140.0 125.0 111.0 96.0 82.0 67.0 68.0 69.0 70.0 70.0 71.0 72.0 73.0 74.0 75.0 77.0 78.0 79.0 80.0 92.0 108.0 125.0 141.0 158.0 174.0 191.0 207.0 223.0 240.0 256.0 272.0 289.0 301.0 306.0 311.0 317.0 322.0 327.0 332.0 331.0 326.0 321.0 316.0 311.0 307.0 302.0 290.0 278.0 266.0 254.0 242.0 230.0 221.0 219.0 217.0 215.0 213.0 211.0 209.0 198.0 182.0 166.0 150.0 134.0 118.0 102.0 98.0 93.0 89.0 84.0 80.0 75.0 71.0 70.0 69.0 67.0 66.0 64.0 63.0 62.0 61.0 60.0 59.0 58.0 57.0 57.0 56.0 56.0 56.0 56.0 55.0 55.0 55.0 55.0 54.0 54.0 54.0 54.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 53.0 52.0 52.0 52.0 52.0 51.0 51.0 50.0 50.0 49.0 48.0 48.0 47.0 46.0 46.0 45.0 44.0 43.0 43.0 42.0 41.0 40.0 39.0 38.0 37.0 37.0 36.0 35.0 34.0 33.0 32.0 31.0 30.0 29.0 28.0 27.0 26.0 25.0 24.0 23.0 22.0 21.0 20.0 19.0 18.0 17.0 16.0 16.0 15.0 14.0 13.0 12.0 12.0 11.0 10.0 9.0 9.0 8.0 7.0 7.0 6.0 6.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 3.0 3.0 3.0 4.0 5.0 5.0 6.0 7.0 7.0 8.0 9.0 9.0 10.0 11.0 11.0 12.0
136.0 139.0 143.0 146.0 150.0 154.0 157.0 160.0 160.0 161.0 161.0 162.0 162.0 162.0 153.0 139.0 124.0 110.0 96.0 82.0 67.0 68.0 69.0 70.0 71.0 71.0 72.0 73.0 74.0 76.0 77.0 78.0 79.0 80.0 91.0 107.0 123.0 138.0 154.0 170.0 185.0 204.0 224.0 243.0 262.0 281.0 300.0 316.0 325.0 334.0 343.0 352.0 362.0 371.0 368.0 358.0 349.0 340.0 330.0 321.0 312.0 298.0 285.0 271.0 258.0 244.0 231.0 222.0 222.0 223.0 223.0 224.0 224.0 224.0 213.0 195.0 178.0 160.0 143.0 126.0 108.0 103.0 98.0 92.0 87.0 82.0 76.0 72.0 71.0 69.0 68.0 66.0 65.0 63.0 62.0 61.0 60.0 59.0 58.0 58.0 57.0 56.0 56.0 56.0 56.0 56.0 55.0 55.0 55.0 55.0 55.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 54.0 53.0 53.0 52.0 52.0 51.0 51.0 50.0 49.0 49.0 48.0 47.0 47.0 46.0 45.0 44.0 43.0 42.0 41.0 41.0 40.0 39.0 38.0 37.0 36.0 35.0 34.0 33.0 32.0 31.0 30.0 29.0 28.0 27.0 26.0 25.0 24.0 23.0 22.0 20.0 20.0 19.0 18.0 17.0 16.0 15.0 14.0 14.0 13.0 12.0 11.0 10.0 10.0 9.0 8.0 8.0 7.0 7.0 6.0 5.0 5.0 5.0 4.0 4.0 3.0 3.0 3.0 2.0 2.0 2.0 2.0 2.0 2.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 2.0 3.0 3.0 3.0 4.0 4.0 5.0 5.0 6.0 6.0 7.0 7.0 8.0 8.0 9.0 10.0 10.0
136.0 139.0 143.0 146.0 150.0 153.0 157.0 159.0 159.0 160.0 160.0 160.0 160.0 161.0 152.0 138.0 123.0 109.0 95.0 81.0 67.0 68.0 69.0 70.0 71.0 72.0 73.0 73.0 75.0 76.0 77.0 78.0 79.0 80.0 91.0 105.0 120.0 135.0 150.0 165.0 180.0 202.0 224.0 246
Now I want to convert into NetCDF file. I imported the .asc file using
asc = import.asc("/Users/Pushp/Desktop/test/CF/2004/Eta-20040101-India-RH50.asc")
then change into raster
raster = raster(asc)
Now I want to change this raster file into NetCDF file?
You can do:
library(raster)
asc <- raster("Eta-20040101-India-RH50.asc")
x <- writeRaster(asc, "eta.nc")

Subsetting and Looping a Time Series Data in R

I have a dataset of timeseries (30 years). I did a subset for the month and the date I want (shown below in the code). Is there a way to do a loop for each month and the days in those month? Also, is there a way to save the plots automatically, in different folders corresponding to each month? Right now I am doing it manually by changing the month and date which corresponds to dfOct31all <- df [ which(df$Month==10 & df$Day==31), ]in the code below then plotting and saving it. By the way, I'm using RStudio.
Can someone please guide me?
Thanks!
setwd("WDir")
df <- read.csv("Velocity.csv", header = TRUE)
attach(df)
#Day 31
dfOct31all <- df [ which(df$Month==10 & df$Day==31), ]
dfall31Mbs <- dfOct31all[c(-1,-2,-3)]
densities <- lapply(dfall31Mbs, density)
par(mfcol=c(5,5), oma=c(1,1,0,0), mar=c(1,1,1,0), tcl=-0.1, mgp=c(0,0,0))
plot(densities[[1]], col="black",main = "1000mb",xlab=NA,ylab=NA)
plot(densities[[2]], col="black",main="925mb",xlab=NA,ylab=NA)
plot(densities[[3]], col="black",main="850mb",xlab=NA,ylab=NA)
plot(densities[[4]], col="black",main="700mb",xlab=NA,ylab=NA)
plot(densities[[5]], col="black",main="600mb",xlab=NA,ylab=NA)
plot(densities[[6]], col="black",main="500mb",xlab=NA,ylab=NA)
plot(densities[[7]], col ="black",main="400mb",xlab=NA,ylab=NA)
plot(densities[[8]], col="black",main="300mb",xlab=NA,ylab=NA)
plot(densities[[9]], col="black",main="250mb",xlab=NA,ylab=NA)
plot(densities[[10]], col="black",main="200mb",xlab=NA,ylab=NA)
plot(densities[[11]], col= "black",main="150mb",xlab=NA,ylab=NA)
plot(densities[[12]], col= "black",main="100mb",xlab=NA,ylab=NA)
plot(densities[[13]], col = "black",main="70mb",xlab=NA,ylab=NA)
plot(densities[[14]], col="black",main="50mb",xlab=NA,ylab=NA)
plot(densities[[15]], col="black",main="30mb",xlab=NA,ylab=NA)
plot(densities[[16]], col = "black",main="20mb",xlab=NA,ylab=NA)
plot(densities[[17]], col="black",main="10mb",xlab=NA,ylab=NA)
Snippet of data is shown as well
Year Month Day 1000mb 925mb 850mb 700mb 600mb 500mb 400mb 300mb 250mb 200mb 150mb 100mb 70mb 50mb 30mb 20mb 10mb
1984 10 31 6 6.6 7.9 11.5 14.6 17 20.8 25.8 26.4 25.3 24.4 22.7 19.9 19.2 20.4 24.8 30.8
1985 10 31 5.8 7.1 7.7 11.5 14.7 17.3 25.3 32.6 32.9 32.4 27.1 20.9 14.2 9.7 6.4 7.3 7.4
1986 10 31 4.3 6.1 7.7 11.3 18.4 26.3 34.4 44.5 48.9 46.2 34.5 20.4 13.8 13.2 21.7 31 46.4
1987 10 31 2.2 2.9 4 7 9 13.9 19.9 25.8 26.6 23.7 17.3 12 7 3.1 1.7 5.8 14.1
1988 10 31 2.5 2.1 2.3 6.5 6.4 5.1 7.4 12.1 13.4 16.1 16.7 15.2 8.8 5 2.8 6.2 8.9
1989 10 31 3.4 4 4.7 4.4 4.1 4 4.6 4.8 5.9 5.6 10.9 13.9 12.3 10.4 8.1 8 8
1990 10 31 4 4.9 7.5 14.6 19 21.9 25.7 28.3 29.4 29.2 27.3 18 12.6 10.1 9 12 19.9
1991 10 31 2.8 3.2 4 10.8 12.1 11.2 9.9 9.1 9.9 12.8 18 17.5 10.4 6.3 4.2 7.6 11.7
1992 10 31 5.9 6.9 7.9 13.1 17.9 25.2 34.6 47.3 53.3 53 42.4 21.3 11.6 6 4.6 8.5 12.8
1993 10 31 2.3 1.5 0.4 3.6 6.3 10.1 14.3 19.1 21.6 21.8 18.4 13.6 12.3 9.5 6.9 11 18.1
1994 10 31 2 2.2 3.8 11.6 17 19.8 23.6 24.9 25.5 26.2 28.4 25.2 16.7 13.6 9.3 8.3 9.8
1995 10 31 1.5 2 3.4 7.6 9.1 11.2 13.7 17.9 20.3 21.7 21.1 16.7 13 12.1 14.9 21.4 27.3
1996 10 31 1.9 2.4 3.5 8 11.7 17.4 26.4 35.6 33.3 24.6 12.4 4.1 0.5 3.4 7.2 9.4 11.6
1997 10 31 3.7 4.8 7.8 19.2 24.6 29.6 35.6 41 41.8 42 37.9 23.7 11.2 8.6 4.2 3.8 7
1998 10 31 0.7 1.1 0.9 4.8 8.4 11.4 14 25.3 29.7 25.2 15.9 6.6 2.1 1 4.5 8.9 6.1
1999 10 31 1.9 1.6 2.4 10.7 15.3 19 23.2 29 32.4 31.9 28 20.3 10.8 9.4 12 14.5 16.9
2000 10 31 5.1 5.8 6.7 12.8 18.2 23.9 29.9 40.7 42.2 33.7 23.5 12.7 2.6 1.6 3.8 4.7 5.1
2001 10 31 5.7 6.1 7.1 10.1 10.8 14.7 18.3 22.8 22.3 22.2 22 14 9.5 6.6 5.2 6.5 8.6
2002 10 31 1.4 1.6 1.8 9.2 14.5 19.5 24.8 30 30.5 27.6 22.2 13.9 9.1 7.1 8.5 16.1 23.8
2003 10 31 1.5 1.3 0.7 1 3.5 6 11.7 21.5 21.9 22.9 23 20.7 15.8 12.5 14.5 20.1 26
2004 10 31 5.4 5.6 6.9 14.4 23.3 33.3 46.1 60.9 62.1 54.6 42.9 28 17.3 12.3 10.1 13.6 13.3
2005 10 31 1.7 1.3 3 10.3 15.8 19.5 21.1 22.8 24.1 24.5 24.5 20.6 13.5 10.7 10 10.7 10.4
2006 10 31 2.3 1.5 1.7 8.7 12.5 15.9 18.7 20.5 21.8 24.3 29.9 25.3 18.3 12.8 7.7 8.8 12.4
2007 10 31 3.7 2.7 2.3 2.2 2.6 4.2 6.5 11.9 15.9 19.6 17.2 9.5 6.9 5.7 4.9 5.8 11.7
2008 10 31 7.7 10.8 14.3 20.3 23 25.8 27.4 32.1 35.4 34.8 25.8 13.2 7.1 2.9 2.6 3.4 6
2009 10 31 0.5 0.2 2 9.3 13.5 17.6 18.8 20.8 21.4 21.2 18.9 14.2 11.1 6.4 1.9 3 8
2010 10 31 5.6 6.8 8.5 13.4 16.5 20.3 23.8 26.8 31 28.1 24 15.7 9.9 7 4.8 3.9 1.8
2011 10 31 5.9 6.7 5.6 7.9 10.3 11.8 12.5 16.2 19.5 21.4 17.9 13.2 9.6 7.9 8 8.3 10.8
2012 10 31 4.8 6.3 9.4 19.5 24.2 27.2 27.5 27.3 27.7 30.7 27.5 16.7 10 7.6 8 13.8 19.7
2013 10 31 1.4 1.9 3.9 9.1 13.1 17.3 22.9 29.7 30.4 27.3 23.5 18.2 13.1 6.3 4.4 2.4 9.4
I wrote it out for each day rather than doing a loop.

Draw regression line per row in R

I have the following data.
HEIrank1
HEI.ID X2007 X2008 X2009 X2010 X2011 X2012
1 OP 41.8 147.6 90.3 82.9 106.8 63.0
2 MO 20.0 20.8 21.1 20.9 12.6 20.6
3 SD 21.2 32.3 25.7 23.9 25.0 40.1
4 UN 51.8 39.8 19.9 20.9 21.6 22.5
5 WS 18.0 19.9 15.3 13.6 15.7 15.2
6 BF 11.5 36.9 20.0 23.2 18.2 23.8
7 ME 34.2 30.3 28.4 30.1 31.5 25.6
8 IM 7.7 18.1 20.5 14.6 17.2 17.1
9 OM 11.4 11.2 12.2 11.1 13.4 19.2
10 DC 14.3 28.7 20.1 17.0 22.3 16.2
11 OC 28.6 44.0 24.9 27.9 34.0 30.7
12 TH 7.4 10.0 5.8 8.8 8.7 8.6
13 CC 12.1 11.0 12.2 12.1 14.9 15.0
14 MM 11.7 24.2 18.4 18.6 31.9 31.7
15 MC 19.0 13.7 17.0 20.4 20.5 12.1
16 SH 11.4 24.8 26.1 12.7 19.9 25.9
17 SB 13.0 22.8 15.9 17.6 17.2 9.6
18 SN 11.5 18.6 22.9 12.0 20.3 11.6
19 ER 10.8 13.2 20.0 11.0 14.9 14.2
20 SL 44.9 21.6 21.3 26.5 17.0 8.0
I try following commends to draw regression line for each HEIs.
year <- c(2007 , 2008 , 2009 , 2010 , 2011, 2012)
op <- as.numeric(HEIrank1[1,])
lm.r <- lm(op~year)
plot(year, op)
abline(lm.r)
I want to draw to draw regression line for each college in one graph and I do not how.can you help me.
Here's my approach with ggplot2 but the graph is uninterpretable with that many lines.
library(ggplot2);library(reshape2)
mdat <- melt(HEIrank1, variable.name="year")
mdat$year <- as.numeric(substring(mdat$year, 2))
ggplot(mdat, aes(year, value, colour=HEI.ID, group=HEI.ID)) +
geom_point() + stat_smooth(se = FALSE, method="lm")
Faceting may be a better way to got:
ggplot(mdat, aes(year, value, group=HEI.ID)) +
geom_point() + stat_smooth(se = FALSE, method="lm") +
facet_wrap(~HEI.ID)

Draw histograms per row over multiple columns in R

I'm using R for the analysis of my master thesis
I have the following data frame: STOF: Student to staff ratio
HEI.ID X2007 X2008 X2009 X2010 X2011 X2012
1 OP 41.8 147.6 90.3 82.9 106.8 63.0
2 MO 20.0 20.8 21.1 20.9 12.6 20.6
3 SD 21.2 32.3 25.7 23.9 25.0 40.1
4 UN 51.8 39.8 19.9 20.9 21.6 22.5
5 WS 18.0 19.9 15.3 13.6 15.7 15.2
6 BF 11.5 36.9 20.0 23.2 18.2 23.8
7 ME 34.2 30.3 28.4 30.1 31.5 25.6
8 IM 7.7 18.1 20.5 14.6 17.2 17.1
9 OM 11.4 11.2 12.2 11.1 13.4 19.2
10 DC 14.3 28.7 20.1 17.0 22.3 16.2
11 OC 28.6 44.0 24.9 27.9 34.0 30.7
Then I rank colleges using this commend
HEIrank1<-(STOF[,-c(1)])
rank1 <- apply(HEIrank1,2,rank)
> HEIrank11
HEI.ID X2007 X2008 X2009 X2010 X2011 X2012
1 OP 18.0 20 20.0 20.0 20.0 20
2 MO 14.0 9 13.0 13.5 2.0 12
3 SD 15.0 16 17.0 16.0 16.0 19
4 UN 20.0 18 8.0 13.5 14.0 13
5 WS 12.0 8 4.0 7.0 6.0 8
6 BF 6.5 17 9.5 15.0 10.0 14
7 ME 17.0 15 19.0 19.0 17.0 15
8 IM 2.0 6 12.0 8.0 8.5 10
9 OM 4.5 3 2.5 3.0 3.0 11
10 DC 11.0 14 11.0 9.0 15.0 9
11 OC 16.0 19 16.0 18.0 19.0 17
I would like to draw histogram for each HEIs (for each row)?
If you use ggplot you won't need to do it as a loop, you can plot them all at once. Also, you need to reformat your data so that it's in long format not short format. You can use the melt function from the reshape package to do so.
library(reshape2)
new.df<-melt(HEIrank11,id.vars="HEI.ID")
names(new.df)=c("HEI.ID","Year","Rank")
substring is just getting rid of the X in each year
library(ggplot2)
ggplot(new.df, aes(x=HEI.ID,y=Rank,fill=substring(Year,2)))+
geom_histogram(stat="identity",position="dodge")
Here's a solution in lattice:
require(lattice)
barchart(X2007+X2008+X2009+X2010+X2011+X2012 ~ HEI.ID,
data=HEIrank11,
auto.key=list(space='right')
)

gnuplot input file 7 columns with decimals

I am trying to graph the following data file:
61.0 16.4 100.0 28.6 28.6 12.2 12.2
59.0 25.4 100.0 21.4 21.4 11.8 11.8
69.0 15.9 100.0 35.7 35.7 11.5 11.5
59.0 23.7 100.0 23.4 23.4 11.8 11.8
49.0 20.4 100.0 18.0 18.0 9.8 9.8
84.0 13.1 90.9 50.8 50.8 16.8 16.8
59.0 16.9 100.0 22.6 22.6 11.8 11.8
71.0 16.9 100.0 32.8 32.8 14.2 14.2
68.0 19.1 100.0 26.2 26.2 13.6 13.6
91.0 13.2 100.0 51.6 51.6 18.2 18.2
57.0 22.8 100.0 29.4 29.4 11.4 11.4
52.0 26.9 100.0 17.8 17.8 10.4 10.4
55.0 21.8 100.0 32.2 32.2 11.0 11.0
68.0 19.1 100.0 29.8 29.8 13.6 13.6
50.0 22.0 100.0 19.0 19.0 10.0 10.0
149.0 12.1 66.7 111.2 111.2 29.8 29.8
69.0 20.3 100.0 29.8 29.8 13.8 13.8
I am very new to gnuplot I cant seem to figure out what the correct code will be to get this graph:
I was trying something like this:
gnuplot> set output 'datastore1.png'
gnuplot> plot 'desktop1.dat' using 0:1 title "totalio" with lines, 'desktop1.dat' using 0:2 title "readpercentage" with lines, 'desktop1.dat' using 0:3 title "cachehitpercentage" with lines, 'desktop1.dat' using 0:4 title "currentkbpersecond" with lines, 'desktop1.dat' using 0:5 title "maximumkbpersecond" with lines, 'desktop1.dat' using 0:6 title "currentiopersecond" with lines, 'desktop1.dat' using 0:7 title "maximumiopersecond" with lines
gnuplot> quit
However the graph is not exactly correct.
Thanks for the help!
Not sure what you are trying to plot here, but I think the error is that you are using the zero-th column for the 'using' command which does not exist. Rather use this
p 'desktop1.dat' u 1:2, 'desktop1.dat' u 1:3
edit
So when you are plotting against time, you might want to add another column to the data that you read in from the file such that you have
15 61.0 16.4 100.0 28.6 28.6 12.2 12.2
as an example for the first line of your data. Afterwards you use the given plotting command I gove above.

Resources