convert ascii to netcdf format using r language - r

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")

Related

how add spatial interpolation in R

I have question to you guys because I don't know how to interpolate my data on map.
I'm try to reproduce map from http://aasa.ut.ee/Rspatial/05_session.html and I want to achive something like that:
map from tutorial. But there is happening something in background so I don't understand everything.
What I already have:
data frame with longitude, latitude and preassure columns.
The range of longitude and latitude is xlim=c(-10, 30), ylim=c(40,65)
> df
longitude latitude preassure
1 65.0 -10.0 999.6984
2 65.0 -7.5 999.7445
3 65.0 -5.0 999.5182
4 65.0 -2.5 999.0021
5 65.0 0.0 998.4595
6 65.0 2.5 998.0452
7 65.0 5.0 997.8119
8 65.0 7.5 997.4956
9 65.0 10.0 997.1532
10 65.0 12.5 997.1851
11 65.0 15.0 997.3216
12 65.0 17.5 997.0767
13 65.0 20.0 996.5215
14 65.0 22.5 996.0055
15 65.0 25.0 995.7271
16 65.0 27.5 995.6919
17 65.0 30.0 995.8885
18 65.0 32.5 996.1016
19 65.0 35.0 996.3514
20 62.5 -10.0 1001.9770
21 62.5 -7.5 1002.4431
22 62.5 -5.0 1002.6335
23 62.5 -2.5 1002.6905
24 62.5 0.0 1002.7607
25 62.5 2.5 1002.7090
26 62.5 5.0 1002.2872
27 62.5 7.5 1001.8760
28 62.5 10.0 1001.8066
29 62.5 12.5 1001.3829
30 62.5 15.0 1000.1226
31 62.5 17.5 998.7299
32 62.5 20.0 997.8351
33 62.5 22.5 997.3430
34 62.5 25.0 997.2307
35 62.5 27.5 997.3709
36 62.5 30.0 997.5797
37 62.5 32.5 997.7341
38 62.5 35.0 997.9541
39 60.0 -10.0 1006.7589
40 60.0 -7.5 1007.3146
41 60.0 -5.0 1007.5654
42 60.0 -2.5 1007.7268
43 60.0 0.0 1007.8035
44 60.0 2.5 1007.5192
45 60.0 5.0 1006.9113
46 60.0 7.5 1006.3931
47 60.0 10.0 1005.7369
48 60.0 12.5 1004.3599
49 60.0 15.0 1002.6407
50 60.0 17.5 1001.2952
51 60.0 20.0 1000.2639
52 60.0 22.5 999.5920
53 60.0 25.0 999.3969
54 60.0 27.5 999.3746
55 60.0 30.0 999.4167
56 60.0 32.5 999.6822
57 60.0 35.0 1000.2075
58 57.5 -10.0 1011.8321
59 57.5 -7.5 1012.5412
60 57.5 -5.0 1013.0607
61 57.5 -2.5 1013.2542
62 57.5 0.0 1013.0942
63 57.5 2.5 1012.6132
64 57.5 5.0 1011.7856
65 57.5 7.5 1010.6275
66 57.5 10.0 1009.2998
67 57.5 12.5 1007.9899
68 57.5 15.0 1006.7288
69 57.5 17.5 1005.4613
70 57.5 20.0 1004.2938
71 57.5 22.5 1003.5243
72 57.5 25.0 1003.2300
73 57.5 27.5 1003.0484
74 57.5 30.0 1002.9350
75 57.5 32.5 1003.1247
76 57.5 35.0 1003.6449
77 55.0 -10.0 1017.0270
78 55.0 -7.5 1018.0844
79 55.0 -5.0 1018.8001
80 55.0 -2.5 1018.8523
81 55.0 0.0 1018.3936
82 55.0 2.5 1017.8593
83 55.0 5.0 1017.3493
84 55.0 7.5 1016.5559
85 55.0 10.0 1015.3215
86 55.0 12.5 1013.9338
87 55.0 15.0 1012.6094
88 55.0 17.5 1011.2712
89 55.0 20.0 1009.9580
90 55.0 22.5 1008.9790
91 55.0 25.0 1008.4529
92 55.0 27.5 1008.0782
93 55.0 30.0 1007.7059
94 55.0 32.5 1007.5469
95 55.0 35.0 1007.5681
96 52.5 -10.0 1021.5118
97 52.5 -7.5 1022.4463
98 52.5 -5.0 1022.9901
99 52.5 -2.5 1023.1570
100 52.5 0.0 1023.0451
101 52.5 2.5 1022.8766
102 52.5 5.0 1022.7153
103 52.5 7.5 1022.2165
104 52.5 10.0 1021.0707
105 52.5 12.5 1019.6818
106 52.5 15.0 1018.5007
107 52.5 17.5 1017.3067
108 52.5 20.0 1015.8635
109 52.5 22.5 1014.5103
110 52.5 25.0 1013.4495
111 52.5 27.5 1012.4800
112 52.5 30.0 1011.7438
113 52.5 32.5 1011.5841
114 52.5 35.0 1011.6581
115 50.0 -10.0 1024.6059
116 50.0 -7.5 1025.4720
117 50.0 -5.0 1026.0310
118 50.0 -2.5 1026.4533
119 50.0 0.0 1026.6738
120 50.0 2.5 1026.6087
121 50.0 5.0 1026.4604
122 50.0 7.5 1026.2937
123 50.0 10.0 1025.5823
124 50.0 12.5 1024.3837
125 50.0 15.0 1023.3542
126 50.0 17.5 1022.3630
127 50.0 20.0 1021.0361
128 50.0 22.5 1019.7546
129 50.0 25.0 1018.5380
130 50.0 27.5 1016.9760
131 50.0 30.0 1015.5891
132 50.0 32.5 1015.2455
133 50.0 35.0 1015.3957
134 47.5 -10.0 1026.5579
135 47.5 -7.5 1027.6550
136 47.5 -5.0 1028.1215
137 47.5 -2.5 1028.2034
138 47.5 0.0 1028.3665
139 47.5 2.5 1028.4254
140 47.5 5.0 1028.6371
141 47.5 7.5 1029.1085
142 47.5 10.0 1028.8970
143 47.5 12.5 1027.6374
144 47.5 15.0 1026.1353
145 47.5 17.5 1024.8384
146 47.5 20.0 1023.6115
147 47.5 22.5 1022.7498
148 47.5 25.0 1022.1981
149 47.5 27.5 1020.5934
150 47.5 30.0 1018.6138
151 47.5 32.5 1017.7399
152 47.5 35.0 1017.6530
153 45.0 -10.0 1027.3521
154 45.0 -7.5 1028.1177
155 45.0 -5.0 1028.4873
156 45.0 -2.5 1028.4996
157 45.0 0.0 1028.7896
158 45.0 2.5 1028.8341
159 45.0 5.0 1027.7460
160 45.0 7.5 1026.4421
161 45.0 10.0 1025.6955
162 45.0 12.5 1025.1825
163 45.0 15.0 1025.0429
164 45.0 17.5 1025.2959
165 45.0 20.0 1025.0541
166 45.0 22.5 1024.3106
167 45.0 25.0 1022.9915
168 45.0 27.5 1021.3729
169 45.0 30.0 1020.4743
170 45.0 32.5 1019.7272
171 45.0 35.0 1019.3080
172 42.5 -10.0 1027.2090
173 42.5 -7.5 1027.8139
174 42.5 -5.0 1028.9445
175 42.5 -2.5 1029.2019
176 42.5 0.0 1028.4393
177 42.5 2.5 1026.8987
178 42.5 5.0 1025.0419
179 42.5 7.5 1023.9250
180 42.5 10.0 1024.0046
181 42.5 12.5 1024.0845
182 42.5 15.0 1023.5381
183 42.5 17.5 1023.5317
184 42.5 20.0 1024.7725
185 42.5 22.5 1025.0638
186 42.5 25.0 1022.9272
187 42.5 27.5 1022.0318
188 42.5 30.0 1021.7181
189 42.5 32.5 1020.8714
190 42.5 35.0 1020.5228
191 40.0 -10.0 1026.8144
192 40.0 -7.5 1027.3171
193 40.0 -5.0 1029.2556
194 40.0 -2.5 1028.9006
195 40.0 0.0 1026.9173
196 40.0 2.5 1026.1678
197 40.0 5.0 1025.5287
198 40.0 7.5 1024.6530
199 40.0 10.0 1024.0673
200 40.0 12.5 1023.5342
201 40.0 15.0 1022.7612
202 40.0 17.5 1022.1834
203 40.0 20.0 1022.2757
204 40.0 22.5 1022.6448
205 40.0 25.0 1021.9519
206 40.0 27.5 1021.6179
207 40.0 30.0 1022.3322
208 40.0 32.5 1023.9349
209 40.0 35.0 1023.3993
here is my code:
library("rnaturalearth")
library(sf)
library(ggplot2)
library(viridis)
world <- ne_countries(scale = "medium", returnclass = "sf")
sf::sf_use_s2(FALSE)
ggplot(data = world) +
geom_raster(data = df, aes(x=latitude, y=longitude, fill=preassure), interpolate = TRUE) +
scale_fill_viridis(direction = 1) +
geom_sf(fill = "NA", colour = "white") +
coord_sf(xlim=c(-10, 30),ylim=c(40,65), expand = F) +
xlab("Długość geograficzna") + ylab("Szerokość geograficzna") +
labs(title = "SLP zima",fill = "Cisnienie") +
theme(plot.title = element_text(hjust = 0.5)
)
and result: what I was able to do
and finaly I want something like this:
map with the preasure interpolation - demo from paint
Thanks for all your help!!!

