How to retrieve a slot coords of each spatialPpolygon object - r

I have a spatialPolygons object in R; but I can not to retrieve the "coords" for each spatialPolygon object
Here's my R session:
[[1]]
An object of class "Polygon"
Slot "labpt":
[1] 5.7828 20.5764
Slot "area":
[1] 0.16
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
x y
[1,] 5.6828 20.7764
[2,] 5.7828 20.7764
[3,] 5.8828 20.7764
[4,] 5.9828 20.7764
[5,] 5.9828 20.3764
[6,] 5.8828 20.3764
[7,] 5.7828 20.3764
[8,] 5.6828 20.3764
[9,] 5.5828 20.3764
[10,] 5.5828 20.7764
[11,] 5.6828 20.7764
[[2]]
An object of class "Polygon"
Slot "labpt":
[1] 3.715865 35.160271
Slot "area":
[1] 39.68
Slot "hole":
[1] FALSE
Slot "ringDir":
[1] 1
Slot "coords":
x y
[1,] -0.7172 35.1764
[2,] -0.8172 35.1764
[3,] -0.9172 35.1764
[4,] -1.0172 35.1764
[5,] -1.1172 35.1764
[6,] -1.1172 35.5764
[7,] -1.2172 35.5764
[8,] -1.2172 35.9764
[9,] -1.2172 36.3764
[10,] -1.3172 36.3764
[11,] -1.3172 36.7764
[12,] -1.4172 36.7764
[13,] -1.5172 36.7764
[14,] -1.6172 36.7764
[15,] -1.6172 37.1764
[16,] -1.5172 37.1764
[17,] -1.4172 37.1764
[18,] -1.3172 37.1764
[19,] -1.2172 37.1764
[20,] -1.1172 37.1764
[21,] -1.0172 37.1764
[22,] -0.9172 37.1764
[23,] -0.8172 37.1764
[24,] -0.7172 37.1764
[25,] -0.6172 37.1764
[26,] -0.5172 37.1764
[27,] -0.4172 37.1764
[28,] -0.3172 37.1764
[29,] -0.2172 37.1764
[30,] -0.1172 37.1764
[31,] -0.0172 37.1764
[32,] 0.0828 37.1764
[33,] 0.1828 37.1764
[34,] 0.2828 37.1764
[35,] 0.3828 37.1764
[36,] 0.4828 37.1764
[37,] 0.5828 37.1764
[38,] 0.6828 37.1764
[39,] 0.7828 37.1764
[40,] 0.8828 37.1764
[41,] 0.9828 37.1764
[42,] 1.0828 37.1764
[43,] 1.1828 37.1764
[44,] 1.2828 37.1764
[45,] 1.3828 37.1764
[46,] 1.4828 37.1764
[47,] 1.5828 37.1764
[48,] 1.6828 37.1764
[49,] 1.7828 37.1764
[50,] 1.8828 37.1764
[51,] 1.9828 37.1764
[52,] 2.0828 37.1764
[53,] 2.1828 37.1764
[54,] 2.2828 37.1764
[55,] 2.3828 37.1764
[56,] 2.4828 37.1764
[57,] 2.5828 37.1764
[58,] 2.6828 37.1764
[59,] 2.7828 37.1764
[60,] 2.8828 37.1764
[61,] 2.9828 37.1764
[62,] 3.0828 37.1764
[63,] 3.1828 37.1764
[64,] 3.2828 37.1764
[65,] 3.3828 37.1764
[66,] 3.4828 37.1764
[67,] 3.5828 37.1764
[68,] 3.6828 37.1764
[69,] 3.7828 37.1764
[70,] 3.8828 37.1764
[71,] 3.9828 37.1764
[72,] 4.0828 37.1764
[73,] 4.1828 37.1764
[74,] 4.2828 37.1764
[75,] 4.3828 37.1764
[76,] 4.4828 37.1764
[77,] 4.5828 37.1764
[78,] 4.6828 37.1764
[79,] 4.7828 37.1764
[80,] 4.8828 37.1764
[81,] 4.9828 37.1764
[82,] 5.0828 37.1764
[83,] 5.1828 37.1764
[84,] 5.2828 37.1764
[85,] 5.3828 37.1764
[86,] 5.4828 37.1764
[87,] 5.5828 37.1764
[88,] 5.6828 37.1764
[89,] 5.7828 37.1764
[90,] 5.8828 37.1764
[91,] 5.9828 37.1764
[92,] 6.0828 37.1764
[93,] 6.1828 37.1764
[94,] 6.2828 37.1764
[95,] 6.3828 37.1764
[96,] 6.4828 37.1764
[97,] 6.5828 37.1764
[98,] 6.6828 37.1764
[99,] 6.7828 37.1764
[100,] 6.8828 37.1764
[101,] 6.9828 37.1764
[102,] 7.0828 37.1764
[103,] 7.1828 37.1764
[104,] 7.2828 37.1764
[105,] 7.3828 37.1764
[106,] 7.4828 37.1764
[107,] 7.5828 37.1764
[108,] 7.6828 37.1764
[109,] 7.7828 37.1764
[110,] 7.8828 37.1764
[111,] 7.9828 37.1764
[112,] 8.0828 37.1764
[113,] 8.1828 37.1764
[114,] 8.2828 37.1764
[115,] 8.3828 37.1764
[116,] 8.4828 37.1764
[117,] 8.5828 37.1764
[118,] 8.6828 37.1764
[119,] 8.7828 37.1764
[120,] 8.8828 37.1764
[121,] 8.9828 37.1764
[122,] 9.0828 37.1764
[123,] 9.1828 37.1764
[124,] 9.2828 37.1764
[125,] 9.3828 37.1764
[126,] 9.4828 37.1764
[127,] 9.5828 37.1764
[128,] 9.6828 37.1764
[129,] 9.7828 37.1764
[130,] 9.8828 37.1764
[131,] 9.9828 37.1764
[132,] 10.0828 37.1764
[133,] 10.1828 37.1764
[134,] 10.2828 37.1764
[135,] 10.3828 37.1764
[136,] 10.4828 37.1764
[137,] 10.5828 37.1764
[138,] 10.6828 37.1764
[139,] 10.7828 37.1764
[140,] 10.8828 37.1764
[141,] 10.9828 37.1764
[142,] 11.0828 37.1764
[143,] 11.1828 37.1764
[144,] 11.2828 37.1764
[145,] 11.3828 37.1764
[146,] 11.4828 37.1764
[147,] 11.5828 37.1764
[148,] 11.6828 37.1764
[149,] 11.7828 37.1764
[150,] 11.8828 37.1764
[151,] 11.9828 37.1764
[152,] 11.9828 36.7764
[153,] 11.9828 36.3764
[154,] 11.9828 35.9764
[155,] 11.9828 35.5764
[156,] 11.9828 35.1764
[157,] 11.8828 35.1764
[158,] 11.7828 35.1764
[159,] 11.6828 35.1764
[160,] 11.5828 35.1764
[161,] 11.4828 35.1764
[162,] 11.3828 35.1764
[163,] 11.2828 35.1764
[164,] 11.1828 35.1764
[165,] 11.0828 35.1764
[166,] 10.9828 35.1764
[167,] 10.8828 35.1764
[168,] 10.7828 35.1764
[169,] 10.6828 35.1764
[170,] 10.5828 35.1764
[171,] 10.4828 35.1764
[172,] 10.3828 35.1764
[173,] 10.2828 35.1764
[174,] 10.1828 35.1764
[175,] 10.0828 35.1764
[176,] 9.9828 35.1764
[177,] 9.8828 35.1764
[178,] 9.7828 35.1764
[179,] 9.6828 35.1764
[180,] 9.5828 35.1764
[181,] 9.4828 35.1764
[182,] 9.3828 35.1764
[183,] 9.2828 35.1764
[184,] 9.2828 35.5764
[185,] 9.1828 35.5764
[186,] 9.0828 35.5764
[187,] 8.9828 35.5764
[188,] 8.8828 35.5764
[189,] 8.7828 35.5764
[190,] 8.6828 35.5764
[191,] 8.6828 35.9764
[192,] 8.5828 35.9764
[193,] 8.5828 36.3764
[194,] 8.4828 36.3764
[195,] 8.3828 36.3764
[196,] 8.2828 36.3764
[197,] 8.1828 36.3764
[198,] 8.0828 36.3764
[199,] 8.0828 36.7764
[200,] 7.9828 36.7764
[201,] 7.8828 36.7764
[202,] 7.8828 36.3764
[203,] 7.8828 35.9764
[204,] 7.7828 35.9764
[205,] 7.7828 35.5764
[206,] 7.6828 35.5764
[207,] 7.5828 35.5764
[208,] 7.4828 35.5764
[209,] 7.4828 35.1764
[210,] 7.3828 35.1764
[211,] 7.2828 35.1764
[212,] 7.1828 35.1764
[213,] 7.0828 35.1764
[214,] 6.9828 35.1764
[215,] 6.8828 35.1764
[216,] 6.7828 35.1764
[217,] 6.7828 35.5764
[218,] 6.8828 35.5764
[219,] 6.8828 35.9764
[220,] 6.7828 35.9764
[221,] 6.7828 36.3764
[222,] 6.6828 36.3764
[223,] 6.5828 36.3764
[224,] 6.4828 36.3764
[225,] 6.4828 36.7764
[226,] 6.3828 36.7764
[227,] 6.2828 36.7764
[228,] 6.1828 36.7764
[229,] 6.0828 36.7764
[230,] 5.9828 36.7764
[231,] 5.9828 36.3764
[232,] 5.9828 35.9764
[233,] 5.8828 35.9764
[234,] 5.7828 35.9764
[235,] 5.7828 35.5764
[236,] 5.7828 35.1764
[237,] 5.6828 35.1764
[238,] 5.5828 35.1764
[239,] 5.4828 35.1764
[240,] 5.3828 35.1764
[241,] 5.2828 35.1764
[242,] 5.1828 35.1764
[243,] 5.0828 35.1764
[244,] 4.9828 35.1764
[245,] 4.9828 34.7764
[246,] 4.8828 34.7764
[247,] 4.7828 34.7764
[248,] 4.6828 34.7764
[249,] 4.6828 34.3764
[250,] 4.5828 34.3764
[251,] 4.4828 34.3764
[252,] 4.4828 33.9764
[253,] 4.3828 33.9764
[254,] 4.2828 33.9764
[255,] 4.2828 33.5764
[256,] 4.1828 33.5764
[257,] 4.0828 33.5764
[258,] 3.9828 33.5764
[259,] 3.9828 33.1764
[260,] 3.8828 33.1764
[261,] 3.7828 33.1764
[262,] 3.6828 33.1764
[263,] 3.5828 33.1764
[264,] 3.4828 33.1764
[265,] 3.3828 33.1764
[266,] 3.3828 32.7764
[267,] 3.2828 32.7764
[268,] 3.1828 32.7764
[269,] 3.0828 32.7764
[270,] 3.0828 32.3764
[271,] 2.9828 32.3764
[272,] 2.8828 32.3764
[273,] 2.7828 32.3764
[274,] 2.7828 31.9764
[275,] 2.6828 31.9764
[276,] 2.5828 31.9764
[277,] 2.4828 31.9764
[278,] 2.3828 31.9764
[279,] 2.3828 31.5764
[280,] 2.2828 31.5764
[281,] 2.1828 31.5764
[282,] 2.0828 31.5764
[283,] 1.9828 31.5764
[284,] 1.8828 31.5764
[285,] 1.7828 31.5764
[286,] 1.6828 31.5764
[287,] 1.5828 31.5764
[288,] 1.4828 31.5764
[289,] 1.3828 31.5764
[290,] 1.2828 31.5764
[291,] 1.1828 31.5764
[292,] 1.0828 31.5764
[293,] 0.9828 31.5764
[294,] 0.8828 31.5764
[295,] 0.7828 31.5764
[296,] 0.6828 31.5764
[297,] 0.5828 31.5764
[298,] 0.4828 31.5764
[299,] 0.3828 31.5764
[300,] 0.2828 31.5764
[301,] 0.1828 31.5764
[302,] 0.0828 31.5764
[303,] -0.0172 31.5764
[304,] -0.0172 31.9764
[305,] -0.1172 31.9764
[306,] -0.2172 31.9764
[307,] -0.3172 31.9764
[308,] -0.4172 31.9764
[309,] -0.5172 31.9764
[310,] -0.6172 31.9764
[311,] -0.7172 31.9764
[312,] -0.7172 32.3764
[313,] -0.8172 32.3764
[314,] -0.9172 32.3764
[315,] -1.0172 32.3764
[316,] -1.0172 32.7764
[317,] -1.1172 32.7764
[318,] -1.1172 33.1764
[319,] -1.0172 33.1764
[320,] -1.0172 33.5764
[321,] -0.9172 33.5764
[322,] -0.8172 33.5764
[323,] -0.8172 33.9764
[324,] -0.7172 33.9764
[325,] -0.6172 33.9764
[326,] -0.5172 33.9764
[327,] -0.4172 33.9764
[328,] -0.3172 33.9764
[329,] -0.3172 34.3764
[330,] -0.2172 34.3764
[331,] -0.2172 34.7764
[332,] -0.3172 34.7764
[333,] -0.4172 34.7764
[334,] -0.5172 34.7764
[335,] -0.5172 35.1764
[336,] -0.6172 35.1764
[337,] -0.7172 35.1764
[[3]]
An object of class "Polygon"
Slot "labpt":
[1] 1.199467 36.200642
Slot "area":
[1] 1.32
Slot "hole":
[1] TRUE
Slot "ringDir":
[1] -1
Slot "coords":
x y
[1,] 0.7828 35.5764
[2,] 0.8828 35.5764
[3,] 0.9828 35.5764
[4,] 1.0828 35.5764
[5,] 1.1828 35.5764
[6,] 1.2828 35.5764
[7,] 1.3828 35.5764
[8,] 1.4828 35.5764
[9,] 1.5828 35.5764
[10,] 1.6828 35.5764
[11,] 1.6828 35.9764
[12,] 1.7828 35.9764
[13,] 1.8828 35.9764
[14,] 1.8828 36.3764
[15,] 1.7828 36.3764
[16,] 1.6828 36.3764
[17,] 1.6828 36.7764
[18,] 1.5828 36.7764
[19,] 1.4828 36.7764
[20,] 1.3828 36.7764
[21,] 1.2828 36.7764
[22,] 1.1828 36.7764
[23,] 1.0828 36.7764
[24,] 0.9828 36.7764
[25,] 0.8828 36.7764
[26,] 0.7828 36.7764
[27,] 0.6828 36.7764
[28,] 0.5828 36.7764
[29,] 0.5828 36.3764
[30,] 0.5828 35.9764
[31,] 0.6828 35.9764
[32,] 0.7828 35.9764
[33,] 0.7828 35.5764
[[4]]
An object of class "Polygon"
Slot "labpt":
[1] 2.8828 33.6164
Slot "area":
[1] 0.4
Slot "hole":
[1] TRUE
Slot "ringDir":
[1] -1
Slot "coords":
x y
[1,] 2.5828 33.9764
[2,] 2.5828 33.5764
[3,] 2.6828 33.5764
[4,] 2.6828 33.1764
[5,] 2.7828 33.1764
[6,] 2.8828 33.1764
[7,] 2.9828 33.1764
[8,] 3.0828 33.1764
[9,] 3.0828 33.5764
[10,] 3.1828 33.5764
[11,] 3.1828 33.9764
[12,] 3.0828 33.9764
[13,] 2.9828 33.9764
[14,] 2.8828 33.9764
[15,] 2.7828 33.9764
[16,] 2.6828 33.9764
[17,] 2.5828 33.9764
so I want to extract the
Slot "coords" from [[1]] in a dataframe ,Slot "coords" from[[2]] in another dataframe so on...
can someone help me please?
edit :
for example, as extracting coordinates???
[1,] 5.6828 20.7764
[2,] 5.7828 20.7764
[3,] 5.8828 20.7764
[4,] 5.9828 20.7764
[5,] 5.9828 20.3764
[6,] 5.8828 20.3764
[7,] 5.7828 20.3764
[8,] 5.6828 20.3764
[9,] 5.5828 20.3764
[10,] 5.5828 20.7764
[11,] 5.6828 20.7764

