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
Related
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
I am trying to load the following PDF into R, and convert the table into a CSV file.
I have tried both the library(pdftools) and library(tabulizer), & I have spent an afternoon going through various forums, but I do not seem to find an answer that works for me. I can load the PDF to R using the following code
x <- pdf_text("~/Desktop/PlantTraitAsia.pdf")
It uploads just fine, but it is not at all a table of table I can work with.
Here is a link to the PDF file:
http://vege1.kan.ynu.ac.jp/traits/PlantTraitAsia.pdf
I would simply like to upload the table into R, keep the header, and be able to export it to a TXT, CSV, or XLS file.
Thanks for your help
This works well on my machine:
zz <- tabulizer::extract_tables("http://vege1.kan.ynu.ac.jp/traits/PlantTraitAsia.pdf", pages = 2)
head(zz[[1]])
This produces:
[,1] [,2] [,3]
[1,] "ID" "Category\rof\rpermissio" "Species"
[2,] "83" "A" "Abies mariesii Masters"
[3,] "155" "A" "Abies mariesii Masters"
[4,] "225" "A" "Abies mariesii Masters"
[5,] "297" "A" "Abies mariesii Masters"
[6,] "369" "A" "Abies mariesii Masters"
[,4] [,5] [,6]
[1,] "Traits" "Value" "Notes"
[2,] "Maximum heighyt (m)" "18.17" ""
[3,] "Shade tolerance (min. relative\rlight intensity, %), Anderson\r1964. J. Ecol." "1.15" ""
[4,] "Length of fruit (mm)" "8" ""
[5,] "Pollination mode" "Anemophily" ""
[6,] "Type of fruit" "Wing-hair" ""
To get only headers (first line of your table):
zz[[1]][1,]
Here is another approach based on the R package RDCOMClient
library(RDCOMClient)
####################################################################
#### Step 1 : We use the OCR of Word to convert the PDF to Word ####
####################################################################
wordApp <- COMCreate("Word.Application")
wordApp[["Visible"]] <- TRUE
wordApp[["DisplayAlerts"]] <- 0
path_To_PDF_File <- "C:\\PlantTraitAsia.pdf"
path_To_Word_File <- "C:\\PlantTraitAsia.docx"
doc <- wordApp[["Documents"]]$Open(normalizePath(path_To_PDF_File),
ConfirmConversions = FALSE)
doc$SaveAs2(path_To_Word_File)
###############################################################
#### Step 2 : We extract the tables from the word document ####
###############################################################
nb_Tables <- doc$tables()$count()
list_Table <- list()
for(l in 1 : nb_Tables)
{
print(l)
nb_Row <- doc$tables(l)$Rows()$Count()
nb_Col <- doc$tables(l)$Columns()$Count()
mat_Temp <- matrix(NA, nrow = nb_Row, ncol = nb_Col)
for(i in 1 : nb_Row)
{
for(j in 1 : nb_Col)
{
mat_Temp[i, j] <- doc$tables(l)$cell(i, j)$range()$text()
}
}
list_Table[[l]] <- mat_Temp
}
final_Mat <- do.call("rbind", list_Table)
final_Mat[, 1 : 4]
We obtain the following matrix
[,1] [,2] [,3]
[1,] "ID\r\a" "Category of permissio\r\a" "Species\r\a"
[2,] "83\r\a" "A\r\a" "Abies mariesii Masters\r\a"
[3,] "155\r\a" "A\r\a" "Abies mariesii Masters\r\a"
[4,] "225\r\a" "A\r\a" "Abies mariesii Masters\r\a"
[5,] "297\r\a" "A\r\a" "Abies mariesii Masters\r\a"
[6,] "369\r\a" "A\r\a" "Abies mariesii Masters\r\a"
[7,] "706\r\a" "B\r\a" "Abies sachalinensis (Schmidt) Masters\r\a"
[8,] "750\r\a" "B\r\a" "Abies sachalinensis (Schmidt) Masters\r\a"
[9,] "794\r\a" "B\r\a" "Abies sachalinensis (Schmidt) Masters\r\a"
[10,] "837\r\a" "B\r\a" "Abies sachalinensis (Schmidt) Masters\r\a"
[11,] "881\r\a" "B\r\a" "Abies sachalinensis (Schmidt) Masters\r\a"
[12,] "6\r\a" "A\r\a" "Abies spectabilis (D. Don) Mirbel\r\a"
[13,] "10\r\a" "A\r\a" "Abies spectabilis (D. Don) Mirbel\r\a"
[14,] "82\r\a" "A\r\a" "Abies veitchii Lindley\r\a"
[15,] "154\r\a" "A\r\a" "Abies veitchii Lindley\r\a"
[16,] "224\r\a" "A\r\a" "Abies veitchii Lindley\r\a"
[17,] "296\r\a" "A\r\a" "Abies veitchii Lindley\r\a"
[18,] "368\r\a" "A\r\a" "Abies veitchii Lindley\r\a"
[19,] "707\r\a" "B\r\a" "Acanthopanax senticosus (Rupr. et Maxim.) Harms.\r\a"
[20,] "751\r\a" "B\r\a" "Acanthopanax senticosus (Rupr. et Maxim.) Harms.\r\a"
[21,] "795\r\a" "B\r\a" "Acanthopanax senticosus (Rupr. et Maxim.) Harms.\r\a"
[22,] "838\r\a" "B\r\a" "Acanthopanax senticosus (Rupr. et Maxim.) Harms.\r\a"
[23,] "882\r\a" "B\r\a" "Acanthopanax senticosus (Rupr. et Maxim.) Harms.\r\a"
[24,] "708\r\a" "B\r\a" "Acer japonicum Thunb.\r\a"
[25,] "752\r\a" "B\r\a" "Acer japonicum Thunb.\r\a"
[26,] "796\r\a" "B\r\a" "Acer japonicum Thunb.\r\a"
[27,] "839\r\a" "B\r\a" "Acer japonicum Thunb.\r\a"
[28,] "883\r\a" "B\r\a" "Acer japonicum Thunb.\r\a"
[29,] "97\r\a" "A\r\a" "Acer micranthum Sieb. et Zucc.\r\a"
[30,] "169\r\a" "A\r\a" "Acer micranthum Sieb. et Zucc.\r\a"
[31,] "239\r\a" "A\r\a" "Acer micranthum Sieb. et Zucc.\r\a"
[32,] "311\r\a" "A\r\a" "Acer micranthum Sieb. et Zucc.\r\a"
[33,] "383\r\a" "A\r\a" "Acer micranthum Sieb. et Zucc.\r\a"
[34,] "99\r\a" "A\r\a" "Acer mono var. mayrii Sugimoto\r\a"
[35,] "171\r\a" "A\r\a" "Acer mono var. mayrii Sugimoto\r\a"
[36,] "241\r\a" "A\r\a" "Acer mono var. mayrii Sugimoto\r\a"
[37,] "313\r\a" "A\r\a" "Acer mono var. mayrii Sugimoto\r\a"
[38,] "385\r\a" "A\r\a" "Acer mono var. mayrii Sugimoto\r\a"
[39,] "130\r\a" "A\r\a" "Acer morifolium Koidz.\r\a"
[40,] "200\r\a" "A\r\a" "Acer morifolium Koidz.\r\a"
[41,] "272\r\a" "A\r\a" "Acer morifolium Koidz.\r\a"
[42,] "344\r\a" "A\r\a" "Acer morifolium Koidz.\r\a"
[43,] "416\r\a" "A\r\a" "Acer morifolium Koidz.\r\a"
[44,] "472\r\a" "B\r\a" "Acer morifolium Koidz.\r\a"
[45,] "522\r\a" "B\r\a" "Acer morifolium Koidz.\r\a"
[46,] "662\r\a" "B\r\a" "Acer morifolium Koidz.\r\a"
[47,] "709\r\a" "B\r\a" "Actaea asiatica Hara.\r\a"
[48,] "753\r\a" "B\r\a" "Actaea asiatica Hara.\r\a"
[49,] "797\r\a" "B\r\a" "Actaea asiatica Hara.\r\a"
[50,] "840\r\a" "B\r\a" "Actaea asiatica Hara.\r\a"
[51,] "884\r\a" "B\r\a" "Actaea asiatica Hara.\r\a"
[52,] "710\r\a" "B\r\a" "Actinidia arguta (Sieb. et Zucc.) Planch. ex Miq.\r\a"
[53,] "754\r\a" "B\r\a" "Actinidia arguta (Sieb. et Zucc.) Planch. ex Miq.\r\a"
[54,] "798\r\a" "B\r\a" "Actinidia arguta (Sieb. et Zucc.) Planch. ex Miq.\r\a"
[55,] "841\r\a" "B\r\a" "Actinidia arguta (Sieb. et Zucc.) Planch. ex Miq.\r\a"
[56,] "885\r\a" "B\r\a" "Actinidia arguta (Sieb. et Zucc.) Planch. ex Miq.\r\a"
[57,] "51\r\a" "A\r\a" "Alangium javanicum\r\a"
[58,] "473\r\a" "B\r\a" "Aleurites cordata (thunb.) R. Br. ex Steudel.\r\a"
[59,] "523\r\a" "B\r\a" "Aleurites cordata (thunb.) R. Br. ex Steudel.\r\a"
[60,] "580\r\a" "B\r\a" "Aleurites cordata (thunb.) R. Br. ex Steudel.\r\a"
[61,] "622\r\a" "B\r\a" "Aleurites cordata (thunb.) R. Br. ex Steudel.\r\a"
[62,] "663\r\a" "B\r\a" "Aleurites cordata (thunb.) R. Br. ex Steudel.\r\a"
[,4] [,5] [,6]
[1,] "Traits\r\a" "Value\r\a" "Notes\r\a"
[2,] "Maximum heighyt (m)\r\a" "18.17\r\a" "\r\a"
[3,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "1.15\r\a" "\r\a"
[4,] "Length of fruit (mm)\r\a" "8\r\a" "\r\a"
[5,] "Pollination mode\r\a" "Anemophily\r\a" "\r\a"
[6,] "Type of fruit\r\a" "Wing-hair\r\a" "\r\a"
[7,] "Stem type\r\a" "Woody\r\a" "\r\a"
[8,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "3.816887653\r\a" "\r\a"
[9,] "Maximum height (m)\r\a" "24.7\r\a" "\r\a"
[10,] "Vegetative spread distance (m)\r\a" "0\r\a" "\r\a"
[11,] "Vegetative reproduction\r\a" "None\r\a" "\r\a"
[12,] "Maximum heighyt (m)\r\a" "24\r\a" "\r\a"
[13,] "Maximum heighyt (m)\r\a" "45.7\r\a" "\r\a"
[14,] "Maximum heighyt (m)\r\a" "18\r\a" "\r\a"
[15,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "1.57\r\a" "\r\a"
[16,] "Length of fruit (mm)\r\a" "6\r\a" "\r\a"
[17,] "Pollination mode\r\a" "Anemophily\r\a" "\r\a"
[18,] "Type of fruit\r\a" "Wing-hair\r\a" "\r\a"
[19,] "Stem type\r\a" "Woody\r\a" "\r\a"
[20,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "5.629993917\r\a" "\r\a"
[21,] "Maximum height (m)\r\a" "1.5\r\a" "\r\a"
[22,] "Vegetative spread distance (m)\r\a" "1\r\a" "\r\a"
[23,] "Vegetative reproduction\r\a" "Rhyzome\r\a" "\r\a"
[24,] "Stem type\r\a" "Woody\r\a" "\r\a"
[25,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "5.870242245\r\a" "\r\a"
[26,] "Maximum height (m)\r\a" "12.1\r\a" "\r\a"
[27,] "Vegetative spread distance (m)\r\a" "0\r\a" "\r\a"
[28,] "Vegetative reproduction\r\a" "Sprout at the base of stem\r\a" "\r\a"
[29,] "Maximum heighyt (m)\r\a" "9.2\r\a" "\r\a"
[30,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "8.86\r\a" "\r\a"
[31,] "Length of fruit (mm)\r\a" "15\r\a" "\r\a"
[32,] "Pollination mode\r\a" "Entomophily\r\a" "\r\a"
[33,] "Type of fruit\r\a" "Wing-hair\r\a" "\r\a"
[34,] "Maximum heighyt (m)\r\a" "18.3\r\a" "\r\a"
[35,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "7.24\r\a" "\r\a"
[36,] "Length of fruit (mm)\r\a" "23\r\a" "\r\a"
[37,] "Pollination mode\r\a" "Entomophily\r\a" "\r\a"
[38,] "Type of fruit\r\a" "Wing-hair\r\a" "\r\a"
[39,] "Maximum heighyt (m)\r\a" "13.5\r\a" "\r\a"
[40,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "27.05059776\r\a" "\r\a"
[41,] "Length of fruit (mm)\r\a" "15\r\a" "\r\a"
[42,] "Pollination mode\r\a" "Entomophily\r\a" "\r\a"
[43,] "Type of fruit\r\a" "Wing-hair\r\a" "\r\a"
[44,] "Vegetative spread distance (m)\r\a" "0\r\a" "\r\a"
[45,] "Stem type\r\a" "Woody\r\a" "\r\a"
[46,] "Vegetative reproduction\r\a" "None\r\a" "\r\a"
[47,] "Stem type\r\a" "Herbaceous\r\a" "\r\a"
[48,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "3.236520779\r\a" "\r\a"
[49,] "Maximum height (m)\r\a" "0.61\r\a" "\r\a"
[50,] "Vegetative spread distance (m)\r\a" "0\r\a" "\r\a"
[51,] "Vegetative reproduction\r\a" "Sprout at the base of stem\r\a" "\r\a"
[52,] "Stem type\r\a" "Liana\r\a" "\r\a"
[53,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "5.547807614\r\a" "\r\a"
[54,] "Maximum height (m)\r\a" "16.3\r\a" "\r\a"
[55,] "Vegetative spread distance (m)\r\a" "10\r\a" "\r\a"
[56,] "Vegetative reproduction\r\a" "Rooting from vine\r\a" "\r\a"
[57,] "Maximum heighyt (m)\r\a" "16.8\r\a" "\r\a"
[58,] "Vegetative spread distance (m)\r\a" "0\r\a" "\r\a"
[59,] "Stem type\r\a" "Woody\r\a" "\r\a"
[60,] "Shade tolerance (min. relative light intensity, %), Anderson 1964. J. Ecol.\r\a" "12.19244\r\a" "\r\a"
[61,] "Maximum heighyt (m)\r\a" "9.7\r\a" "\r\a"
[62,] "Vegetative reproduction\r\a" "None\r\a" "\r\a"
[ reached getOption("max.print") -- omitted 863 rows ]
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
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)
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.