Deep Nested Tag IDs not found using BeautifulSoup

I am trying to scrape NBA data from https://www.basketball-reference.com/leagues/NBA_2019.html, but I am running into issues where BeautifulSoup drops deeply nested tags.
I tried to use soup.find(id='opponent-stats-per_game') to grab the "Opponent Per Game Stats" table. However, I am getting None result. If I try to instead find a div that is higher up in the tree, then it clips the more deep children.
Could someone please offer me some guidance on how this works? I am fairly new to web scraping using BeautifulSoup
The reference.com sites are partially dynamic. I had the same issue a long while back when trying to figure out football-reference.com
There's a couple ways to handle it. One is to use Selenium to render the page first, and then you can go in and grab the tables. Now you can still use BeautifulSoup to get it, but whenever I see <table> tags, my first initial try is to use pandas and .read_html(), as that'll do most of the work for you on the tables.
This will return a list of dataframes. It's then just a matter of finding which dataframe you want, and then possibly do a little manipulation of column names and what-not to get it the way you need.
Doing this, your opponent stats per game table was in index position 19:
from bs4 import BeautifulSoup
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
page_url = 'https://www.basketball-reference.com/leagues/NBA_2019.html'
driver.get(page_url)
tables = pd.read_html(driver.page_source)
opp_per_gm_df = tables[19]
driver.quit()
Output:
print (opp_per_gm_df)
Rk Team G MP FG ... STL BLK TOV PF PTS
0 1.0 Memphis Grizzlies 77 242.3 37.2 ... 7.7 4.9 15.5 21.7 105.6
1 2.0 Miami Heat 77 240.3 38.2 ... 7.4 4.8 14.2 20.3 105.6
2 3.0 Indiana Pacers* 78 240.3 38.7 ... 7.5 5.2 15.6 20.1 104.3
3 4.0 Utah Jazz* 77 240.6 39.7 ... 8.6 4.7 13.9 22.2 106.1
4 5.0 Denver Nuggets* 77 240.6 39.6 ... 7.5 5.0 13.5 20.5 106.9
5 6.0 Detroit Pistons 77 242.3 40.0 ... 6.9 5.2 14.1 21.5 107.5
6 7.0 Orlando Magic 78 241.3 39.9 ... 6.9 4.4 13.0 18.8 106.5
7 8.0 Boston Celtics* 78 241.3 39.5 ... 6.8 3.8 15.2 19.6 108.0
8 9.0 Toronto Raptors* 78 242.2 40.2 ... 7.7 4.5 15.1 20.6 108.4
9 10.0 Dallas Mavericks 77 241.0 40.9 ... 7.9 4.6 13.1 23.4 109.9
10 11.0 Milwaukee Bucks* 78 241.3 40.3 ... 6.9 4.9 13.4 20.0 108.6
11 12.0 Portland Trail Blazers* 77 242.3 41.1 ... 7.3 5.1 12.4 20.8 110.5
12 13.0 Houston Rockets* 78 241.9 40.4 ... 7.4 4.6 15.0 20.1 109.3
13 14.0 Golden State Warriors* 77 241.6 40.3 ... 7.6 3.7 13.5 19.8 111.4
14 15.0 San Antonio Spurs* 78 241.6 41.6 ... 7.2 4.1 12.2 19.7 110.4
15 16.0 Philadelphia 76ers* 77 241.6 41.5 ... 7.9 4.0 12.9 22.3 112.2
16 17.0 Charlotte Hornets 77 241.9 42.0 ... 7.1 6.1 13.6 20.6 112.2
17 18.0 Oklahoma City Thunder* 78 242.2 40.8 ... 8.2 5.1 16.9 22.6 110.8
18 19.0 Brooklyn Nets 78 243.8 42.2 ... 7.8 5.4 13.5 22.3 112.5
19 20.0 Minnesota Timberwolves 77 241.9 42.0 ... 6.6 5.6 14.7 22.0 114.0
20 21.0 New York Knicks 77 241.3 42.0 ... 7.4 5.7 13.4 21.0 114.1
21 22.0 Chicago Bulls 78 242.9 42.1 ... 7.5 5.6 13.5 18.9 113.4
22 23.0 Los Angeles Clippers* 78 241.6 41.4 ... 8.2 5.9 13.1 24.0 113.4
23 24.0 Los Angeles Lakers 78 241.3 42.1 ... 8.3 5.1 14.3 21.0 113.7
24 25.0 Cleveland Cavaliers 78 241.0 43.0 ... 6.9 5.6 12.5 19.6 113.9
25 26.0 Sacramento Kings 78 240.6 41.9 ... 7.7 5.1 15.9 21.6 114.9
26 27.0 Phoenix Suns 78 242.2 42.2 ... 9.1 5.0 15.6 20.7 116.3
27 28.0 New Orleans Pelicans 78 240.6 43.2 ... 8.4 5.4 13.8 21.3 116.5
28 29.0 Washington Wizards 78 243.2 43.3 ... 7.8 4.6 15.9 21.4 116.9
29 30.0 Atlanta Hawks 78 242.2 42.6 ... 9.9 5.4 15.1 22.0 118.8
30 NaN League Average 78 241.7 41.0 ... 7.7 5.0 14.2 21.0 111.1
[31 rows x 25 columns]