A SpatialPolygons object is a list of polygons, each of which is a list of Polygons (yes, the only difference is in the capitalization...). The latter has the coordinates.
Here is an example of provinces in France, downloaded from GADM:
library(raster)
FR <- getData('GADM',country='FRA',level=1)
length(FR#polygons)
# [1] 22
sapply(FR#polygons,function(p)length(p#Polygons))
# [1] 1 1 1 2 1 28 2 2 1 1 2 16 245 10 11 3 1 2 1 8 28 23
So FR is a spatialPolygons object representing French provinces. There are 22 polygons representing the provinces, and each of these has one or more Polygons containing the physical coordinates (e.g., lat and long) of the boundary. There can be multiple Polygons because the geographies can be dis-contiguous (e.g. islands, etc.). In fact the land-locked provinces generally have just one Polygon, whereas the coastal provinces have many.
So do you want 22 data frames, one for each province, or you so want a dataframe for each geography?
If you know that your spatiaPolygons object has only one Polygon for each polygon, then you could do it this way:
result <- lapply(FR#polygons,function(p) data.frame(p#Polygons[[1]]#coords))
In this case I'm just grabbing the coordinates of the first Polygon in each province.

Wouldn't this just be?
name_of_list_of_objects[[1]]#coords
Usually there is an extraction function for an S4 object that is considered safer and more correct, so this might work:
coordinates( name_of_list_object[[1]] )
So for multiple items, just:
lapply(name_of_list_of_objects, coordinates)

Related

How to convert the data below to dataframe so that I can plot them out? (data retrieved from API commands)

I have recently retrieved the data from one of our remote monitors through API using the command res <- GET("myurl", authenticate("xxx", "xxx")). Then I got,
> res
Response [https://myurl?dataFormat=json&orient=split&days=1&last=false&valuesOnly=false]
Date: 2021-09-09 17:34
Status: 200
Content-Type: text/html; charset=utf-8
Size: 29.3 kB`
Then I use data = fromJSON(rawToChar(res$content)) to parse the table to characters. The result I got:
> data
$columns
[1] "DATETIME" "TIMESTAMP" "RECORD" "RunType"
[5] "Vapor" "Concentration" "PeakHeight" "PeakLocation"
[9] "CalFactor" "ColTemp" "CarrDuty" "Baseline"
[13] "Ver" "RunCount" "Status" "Latitude"
[17] "Longitude" "WS_ms" "WS_avg" "WindDir"
[21] "Battery_Voltage" "EnclosureTemp" "EngineTemp" "AlarmLevel"
$data
[,1] [,2] [,3] [,4] [,5]
[1,] "2021-09-08T17:26:17Z" "2021-09-08 11:26:17-0600" "3776" "MEAS" "BENZENE"
[2,] "2021-09-08T17:36:17Z" "2021-09-08 11:36:17-0600" "3777" "MEAS" "BENZENE"
[3,] "2021-09-08T17:46:17Z" "2021-09-08 11:46:17-0600" "3778" "MEAS" "BENZENE"
[4,] "2021-09-08T17:56:17Z" "2021-09-08 11:56:17-0600" "3779" "MEAS" "BENZENE"
[5,] "2021-09-08T18:06:25Z" "2021-09-08 12:06:25-0600" "3780" "CAL" "BENZENE"
[6,] "2021-09-08T18:16:17Z" "2021-09-08 12:16:17-0600" "3781" "MEAS" "BENZENE"
[7,] "2021-09-08T18:26:17Z" "2021-09-08 12:26:17-0600" "3782" "MEAS" "BENZENE"
[8,] "2021-09-08T18:36:17Z" "2021-09-08 12:36:17-0600" "3783" "MEAS" "BENZENE"
[9,] "2021-09-08T18:46:17Z" "2021-09-08 12:46:17-0600" "3784" "MEAS" "BENZENE"
[10,] "2021-09-08T18:56:17Z" "2021-09-08 12:56:17-0600" "3785" "MEAS" "BENZENE"
[11,] "2021-09-08T19:06:17Z" "2021-09-08 13:06:17-0600" "3786" "MEAS" "BENZENE"
[12,] "2021-09-08T19:16:17Z" "2021-09-08 13:16:17-0600" "3787" "MEAS" "BENZENE"
[13,] "2021-09-08T19:26:17Z" "2021-09-08 13:26:17-0600" "3788" "MEAS" "BENZENE"
[14,] "2021-09-08T19:36:17Z" "2021-09-08 13:36:17-0600" "3789" "MEAS" "BENZENE"
[15,] "2021-09-08T19:46:17Z" "2021-09-08 13:46:17-0600" "3790" "MEAS" "BENZENE"
[16,] "2021-09-08T19:56:16Z" "2021-09-08 13:56:16-0600" "3791" "MEAS" "BENZENE"
[17,] "2021-09-08T20:06:16Z" "2021-09-08 14:06:16-0600" "3792" "MEAS" "BENZENE"
[18,] "2021-09-08T20:16:16Z" "2021-09-08 14:16:16-0600" "3793" "MEAS" "BENZENE"
[19,] "2021-09-08T20:26:16Z" "2021-09-08 14:26:16-0600" "3794" "MEAS" "BENZENE"
[20,] "2021-09-08T20:36:16Z" "2021-09-08 14:36:16-0600" "3795" "MEAS" "BENZENE"
[21,] "2021-09-08T20:46:16Z" "2021-09-08 14:46:16-0600" "3796" "MEAS" "BENZENE"
[22,] "2021-09-08T20:56:15Z" "2021-09-08 14:56:15-0600" "3797" "MEAS" "BENZENE"
[23,] "2021-09-08T21:06:16Z" "2021-09-08 15:06:16-0600" "3798" "MEAS" "BENZENE"
[24,] "2021-09-08T21:16:16Z" "2021-09-08 15:16:16-0600" "3799" "MEAS" "BENZENE"
[25,] "2021-09-08T21:26:16Z" "2021-09-08 15:26:16-0600" "3800" "MEAS" "BENZENE"
[26,] "2021-09-08T21:36:16Z" "2021-09-08 15:36:16-0600" "3801" "MEAS" "BENZENE"
[27,] "2021-09-08T21:46:16Z" "2021-09-08 15:46:16-0600" "3802" "MEAS" "BENZENE"
[28,] "2021-09-08T21:56:16Z" "2021-09-08 15:56:16-0600" "3803" "MEAS" "BENZENE"
[29,] "2021-09-08T22:06:16Z" "2021-09-08 16:06:16-0600" "3804" "MEAS" "BENZENE"
[30,] "2021-09-08T22:16:16Z" "2021-09-08 16:16:16-0600" "3805" "MEAS" "BENZENE"
[31,] "2021-09-08T22:26:16Z" "2021-09-08 16:26:16-0600" "3806" "MEAS" "BENZENE"
[32,] "2021-09-08T22:36:16Z" "2021-09-08 16:36:16-0600" "3807" "MEAS" "BENZENE"
[33,] "2021-09-08T22:46:16Z" "2021-09-08 16:46:16-0600" "3808" "MEAS" "BENZENE"
[34,] "2021-09-08T22:56:17Z" "2021-09-08 16:56:17-0600" "3809" "MEAS" "BENZENE"
[35,] "2021-09-08T23:06:17Z" "2021-09-08 17:06:17-0600" "3810" "MEAS" "BENZENE"
[36,] "2021-09-08T23:16:17Z" "2021-09-08 17:16:17-0600" "3811" "MEAS" "BENZENE"
[37,] "2021-09-08T23:26:17Z" "2021-09-08 17:26:17-0600" "3812" "MEAS" "BENZENE"
[38,] "2021-09-08T23:36:17Z" "2021-09-08 17:36:17-0600" "3813" "MEAS" "BENZENE"
[39,] "2021-09-08T23:46:17Z" "2021-09-08 17:46:17-0600" "3814" "MEAS" "BENZENE"
[40,] "2021-09-08T23:56:17Z" "2021-09-08 17:56:17-0600" "3815" "MEAS" "BENZENE"
[41,] "2021-09-09T00:06:25Z" "2021-09-08 18:06:25-0600" "3816" "CAL" "BENZENE"
[,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] "0.25" "1767" "185" "1.179" "52.4" "0.08" "130688" "3.04" "834"
[2,] "0.25" "1739" "183" "1.179" "52.2" "0.08" "130976" "3.04" "835"
[3,] "0.26" "2190" "185" "1.179" "51.9" "0.08" "131464" "3.04" "836"
[4,] "0.25" "1837" "185" "1.179" "52.2" "0.08" "131896" "3.04" "837"
[5,] "9.08" "186269" "183" "1.187" "52.4" "0.08" "132304" "3.04" "838"
[6,] "0.31" "3393" "184" "1.187" "51.7" "0.08" "133184" "3.04" "839"
[7,] "0.28" "2653" "185" "1.187" "52.1" "0.08" "133476" "3.04" "840"
[8,] "0.26" "2120" "185" "1.187" "52.4" "0.08" "133900" "3.04" "841"
[9,] "0.27" "2260" "185" "1.187" "51.8" "0.08" "134192" "3.04" "842"
[10,] "0.26" "2178" "185" "1.187" "52" "0.08" "134608" "3.04" "843"
[11,] "0.25" "1868" "185" "1.187" "52.3" "0.08" "135092" "3.04" "844"
[12,] "0.24" "1654" "186" "1.187" "52.4" "0.08" "135496" "3.04" "845"
[13,] "0.27" "2326" "184" "1.187" "51.8" "0.08" "135932" "3.04" "846"
[14,] "0.26" "1963" "185" "1.187" "52.1" "0.08" "136396" "3.04" "847"
[15,] "0.25" "1829" "185" "1.187" "52.5" "0.08" "136764" "3.04" "848"
[16,] "0.26" "2110" "185" "1.187" "51.8" "0.08" "137104" "3.04" "849"
[17,] "0.3" "3114" "184" "1.187" "52" "0.08" "137488" "3.04" "850"
[18,] "0.24" "1557" "186" "1.187" "52.3" "0.08" "137952" "3.04" "851"
[19,] "0.23" "1237" "186" "1.187" "52.4" "0.08" "138312" "3.04" "852"
[20,] "0.27" "2390" "184" "1.187" "51.9" "0.08" "138736" "3.04" "853"
[,15] [,16] [,17] [,18] [,19] [,20]
[1,] "OK " "32.8518183333" "-104.39718" "7.664" "5.334" "23.63"
[2,] "OK " "32.8518233333" "-104.3971816667" "6.664" "4.847" "353.5"
[3,] "OK " "32.851825" "-104.39718" "2.999" "4.805" "352.8"
[4,] "OK " "32.8518133333" "-104.3971783333" "4.165" "4.786" "0.408"
[5,] "OK CAL PASS" "32.8518116667" "-104.397185" "4.831" "4.522" "12.97"
[6,] "OK " "32.8518083333" "-104.397185" "3.165" "4.269" "27.72"
[7,] "OK " "32.8518066667" "-104.3971766667" "3.165" "4.182" "0.085"
[8,] "OK " "32.8518083333" "-104.397175" "2.832" "4.059" "9.05"
[9,] "OK " "32.8518183333" "-104.3971783333" "3.665" "3.597" "53.43"
[10,] "OK " "32.851825" "-104.3971783333" "4.498" "4.291" "40.63"
[11,] "OK " "32.851825" "-104.397175" "2.832" "3.158" "32.96"
[12,] "OK " "32.8518266667" "-104.397175" "4.332" "3.4" "35.28"
[13,] "OK " "32.8518383333" "-104.3971716667" "2.332" "4.121" "343.1"
[14,] "OK " "32.851845" "-104.3971716667" "3.332" "2.911" "25.6"
[15,] "OK " "32.8518416667" "-104.3971683333" "3.499" "1.802" "8.47"
[16,] "OK " "32.85184" "-104.3971683333" "1.333" "3.036" "10.12"
[17,] "OK " "32.8518333333" "-104.3971733333" "2.499" "2.767" "7.565"
[18,] "OK " "32.85183" "-104.397175" "1.499" "2.822" "35.21"
[19,] "OK " "32.851825" "-104.3971716667" "1.999" "2.591" "28"
[20,] "OK " "32.8518216667" "-104.3971716667" "0.666" "1.631" "352.4"
[,21] [,22] [,23] [,24]
[1,] "12.24" "31.5" "33.33" "NONE"
[2,] "12.23" "31.91" "33.25" "NONE"
[3,] "12.25" "32.34" "33.24" "NONE"
[4,] "12.15" "32.87" "33.38" "NONE"
[5,] "12.12" "33.09" "33.42" "NONE"
[6,] "12.22" "33.43" "33.34" "NONE"
[7,] "12.04" "33.92" "33.41" "NONE"
[8,] "12.01" "34.46" "33.48" "NONE"
[9,] "12.02" "34.67" "33.48" "NONE"
[10,] "12.16" "34.88" "33.52" "NONE"
[11,] "12.05" "35.04" "33.46" "NONE"
[12,] "12.21" "35.41" "33.53" "NONE"
[13,] "12.23" "35.6" "33.48" "NONE"
[14,] "12.28" "35.86" "33.45" "NONE"
[15,] "11.89" "36.27" "33.57" "NONE"
[16,] "11.8" "36.47" "33.61" "NONE"
[17,] "11.9" "36.63" "33.66" "NONE"
[18,] "12.05" "36.61" "33.6" "NONE"
[19,] "11.89" "37" "33.61" "NONE"
[20,] "12.09" "37.26" "33.63" "NONE"
[ reached getOption("max.print") -- omitted 104 rows ]
I want to plot the data out and do some analysis but never got this data into a dataframe. Can someone help me out? Thank you in advance!
setNames(as.data.frame(XYZ$data), XYZ$columns)
# DATETIME TIMESTAMP RECORD RunType Vapor
# 1 2021-09-08T17:26:17Z 2021-09-08 11:26:17-0600 3776 MEAS BENZENE
# 2 2021-09-08T17:36:17Z 2021-09-08 11:36:17-0600 3777 MEAS BENZENE
# 3 2021-09-08T17:46:17Z 2021-09-08 11:46:17-0600 3778 MEAS BENZENE
Abridged sample data:
XYZ <- list(columns = c("DATETIME", "TIMESTAMP", "RECORD", "RunType", "Vapor"), data = structure(c("2021-09-08T17:26:17Z", "2021-09-08T17:36:17Z", "2021-09-08T17:46:17Z", "2021-09-08 11:26:17-0600", "2021-09-08 11:36:17-0600", "2021-09-08 11:46:17-0600", "3776", "3777", "3778", "MEAS", "MEAS", "MEAS", "BENZENE", "BENZENE", "BENZENE"), .Dim = c(3L, 5L)))

List to data frame while keeping structure

str(coord_mat)
List of 1
$ :List of 1
..$ : num [1:17, 1:2] -122 -122 -122 -122 -122 ...
I have list of coordinate pairs in coord_mat that I would like to transform in to data frame (or matrix) of coordinate pairs in the same structure(first column being lon, the second being lat).
> coord_mat
[[1]]
[[1]][[1]]
[,1] [,2]
[1,] -122.3435 47.63787
[2,] -122.3435 47.63787
[3,] -122.3434 47.63787
[4,] -122.3434 47.63787
[5,] -122.3434 47.63787
[6,] -122.3434 47.63787
[7,] -122.3434 47.63787
[8,] -122.3434 47.63784
[9,] -122.3433 47.63777
[10,] -122.3430 47.63772
[11,] -122.3427 47.63778
[12,] -122.3425 47.63776
[13,] -122.3423 47.63749
[14,] -122.3421 47.63718
[15,] -122.3420 47.63700
[16,] -122.3419 47.63698
[17,] -122.3419 47.63698
How is this possible in R while keeping the same double column structure as the list?
I have tried matrix(unlist(coord_mat)), but this just produces a long vector of length 34 with the lon values first then the lat values. Is it because I am working with a list of lists?
> matrix(unlist(coord_mat))
[,1]
[1,] -122.34345
[2,] -122.34345
[3,] -122.34340
[4,] -122.34340
[5,] -122.34340
[6,] -122.34340
[7,] -122.34340
[8,] -122.34338
[9,] -122.34334
[10,] -122.34299
[11,] -122.34273
[12,] -122.34249
[13,] -122.34230
[14,] -122.34208
[15,] -122.34198
[16,] -122.34194
[17,] -122.34194
[18,] 47.63787
[19,] 47.63787
[20,] 47.63787
[21,] 47.63787
[22,] 47.63787
[23,] 47.63787
[24,] 47.63787
[25,] 47.63784
[26,] 47.63777
[27,] 47.63772
[28,] 47.63778
[29,] 47.63776
[30,] 47.63749
[31,] 47.63718
[32,] 47.63700
[33,] 47.63698
[34,] 47.63698
Here is the data:
dput(coord_mat)
list(list(structure(c(-122.34345, -122.34345, -122.343398333333,
-122.343398333333, -122.343398333333, -122.343398333333, -122.343398333333,
-122.343376666667, -122.34334, -122.342991666667, -122.342731666667,
-122.342491666667, -122.3423, -122.342081666667, -122.341983333333,
-122.341943333333, -122.341943333333, 47.6378716666667, 47.6378716666667,
47.6378683333333, 47.6378683333333, 47.6378683333333, 47.6378683333333,
47.6378683333333, 47.637835, 47.637775, 47.6377183333333, 47.63778,
47.63776, 47.6374916666667, 47.6371816666667, 47.6369966666667,
47.6369783333333, 47.6369783333333), .Dim = c(17L, 2L))))
res <- coord_mat[[c(1, 1)]]
# or
res <- matrix(unlist(coord_mat), ncol = 2)
colnames(res) <- c("lon", "lat")
res
lon lat
[1,] -122.3435 47.63787
[2,] -122.3435 47.63787
[3,] -122.3434 47.63787
[4,] -122.3434 47.63787
[5,] -122.3434 47.63787
[6,] -122.3434 47.63787
[7,] -122.3434 47.63787
[8,] -122.3434 47.63784
[9,] -122.3433 47.63777
[10,] -122.3430 47.63772
[11,] -122.3427 47.63778
[12,] -122.3425 47.63776
[13,] -122.3423 47.63749
[14,] -122.3421 47.63718
[15,] -122.3420 47.63700
[16,] -122.3419 47.63698
[17,] -122.3419 47.63698

r why does raster extract give dim error?

I'm trying to extract values and get the mean from a raster brick, but get an error I think is related to the dimensions of the raster brick.
the data have been downloaded from NOAA
what I've done is the following:
library(raster)
ERSST <- rotate(brick('sst.mnmean.nc'))
ERSST
class : RasterBrick
dimensions : 89, 180, 16020, 1976 (nrow, ncol, ncell, nlayers)
resolution : 2, 2 (x, y)
extent : -179, 181, -89, 89 (xmin, xmax, ymin, ymax) # ignore extent, needs fixing but not relevant for the question
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : X1854.01.01, X1854.02.01, X1854.03.01, X1854.04.01, X1854.05.01, X1854.06.01, X1854.07.01, X1854.08.01, X1854.09.01, X1854.10.01, X1854.11.01, X1854.12.01, X1855.01.01, X1855.02.01, X1855.03.01, ...
min values : -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, -1.8, ...
max values : 32.09937, 31.44189, 31.72137, 31.47466, 33.23633, 31.90788, 35.22922, 33.66898, 32.26702, 32.15502, 31.68270, 31.74512, 31.32458, 31.23049, 29.88974, ...
Date : 1854-01-01, 2018-08-01 (min, max)
Arbitrary point
xy <- data.frame(x = -49, y = 45)
and when I extract I get:
extract(ERSST, xy, buffer = 1e+05, small = TRUE, fun = mean)
Error in apply(x, 2, fun2) : dim(X) must have a positive length
what makes me think it's a dimension problem is the error I get when I try to specify the layers to use
extract(ERSST, xy, buffer = 1e+05, small = TRUE, layer = 10, nl = 10)
Error in x[, lyrs] : incorrect number of dimensions
and it seems to work fine if I average first (but this is not what I want, I need the time series at the point)
mERSST <- mean(ERSST)
extract(mERSST, xy, buffer = 1e+05, small = TRUE, fun = mean)
[1] 5.649212
Perhaps it's the Date attribute in the raster brick. Any workarounds or solutions to prevent this error?
The answer by #RobertHijmans made me realise that I always get just a single value from extract, even when the point is at the junction of several grid cells, as in the example above.
plot(MSST, xlim = c(-60, -40), ylim = c(40, 50))
points(xy)
Using:
extract(mERSST, xy, buffer = 1e+05, small = TRUE, cellnumbers = TRUE)
[[1]]
cell value
3845.000000 5.649212
I get only a single value, whereas I would expect there to be 4, no matter how small the buffer. Am I missing something in extract? So I tried with converting my point to a circle and use that to extract data
coordinates(xy) <- ~ x + y
proj4string(xy) <- '+init=epsg:4326'
xy_utm <- spTransform(xy, CRS('+init=epsg:32621'))
gbf_utm <- rgeos::gBuffer(xy_utm, width = 1e5, quadsegs = 250L)
gbf <- spTransform(gbf_utm, CRS(proj4string(xy)))
plot(ERSST[[1]], xlim = c(-60, -40), ylim = c(40, 50))
points(xy, pch = 19)
plot(gbf, add = TRUE)
extract(ERSST[[1]], gbf, small = TRUE, weights = TRUE)
this gives me:
[[1]]
value weight
[1,] 1.722664 0.25
[2,] 3.683457 0.25
[3,] 5.985203 0.25
[4,] 8.442450 0.25
in version 2.6.7 (and this seems to make sense).
but
[[1]]
value weight
[1,] 1.722664 0.001236928
[2,] 1.722664 0.003935680
[3,] 1.722664 0.005285056
[4,] 3.683457 0.005285056
[5,] 3.683457 0.003935680
[6,] 3.683457 0.001236928
[7,] 1.722664 0.002136512
[8,] 1.722664 0.008321151
[9,] 1.722664 0.011244799
[10,] 1.722664 0.011244799
[11,] 1.722664 0.011244799
[12,] 3.683457 0.011244799
[13,] 3.683457 0.011244799
[14,] 3.683457 0.011244799
[15,] 3.683457 0.008208703
[16,] 3.683457 0.001911616
[17,] 1.722664 0.003036096
[18,] 1.722664 0.010907455
[19,] 1.722664 0.011244799
[20,] 1.722664 0.011244799
[21,] 1.722664 0.011244799
[22,] 1.722664 0.011244799
[23,] 3.683457 0.011244799
[24,] 3.683457 0.011244799
[25,] 3.683457 0.011244799
[26,] 3.683457 0.011244799
[27,] 3.683457 0.010907455
[28,] 3.683457 0.003036096
[29,] 1.722664 0.000449792
[30,] 1.722664 0.010232767
[31,] 1.722664 0.011244799
[32,] 1.722664 0.011244799
[33,] 1.722664 0.011244799
[34,] 1.722664 0.011244799
[35,] 1.722664 0.011244799
[36,] 3.683457 0.011244799
[37,] 3.683457 0.011244799
[38,] 3.683457 0.011244799
[39,] 3.683457 0.011244799
[40,] 3.683457 0.011244799
[41,] 3.683457 0.010232767
[42,] 3.683457 0.000337344
[43,] 1.722664 0.003036096
[44,] 1.722664 0.011244799
[45,] 1.722664 0.011244799
[46,] 1.722664 0.011244799
[47,] 1.722664 0.011244799
[48,] 1.722664 0.011244799
[49,] 1.722664 0.011244799
[50,] 3.683457 0.011244799
[51,] 3.683457 0.011244799
[52,] 3.683457 0.011244799
[53,] 3.683457 0.011244799
[54,] 3.683457 0.011244799
[55,] 3.683457 0.011244799
[56,] 3.683457 0.002923648
[57,] 5.985203 0.002923648
[58,] 5.985203 0.011244799
[59,] 5.985203 0.011244799
[60,] 5.985203 0.011244799
[61,] 5.985203 0.011244799
[62,] 5.985203 0.011244799
[63,] 5.985203 0.011244799
[64,] 8.442450 0.011244799
[65,] 8.442450 0.011244799
[66,] 8.442450 0.011244799
[67,] 8.442450 0.011244799
[68,] 8.442450 0.011244799
[69,] 8.442450 0.011244799
[70,] 8.442450 0.002923648
[71,] 5.985203 0.000337344
[72,] 5.985203 0.010120319
[73,] 5.985203 0.011244799
[74,] 5.985203 0.011244799
[75,] 5.985203 0.011244799
[76,] 5.985203 0.011244799
[77,] 5.985203 0.011244799
[78,] 8.442450 0.011244799
[79,] 8.442450 0.011244799
[80,] 8.442450 0.011244799
[81,] 8.442450 0.011244799
[82,] 8.442450 0.011244799
[83,] 8.442450 0.010007871
[84,] 8.442450 0.000224896
[85,] 5.985203 0.002811200
[86,] 5.985203 0.010795007
[87,] 5.985203 0.011244799
[88,] 5.985203 0.011244799
[89,] 5.985203 0.011244799
[90,] 5.985203 0.011244799
[91,] 8.442450 0.011244799
[92,] 8.442450 0.011244799
[93,] 8.442450 0.011244799
[94,] 8.442450 0.011244799
[95,] 8.442450 0.010682559
[96,] 8.442450 0.002698752
[97,] 5.985203 0.001799168
[98,] 5.985203 0.007871359
[99,] 5.985203 0.011244799
[100,] 5.985203 0.011244799
[101,] 5.985203 0.011244799
[102,] 8.442450 0.011244799
[103,] 8.442450 0.011244799
[104,] 8.442450 0.011244799
[105,] 8.442450 0.007871359
[106,] 8.442450 0.001799168
[107,] 5.985203 0.001236928
[108,] 5.985203 0.003935680
[109,] 5.985203 0.005285056
[110,] 8.442450 0.005285056
[111,] 8.442450 0.003935680
[112,] 8.442450 0.001236928
in version 2.7-13, which can't be right.
To illustrate the discussion above in a simplified manner (and after fixing the extract with polygons issue), I get
library(raster)
r <- raster(xmn=-59, xmx=-39, ymn=41, ymx=49, res=2, vals=1:40)
xy <- SpatialPoints(data.frame(x = -49, y = 45), proj4string = CRS('+init=epsg:4326'))
p <- buffer(xy, width = 1e5, quadsegs = 250L)
plot(r)
plot(p, add=T)
extract(r, xy)
#26
extract(r, p)
#[[1]]
#[1] 16 26 25 15
extract(r, p, weights=T)
#[[1]]
# value weight
#[1,] 15 0.25
#[2,] 16 0.25
#[3,] 25 0.25
#[4,] 26 0.25
extract(r, xy, buffer=100000)
#[[1]]
#value
# 15
extract(r, xy, buffer=1000000)
#[[1]]
# [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#[37] 37 38 39 40
It looks like the layer and nl arguments are not working. I am not sure why.
One workaround is to extract values first and then subset the values.
library(raster)
value <- extract(ERSST, point, buffer = 1e+05, small = TRUE)
value[[1]][10:19]
# X1854.10.01 X1854.11.01 X1854.12.01 X1855.01.01 X1855.02.01 X1855.03.01 X1855.04.01 X1855.05.01
# 7.932416 5.712043 4.428292 3.010927 2.289096 2.385752 2.528488 3.261783
# X1855.06.01 X1855.07.01
# 5.762860 7.617740

Runif generating NaN terms

I have a matrix of baseline values for different parameters, which is a 1 column x 305 row matrix, and I want to use runif to generate a matrix of random numbers (100 for each parameter value) between +- 10% of the initial parameter value. The problem is that there are NaN terms randomly interspersed in the output when there shouldn't be, and I get this warning message:
Warning message:
In runif(n = 30500, min = lower, max = upper) : NAs produced
This is my code so far:
baseline <- scan("baseline.txt")
baseline <- as.matrix(baseline)
base <- t(baseline)
upper <- 1.1 * base
lower <- 0.9 * base
matrice2 <- matrix(runif(n=30500, min = lower, max = upper), nrow=305)
(I had to take t(baseline) so that the resulting matrice2 is in the right form to be passed to another function that I'm using later). The numbers in baseline range from -0.281 to 800.
Anyone have any ideas as to why it is generating NaN? I was using this exact code before I took some time off for the holidays, and it was working then.
EDIT: this is the baseline.txt data, a 305 row x 1 column text file
baseline
[,1]
[1,] 0.400000000
[2,] 0.800000000
[3,] 0.200000000
[4,] 0.800000000
[5,] 0.600000000
[6,] 0.800000000
[7,] 0.150000000
[8,] 0.900000000
[9,] 0.500000000
[10,] 0.600000000
[11,] 0.500000000
[12,] 0.500000000
[13,] 0.600000000
[14,] 0.500000000
[15,] 0.500000000
[16,] 0.600000000
[17,] 0.200000000
[18,] 0.700000000
[19,] 0.800000000
[20,] 0.300000000
[21,] 0.700000000
[22,] 0.800000000
[23,] 0.300000000
[24,] 0.700000000
[25,] 0.800000000
[26,] 0.200000000
[27,] 0.002000000
[28,] 0.400000000
[29,] 0.001000000
[30,] 0.001300000
[31,] 0.800000000
[32,] 0.526000000
[33,] 0.526000000
[34,] 0.714000000
[35,] 0.306000000
[36,] 0.306000000
[37,] 0.711000000
[38,] 0.711000000
[39,] 0.847000000
[40,] 0.300000000
[41,] 0.300000000
[42,] 0.300000000
[43,] 0.300000000
[44,] 0.300000000
[45,] 0.300000000
[46,] 0.050000000
[47,] 0.400000000
[48,] 0.450000000
[49,] 0.300000000
[50,] 0.400000000
[51,] 0.700000000
[52,] 0.400000000
[53,] 0.600000000
[54,] 0.306000000
[55,] 0.885000000
[56,] 0.008610000
[57,] 0.008610000
[58,] 0.008610000
[59,] 0.008610000
[60,] 0.016500000
[61,] 6.450000000
[62,] 6.450000000
[63,] 6.450000000
[64,] 6.450000000
[65,] 0.014650000
[66,] 0.014650000
[67,] 0.014650000
[68,] 0.014650000
[69,] 0.200000000
[70,] 0.400000000
[71,] 0.400000000
[72,] 3.000000000
[73,] 3.000000000
[74,] 3.000000000
[75,] 3.000000000
[76,] 3.000000000
[77,] 3.000000000
[78,] 3.000000000
[79,] 3.000000000
[80,] 3.000000000
[81,] 0.338000000
[82,] 0.013200000
[83,] 0.001350000
[84,] 0.001350000
[85,] 0.001350000
[86,] 0.338000000
[87,] 34.694000000
[88,] 0.002710000
[89,] 0.000420000
[90,] 0.015900000
[91,] 0.037000000
[92,] 0.024500000
[93,] 0.075000000
[94,] 0.045000000
[95,] 0.024800000
[96,] 0.015900000
[97,] 0.045000000
[98,] 0.045000000
[99,] 2.800000000
[100,] 0.001590000
[101,] 0.004500000
[102,] 0.001000000
[103,] 0.020000000
[104,] 0.004800000
[105,] 7.500000000
[106,] 34.694000000
[107,] 0.338000000
[108,] 0.028900000
[109,] 0.022400000
[110,] 0.022400000
[111,] 0.040000000
[112,] 0.008000000
[113,] 0.338000000
[114,] 0.026400000
[115,] 0.046080000
[116,] 0.057600000
[117,] 0.700000000
[118,] 0.400000000
[119,] 0.200000000
[120,] 0.700000000
[121,] 0.300000000
[122,] 0.750000000
[123,] 4.000000000
[124,] 0.500000000
[125,] 8.000000000
[126,] 800.000000000
[127,] 3.000000000
[128,] 3.000000000
[129,] 6.480000000
[130,] 1.150000000
[131,] 0.700000000
[132,] 0.100000000
[133,] 1.793000000
[134,] 1.291000000
[135,] 1.793000000
[136,] 1.291000000
[137,] 1.793000000
[138,] 1.291000000
[139,] 5.944000000
[140,] 4.877000000
[141,] 0.000210000
[142,] 0.006700000
[143,] 2.000000000
[144,] 0.080000000
[145,] 0.110000000
[146,] 0.008500000
[147,] 120.000000000
[148,] 60.000000000
[149,] 3.200000000
[150,] 0.800000000
[151,] 1.300000000
[152,] 1.300000000
[153,] 1.300000000
[154,] 2.690000000
[155,] 0.024500000
[156,] 7.850000000
[157,] 1.500000000
[158,] 10.000000000
[159,] 10.000000000
[160,] 4.300000000
[161,] 0.050000000
[162,] 0.400000000
[163,] 7.860000000
[164,] 3.000000000
[165,] 0.052800000
[166,] 0.052800000
[167,] 15.439000000
[168,] 7.860000000
[169,] 15.439000000
[170,] 0.200000000
[171,] 0.053000000
[172,] 0.022000000
[173,] 0.164600000
[174,] 0.164600000
[175,] 0.164600000
[176,] 0.164600000
[177,] 4.100000000
[178,] 15.439000000
[179,] 7.860000000
[180,] 0.160000000
[181,] 0.040000000
[182,] 0.014400000
[183,] 7.860000000
[184,] 6.000000000
[185,] 0.300000000
[186,] 0.300000000
[187,] 33.500000000
[188,] 0.020000000
[189,] 1.277000000
[190,] 0.981700000
[191,] 1.000000000
[192,] 1.000000000
[193,] 1.000000000
[194,] 0.016666667
[195,] 0.271000000
[196,] 0.504000000
[197,] 0.504000000
[198,] 0.504000000
[199,] 0.713000000
[200,] 0.742000000
[201,] 0.713000000
[202,] 0.981700000
[203,] 0.058800000
[204,] 1.257000000
[205,] 1.257000000
[206,] 2.723000000
[207,] 3.969000000
[208,] 2.462000000
[209,] 2.492000000
[210,] 3.000000000
[211,] 0.815000000
[212,] 1.191000000
[213,] 1.191000000
[214,] 1.191000000
[215,] 2.241000000
[216,] 3.969000000
[217,] 2.241000000
[218,] 3.969000000
[219,] 152.000000000
[220,] 0.085000000
[221,] 0.493900000
[222,] 0.231000000
[223,] 0.246000000
[224,] 0.500000000
[225,] 0.011363636
[226,] 0.133000000
[227,] 0.133000000
[228,] 0.133000000
[229,] 0.198000000
[230,] 0.493900000
[231,] 0.198000000
[232,] 0.493900000
[233,] 1.447000000
[234,] 1.970000000
[235,] 0.995000000
[236,] 0.995000000
[237,] 1.000000000
[238,] 0.271000000
[239,] 0.623000000
[240,] 0.623000000
[241,] 0.623000000
[242,] 1.128000000
[243,] 1.970000000
[244,] 1.128000000
[245,] 1.970000000
[246,] 2.256000000
[247,] 2.951000000
[248,] 3.000000000
[249,] 3.000000000
[250,] 3.000000000
[251,] -0.281000000
[252,] 1.065000000
[253,] 1.065000000
[254,] 1.065000000
[255,] 0.949000000
[256,] 2.951000000
[257,] 0.949000000
[258,] 2.951000000
[259,] 0.000840000
[260,] 0.000120000
[261,] 0.006200000
[262,] 0.006200000
[263,] 0.006200000
[264,] 0.085000000
[265,] 0.030500000
[266,] 0.459000000
[267,] 0.030500000
[268,] 0.459000000
[269,] 0.030500000
[270,] 0.011100000
[271,] 0.001481000
[272,] 0.000210000
[273,] 0.000210000
[274,] 0.000210000
[275,] 0.000210000
[276,] 142.050000000
[277,] 0.468000000
[278,] 0.355000000
[279,] 0.335000000
[280,] 0.013513514
[281,] 0.729000000
[282,] 0.154000000
[283,] 0.154000000
[284,] 0.154000000
[285,] 0.396000000
[286,] 0.396000000
[287,] 0.009100000
[288,] 0.006700000
[289,] 0.006700000
[290,] 0.006700000
[291,] 0.006700000
[292,] 112.500000000
[293,] 112.500000000
[294,] 112.500000000
[295,] 0.009803922
[296,] 0.237000000
[297,] 0.237000000
[298,] 0.237000000
[299,] 0.017900000
[300,] 0.017900000
[301,] 0.000120000
[302,] 0.000120000
[303,] 0.000120000
[304,] 0.006200000
[305,] 0.006200000
As pointed out in comments, in runif the min must be less than max. Your method of calculating min and max move the max farther from 0 and the mins closer to 0 which only works when the numbers are positive.
As suggested in comments, there are several ways you could do this, for example pmin and pmax:
upper = pmax(0.9 * base, 1.1 * base)
lower = pmin(0.9 * base, 1.1 * base)
another way, probably more efficient, would be:
offset = 0.1 * abs(base)
upper = base + offset
lower = base - offset

r language getting a not so good neural network

i am trying to fit a neural net using nnet method. But for some reason i am getting fitted values =1 . any suggestion?
traininginput <- as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)
#Column bind the data into one variable
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")
nnet1=nnet(trainingdata$Input,trainingdata$Output,size=10,decay=.2,MaxNWts=100)
nnet1=nnet(trainingdata$Input,trainingdata$Output,size=10,decay=.2,MaxNWts=100)
# weights: 31
initial value 2398.911170
iter 10 value 1881.721970
iter 20 value 1879.273609
iter 30 value 1879.248746
final value 1879.248003
converged
nnet1$fitted.values
[,1]
[1,] 0.9995635611
[2,] 0.9995572993
[3,] 0.9994755520
[4,] 0.9995623911
[5,] 0.9946006508
[6,] 0.9995635779
[7,] 0.9995108061
[8,] 0.9995635025
[9,] 0.9995634973
[10,] 0.9993213029
[11,] 0.9994652808
[12,] 0.9979116411
[13,] 0.9993242430
[14,] 0.9995635764
[15,] 0.9995632595
[16,] 0.9995583546
[17,] 0.9992778848
[18,] 0.9995635364
[19,] 0.9939526885
[20,] 0.9995635788
[21,] 0.9993010783
[22,] 0.9995597586
[23,] 0.9995635748
[24,] 0.9995635799
[25,] 0.9995634813
[26,] 0.9992898229
[27,] 0.9959834977
[28,] 0.9991941868
[29,] 0.9995632224
[30,] 0.9995486025
[31,] 0.9995608608
[32,] 0.9995635755
[33,] 0.9995635740
[34,] 0.9995491922
[35,] 0.9995635661
[36,] 0.9995629924
[37,] 0.9995634948
[38,] 0.9994889240
[39,] 0.9995633558
[40,] 0.9995559725
[41,] 0.9995563696
[42,] 0.9995564636
[43,] 0.9995602699
[44,] 0.9995635768
[45,] 0.9995612253
[46,] 0.9895069469
[47,] 0.9995271104
[48,] 0.9995635564
[49,] 0.9995635797
[50,] 0.9995609961
Add the linout = TRUE argument to the nnet function:
nnet1=nnet(trainingdata$Input,trainingdata$Output,size=10,decay=.2,MaxNWts=100, linout = TRUE)
That should solve your problem! By default, the fitted values are logistic output units - see ?nnet.

Resources