Rolling mean with changing window size on a large dataset

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

SVG use one color for each path

I have an svg and in in there are 2 paths
<path d="M 84.4 53.2 C 75.3 50.1 67.1 48.5 59.6 48.3 C 51.2 48 45.2 47.5 41.5 46.5 C 35.7 45.1 29.9 42.4 23.9 38.4 C 21.6 36.9 19.4 35.2 17.3 33.2 C 19.4 34.2 21.6 35 23.9 35.6 C 27.3 36.5 34.1 37 44.1 37.2 C 53.7 37.3 59.7 37.7 62 38.3 C 70.4 40.3 77 44.2 81.8 49.9 C 82.9 51 83.7 52 84.4 53.2 Z" class="st0" style="fill: rgb(15, 141, 225);"/>
<path d="M 68.1 7.3 C 79.5 10.1 86.8 16 89.9 24.9 C 91.4 29.2 91.4 36.1 89.8 45.7 C 89.9 45.8 90 46 90.1 46.2 L 90 46 C 90.4 46.7 90.8 47.4 91.2 48.2 C 93.2 52 94.4 56.2 95 60.7 C 95.1 61.3 95 61.8 94.6 62.3 C 94.2 62.7 93.8 63 93.2 63.1 C 92.7 63.2 92.1 63 91.6 62.7 C 91.2 62.3 90.9 61.9 90.8 61.3 C 90.3 57.3 89.2 53.6 87.5 50.2 C 86.8 49.1 86 48.1 85.1 47.1 C 79.7 40.8 72.4 36.5 63.1 34.2 L 63.2 34.3 C 60.8 33.607 54.5 33.599 44.3 33.1 C 34.709 32.63 28.3 32.4 25.1 31.6 L 25 31.6 C 18.9 30.1 14.1 26.9 10.6 22 C 10.2 21.4 9.8 20.7 9.5 20.1 C 9.5 20.1 9.5 20 9.4 20 C 9.3 19.8 9.2 19.6 9 19.3 C 6.6 15.3 5.3 10.8 5 6.1 C 7.4 8.5 10.3 10.1 13.5 10.9 C 17.7 11.9 22.3 12.1 27.3 11.5 C 30.2 11.2 34.7 10.4 40.6 9 C 46.4 7.5 50.9 6.6 54 6.4 C 59 5.8 63.7 6.1 68.1 7.3 Z" class="st0" style="fill: rgb(11, 59, 91);"/>
Is is possible to select and fill the paths individually with css?
This works
.svg-fill {
fill: red
}
but it fills the whole svg. How can I target the individual paths?
Use nth-child selector:
svg path:nth-child(1) { /*OR svg path:first-child OR svg path:nth-of-type(1)*/
fill:red;
}
svg path:nth-child(2) { /*OR svg path:last-child OR svg path:nth-of-type(2)*/
fill:green;
}
<svg>
<path d="M 84.4 53.2 C 75.3 50.1 67.1 48.5 59.6 48.3 C 51.2 48 45.2 47.5 41.5 46.5 C 35.7 45.1 29.9 42.4 23.9 38.4 C 21.6 36.9 19.4 35.2 17.3 33.2 C 19.4 34.2 21.6 35 23.9 35.6 C 27.3 36.5 34.1 37 44.1 37.2 C 53.7 37.3 59.7 37.7 62 38.3 C 70.4 40.3 77 44.2 81.8 49.9 C 82.9 51 83.7 52 84.4 53.2 Z"/>
<path d="M 68.1 7.3 C 79.5 10.1 86.8 16 89.9 24.9 C 91.4 29.2 91.4 36.1 89.8 45.7 C 89.9 45.8 90 46 90.1 46.2 L 90 46 C 90.4 46.7 90.8 47.4 91.2 48.2 C 93.2 52 94.4 56.2 95 60.7 C 95.1 61.3 95 61.8 94.6 62.3 C 94.2 62.7 93.8 63 93.2 63.1 C 92.7 63.2 92.1 63 91.6 62.7 C 91.2 62.3 90.9 61.9 90.8 61.3 C 90.3 57.3 89.2 53.6 87.5 50.2 C 86.8 49.1 86 48.1 85.1 47.1 C 79.7 40.8 72.4 36.5 63.1 34.2 L 63.2 34.3 C 60.8 33.607 54.5 33.599 44.3 33.1 C 34.709 32.63 28.3 32.4 25.1 31.6 L 25 31.6 C 18.9 30.1 14.1 26.9 10.6 22 C 10.2 21.4 9.8 20.7 9.5 20.1 C 9.5 20.1 9.5 20 9.4 20 C 9.3 19.8 9.2 19.6 9 19.3 C 6.6 15.3 5.3 10.8 5 6.1 C 7.4 8.5 10.3 10.1 13.5 10.9 C 17.7 11.9 22.3 12.1 27.3 11.5 C 30.2 11.2 34.7 10.4 40.6 9 C 46.4 7.5 50.9 6.6 54 6.4 C 59 5.8 63.7 6.1 68.1 7.3 Z" />
</svg>
Each of your paths has the same class (at the end of each path you can see it is defined with class="st0" in the code). If you change the class to make each one unique you can target them individually.
For example, change the second one to class="st1" and then the following CSS will make the 1st path red and the 2nd blue:
.st0 { fill: red; }
.st1 { fill: blue; }
If you want to change them using CSS you should also remove the style="fill: rgb(xx, xx, xx);" from each path.

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.

Resources