[R] Why I am getting error when writing a function for optim?
I have three directories where there is inside each of them 5 files.each file is a matrix lines 500 and columns 300. I want to perform an optimization using values from three corresponding pixels.Finally I get a matrix of lines 500 and columns 300 for each parameter in my equation: y=(ax1+bx2+c)^2+d reproducible example: dat1 - array(1:60, c(3,5,4));dat2 - array(rnorm(60), c(3,5,4)); dat3 -array(rnorm(60), c(3,5,4)) reorder dimensions dat1 - aperm(dat1, c(3,1,2));dat2 - aperm(dat2, c(3,1,2)); dat3 - aperm(dat2, c(3,1,2)) make array a matrix dat1a - dat1 ; dim(dat1a) - c(dim(dat1)[1],prod(dim(dat1)[2:3])) dat2a - dat2; dim(dat2a) - c(dim(dat2)[1],prod(dim(dat2)[2:3])) dat3a - dat3 ; dim(dat3a) - c(dim(dat3)[1],prod(dim(dat3)[2:3])) fun function(x1,x2, y) { keep - !(is.na(x) | is.na(x2)| is.na(y)) if (sum(keep) 2) { #less than 3 non-NA values? temp - sum((y[keep] - (p[1]*x1[keep]+p[2]*x2[keep]+p[3])^p[4]+p[5])^2) res - optim(rep(NA,5),temp) } else { res - c(NA, NA,NA,NA,NA)#five parameters } res } res - mapply(fun, x1=as.data.frame(dat1a), x2=as.data.frame(dat2a), y=as.data.frame(dat3a)) Error in optim(rep(NA, 5), temp) : non-finite value supplied by optim Any idea please on how to correct my function? -- View this message in context: http://r.789695.n4.nabble.com/Why-I-am-getting-error-when-writing-a-function-for-optim-tp4703205.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to add box layer using levelplot in R?
I would like to add some boxes with special extent to my plot. Example gh - raster() gh[] - 1:ncell(gh) SP - spsample(Spatial(bbox=bbox(gh)), 10, type=random) Then plot them levelplot(gh, col.regions = rev(terrain.colors(255)), cuts=254, margin=FALSE) + layer(sp.points(SP, col = red)) this plots a map with several crosses in it but I need to plot a box with spacial extent: extent(gh) = extent(c(xmn=-180,xmx=180,ymn=-90,ymx=90)) e6 - extent( 2 , 8 , 45 , 51 ) I wan to add e6 to the plot and put the number2 inside the box.Any hint please -- View this message in context: http://r.789695.n4.nabble.com/How-to-add-box-layer-using-levelplot-in-R-tp4685658.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] how to add a box to map plotted by a levelplot?
conne2 - file(C:\\ome1440s.bin,rb) bioms- readBin(conne2, integer(), size=1, n=1440*720, signed=F) library(raster) library(rasterVis) data(wrld_simpl) library(maptools) ## needed for wrld_simpl r - raster(nrow=720, ncol=1440) r[] - bioms;r - ratify(r);rat - levels(r)[[1]]; rat$soil - LETTERS[1:13]; levels(r) - rat myPal - c('gray80','gray80','blanchedalmond' ,'chartreuse','yellow','navajowhite2','salmon','lightskyblue','brown4','orange','burlywood4','palegreen','forestgreen') levelplot(r, col.regions=myPal) + layer(sp.polygons(wrld_simpl, lwd=0.5)) I want to add a box representing this zone of latitude and longtitude: e6 - extent( 2 , 8 , 45 , 51 )#erop plot( e6 , add = TRUE ) but I got this Error: invalid graphics state Error: invalid graphics state -- View this message in context: http://r.789695.n4.nabble.com/how-to-add-a-box-to-map-plotted-by-a-levelplot-tp4676815.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to add a box to map plotted by a levelplot?
That worked fine, thanks. But only worked when the values are positive but when I added negative values, the box was wrong. May be the order I lat and long should be different?any ideas -- View this message in context: http://r.789695.n4.nabble.com/how-to-add-a-box-to-map-plotted-by-a-levelplot-tp4676815p4676820.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to add a box to map plotted by a levelplot?
Thanks.That worked fine.Is there a way to print a number like 1 inside the box plotted? -- View this message in context: http://r.789695.n4.nabble.com/how-to-add-a-box-to-map-plotted-by-a-levelplot-tp4676815p4676825.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] problem in while loop?
I have three datasets that I want to compute the errors between them using linear regression.for this, I want to iterate to reach certain criteria for the calibration. if changes become smaller than eps the iteration is successful, hence stop and write parameters into cal:eps=0.1 if number of iterations is itermax the iteration failed, hence stop and fill cal with missing value itermax=400 So I tried this code: x= c(5,2,4,2,1) y= c(5,3,4,6,9) z= c(5,8,4,7,3) itermax=400 get initial calibration parameters, here we assume that:x is the reference dataset offset x_a=0, slope x_b=1 the other two datasets y, z are calibrated to x using a simple linear regression res=lm(x~y) y_a=coef(res)[1] ; y_b=coef(res)[2] res1=lm(x~z) z_a=coef(res1)[1] ; z_b=coef(res1)[2] y_t = y/y_b - y_a/y_b # calibrate y z_t = z/z_b - z_a/z_b #calibrate z x_e = sqrt(mean((x-y_t)*(x-z_t)))#calculate error of x iter - 0 while(((x_e-x) 0.1) (iter itermax)) { iter - 0 ##start iteration x = x_e res=lm(x~y) y_a=coef(res)[1] ; y_b=coef(res)[2] res1=lm(x~z) z_a=coef(res1)[1] ; z_b=coef(res1)[2] y_t = y/y_b - y_a/y_b # calibrate y z_t = z/z_b - z_a/z_b #calibrate z x_e = sqrt(mean((x-y_t)*(x-z_t))) iter - iter + 1 # increase iteration counter } But I got the same result for X_e before and after the loop: x_e [1] 6.454089 -- View this message in context: http://r.789695.n4.nabble.com/problem-in-while-loop-tp4674962.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] What is the maximum limit for an array?
Hello All, I am having a problem with this: file-array(dim=c(1440,720,700,3)) Error in array(dim = c(1440, 720, 700, 3)) : 'dim' specifies too large an array I have a memory of 20GB, But I do not know where is the problem!Any help When I replaced 700 by any number bellow like( 600,500),it worked without any problem. -- View this message in context: http://r.789695.n4.nabble.com/What-is-the-maximum-limit-for-an-array-tp4671395.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] What is the maximum limit for an array?
So If download R 3,my problem will be gone? -- View this message in context: http://r.789695.n4.nabble.com/What-is-the-maximum-limit-for-an-array-tp4671395p4671409.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to represent certain values in a file as we want?
I got this erro Error in c(red, blue, green)[r] : invalid subscript type 'S4' -- View this message in context: http://r.789695.n4.nabble.com/How-to-represent-certain-values-in-a-file-as-we-want-tp4662925p4662935.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to represent certain values in a file as we want?
I have a raster file(1440*720 rows) contains values of 1 ,2 , and 3. when I plot the file , I got a map of three colors but I do not know which is which. How can I put those colors as as I want : 1=red 2=blue 3=green code: pvm - file(C:\\User_sm-das.bin,rb) cor1- readBin(pvm, numeric(), size=4, n=1440*720, signed=TRUE) r -raster(t(matrix((data=cor1), ncol=720, nrow=1440))) image(r) -- View this message in context: http://r.789695.n4.nabble.com/How-to-represent-certain-values-in-a-file-as-we-want-tp4662925.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] why a moving average fun takes too long time!!
I am calculating a moving average for 601 rasters (720, 1440)and it is taking a day to finish. I wonder if this is normal or something is wrong I am using Linux . code: dir1 - list.files(/home/spa4-358-4-365, *.img, full.names = TRUE) saf=stack(dir1) meansmosas - overlay(saf,fun=function(x) movingFun(x, fun=mean,n=35, na.rm=TRUE)) info: saf class : RasterStack dimensions : 720, 1440, 1036800, 601 (nrow, ncol, ncell, nlayers) resolution : 0.25, 0.25 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 -- View this message in context: http://r.789695.n4.nabble.com/why-a-moving-average-fun-takes-too-long-time-tp4661920.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Why stacking rasters return NAs?
Thanks. Could you please tell me how to stack all those files properly? -- View this message in context: http://r.789695.n4.nabble.com/Why-stacking-rasters-return-NAs-tp4661706p4661784.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Why stacking rasters return NAs?
stacking the rasters will make them as multi layers as they have the same dimension exten ... so as one layer. so we can do calculation with them. -- View this message in context: http://r.789695.n4.nabble.com/Why-stacking-rasters-return-NAs-tp4661706p4661787.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to get the rolling standard deviation in rasters?
I am using this code to calculate the moving average mean.It worked fine but when I wanted to also calculate based on sd(stander deviation) I got the error shown below. I read this documentation of R movingFun and found that sd was mentioned at http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/raster/html/movingFun.html I wonder if needs any other things to conseder or sd is not at all supplied by this function. sami- list.files(C:\\Usersfinal-2010, *.envi, full.names = TRUE) saf=stack(sami) dm - overlay(saf ,fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE,fill=NA)) ### worked very well ds - overlay(saf ,fun=function(x) movingFun(x, fun= sd, n=3, na.rm=TRUE)) ### did not work Error in .overlayList(x, fun = fun, filename = filename, ...) : cannot use this formula, probably because it is not vectorized saf class : RasterStack dimensions : 720, 1440, 1036800, 12 (nrow, ncol, ncell, nlayers) resolution : 0.25, 0.25 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 names : SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, SM_RE01_M//7_amenlast, -- View this message in context: http://r.789695.n4.nabble.com/How-to-get-the-rolling-standard-deviation-in-rasters-tp4661819.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Why stacking rasters return NAs?
I have several rasters that I want to do some calculations ,basically calculating the moving average. dir2 - list.files(D:\\2010+2011, *.bin, full.names = TRUE) saf=stack(dir2) movi - overlay(stack(saf),fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE)) Error in .overlayList(x, fun = fun, filename = filename, ...) : cannot use this formula, probably because it is not vectorized I then checked the data but found that all values were returnd as NA and this may explain why i am getting the error. saf class : RasterStack dimensions : 720, 1440, 1036800, 601 (nrow, ncol, ncell, nlayers) resolution : 0.25, 0.25 (x, y) extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs names : Vol_025_H//00_1_wgs84, Vol_025_H//00_1_wgs84, Vol_025_H//00_1_wgs84,Vol_025_H//00_1_wgs84, Vol_025_H//00_1_wgs84, Vol_025_H//00_1_wgs84, , ... min values :NA,NA, NA, NA,NA,NA, NA, NA,NA,NA, NA,NA,NA,NA, NA, ... max values :NA,NA, NA,NA,NA,NA, NA,NA,NA,NA, NA, NA,NA,NA, NA, ... I wonder why this is happening, I checked the files separably(summary) and everything was right!as you can see bellow: ol_025_H14_2011092000_1_wgs84 Vol_025_H14_2011092100_1_wgs84 Vol_025_H14_2011092200_1_wgs84 Vol_025_H14_2011092300_1_wgs84 Vol_025_H14_2011092400_1_wgs84 Min. 0.0 0.000 0.000 0.000 0.000 1st Qu.0.31883 0.3163167 0.3146436 0.3113111 0.3064551 Median .0 .000 .000 .000 .000 3rd Qu. .0 .000 .000 .000 .000 Max..0 .000 .000 .000 .000 NA's 0.0 0.000 0.000 0.0 I am gratful to anyhelp -- View this message in context: http://r.789695.n4.nabble.com/Why-stacking-rasters-return-NAs-tp4661706.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to loop several binary files from two directories?
I have two binary files(rasters) with the same dimensions. The first file is called `over` and the second is `corr`. I want to replace values in `over` by `NA` whenever `corr` is greater than 0.5. to read the two files we can use: conne - file(C:corr.bin,rb) over - readBin(conne, numeric(), size=4, n=1440*720, signed=TRUE) frf - file(C:cor206.bin,rb) corr - readBin(frf, numeric(), size=4, n=1440*720, signed=TRUE) to replace values in `over` by `NA` whenever `corr` is greater than 0.5: over[corr 0.4] = NA to write the results: to.write = file(paste(C:flag.bin, sep=), wb) writeBin(as.double(over), to.write, size = 4) close(to.write) Now I want to do the same but with 24 files(12 files in each directory) i.e. to loop thru several files : so file1 from the first directory with file1 from the second directory and so on To read the files from both directories firstdirctory - list.files(C:final-2010, *.bin, full.names = TRUE) seconddirctory - list.files(C:jop-2012, *.bin, full.names = TRUE) -- View this message in context: http://r.789695.n4.nabble.com/How-to-loop-several-binary-files-from-two-directories-tp4660205.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to merge two functions into one?
hello Rui, Many thanks. That really helped me.you are totally right about[[4]] -- View this message in context: http://r.789695.n4.nabble.com/How-to-merge-two-functions-into-one-tp4659365p4659446.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to merge two functions into one?
I am using the code below to calculate the correlation map between two datasets. This code worked fine. dir1 - list.files(D:thly, *.bin, full.names = TRUE) dir2 - list.files(D:002, *.envi, full.names = TRUE) file_tot - array(dim = c(1440, 720, 11, 2)) for(i in 1:length(dir1)) { file_tot[, , i, 1] - readBin(dir1[i], numeric(), size = 4, n = 1440 * 720, signed = T) file_tot[, , i, 2] - readBin(dir2[i], numeric(), size = 4, n = 1440 * 720, signed = T) } resultscor-apply(file_tot,c(1,2),function(x){cor(x[,1],x[,2],use = na.or.complete)}) I would like to calculate the correlation only when the `P-value is lower than 0.05`. so this function bellow will do the job: return_cor = function(x, y) { z = cor.test(x,y) if(z[[3]] 0.05) { return(z[[5]]) } else { return(NA) } } However I got this error(as some pairs of my data are less then 3): Error in cor.test.default(x, y) : not enough finite observations in order to avoid this error and return NA when there are less than 3 pairs,this function does the job: cor_withN - function(...) { res - try(cor.test(...)$estimate, silent=TRUE) ifelse(class(res)==try-error, NA, res) } Both functions worked perfectly.How can we merge both functions into one function so we calculate correlation when P value is (certain value, threshold) and also do the calculations even if there are less than 3 pairs. -- View this message in context: http://r.789695.n4.nabble.com/How-to-merge-two-functions-into-one-tp4659365.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to calculate the moving average for binary files?
I have 12 binary (raster) files https://echange-fichiers.inra.fr/get?k=k3M2jatJyHy65Cs99G4 . I would like to calculate the moving average for the 12 values for each pixel in the 12 files. For a simple vector we can get a moving average by using this : x - c(1,2,3,NA,NA,4,6,5,6,4,2,5) movingmean - rollapply(x, 3, FUN = mean, na.rm = T,fill=NA) now I want to do the same but with binary files and I tried: files - list.files(C:final-2010, *.envi, full.names = TRUE) results - list() for (.files in files) { # read in the 12 files as a vector of numbers # that we take the average of x - do.call(rbind,(lapply(.files, readBin , double() , size = 4 ,n =1440 * 720 , signed = T))) # take the moving average across the 12 values # from the 12 files for each pixel results[[length(results) + 1L]] - rollapply(x, 3, FUN = mean,na.rm = T) } But got this error: Error in seq.default(start.at, NROW(data), by = by) : wrong sign in 'by' argument -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-moving-average-for-binary-files-tp4658986.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How write raster files after manipulation?
I have 12 binary (raster) files https://echange-fichiers.inra.fr/get?k=k3M2jatJyHy65Cs99G4 . I would like to calculate the moving average for the 12 values for each pixel in the 12 files. For a simple vector we can get a moving average by using this : x - c(1,2,3,NA,NA,4,6,5,6,4,2,5) movingmean - rollapply(x, 3, FUN = mean, na.rm = T) now I want to do the same but with rasters and I tried: files - list.files(C:final-2010, *.envi, full.names = TRUE) results- overlay(stack(files), fun=function(x) movingFun(x, fun=mean, n=3, na.rm=TRUE)) That worked without errors but I want to write the results and check them: I tried several ways of writing but all of them got errors: 1)for (i in seq_along(results)) { fileName - sprintf(C:\\New folder (3)\\final-2010.bin, i) writeBin(as.double(results[i]), fileName, size = 4) } Error in writeBin(as.double(results[i]), fileName, size = 4) : (list) object cannot be coerced to type 'double' 2) for (i in seq_along(results)) { fileName - sprintf(C:\\New folder (3)\\final-2010.bin, i) writeBin(as.double(results[[i]]), fileName, size = 4) } Error in as.double(results[[i]]) : cannot coerce type 'S4' to vector of type 'double' 3) for(i in 1:length(results)){ fileName - strsplit(results[i],split='\\.')[[1]][1] outputFile - paste(fileName,'_amenlast','.envi',sep='') rf - writeRaster(results, filename=outputFile, overwrite=TRUE) } Error in strsplit(results[i], split = \\.) : non-character argument to write the results: Any ideas? -- View this message in context: http://r.789695.n4.nabble.com/How-write-raster-files-after-manipulation-tp4658539.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to extract values from a raster according to Lat and long of the values?
jholtman: I do not understand you question? -- View this message in context: http://r.789695.n4.nabble.com/how-to-extract-values-from-a-raster-according-to-Lat-and-long-of-the-values-tp4656767p4656847.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to extract values from a raster according to Lat and long of the values?
extract will extract values if you provide the x , y but then who to know which lat and long correspond to which x and y -- View this message in context: http://r.789695.n4.nabble.com/how-to-extract-values-from-a-raster-according-to-Lat-and-long-of-the-values-tp4656767p4656848.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] how to extract values from a raster according to Lat and long of the values?
having 12 files with 12 hdrs for one year:these files are raster (projected WGS84,lat long):https://echange-fichiers.inra.fr/get?k=rLSyoavrnifGyH5XrlO samples = 1440 lines = 720 bands = 1 header offset = 0 file type = ENVI Standard data type = 4 interleave = bsq byte order = 0 map info = { Geographic Lat/Lon, 1, 1, -180, 90, 0.25, 0.25,WGS-84} coordinate system string = GEOGCS[GCS_WGS_1984,DATUM[D_WGS_1984, SPHEROID[WGS_1984,6378137,298.257223563]] ,PRIMEM[Greenwich,0],UNIT[Degree,0.017453292519943295]] } These lines will open the files as a list: a-list.files(D:\\ECV\\2010, *.envi, full.names = TRUE) for(i in 1:length(a)){ d - raster(a[i]} I would like to extract the values correspond to 44.8386° N, 0.5783° W from all files as txt file -- View this message in context: http://r.789695.n4.nabble.com/how-to-extract-values-from-a-raster-according-to-Lat-and-long-of-the-values-tp4656767.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to add a function to another written code?
I have two directories https://echange-fichiers.inra.fr/get?k=AcHKdNI4No44GEsj7PK with 12 (global maps)binary files in each.I used the code given below to calculate the spatial correlation between these files and it worked well(the output is a global correlation map). I wonder if there is a simple way to calculate RMSE and bias along with cor.so finally we get three outputs (bias map,RMSE map,cor map) instead of only(cor map). Could anyone tell me how to add the RMSE function written below the code of Corr so that When I run the code.I get both RMSE map,corr map. RMSE function RMSE - function(obs, sat){ sqrt(mean((obs-sat)^2, na.rm=T)) } Corr code dir1 - list.files(D:\\2010\\data\\monthly, *.bin, full.names = TRUE) dir1- mixedsort(dir1) dir2 - list.files(D:\\RE01\\MIR_CLF3MD\\2010\\final-2010, *.envi, full.names = TRUE) dir2- mixedsort(dir2) file_tot-array(dim=c(1440,720,12,2)) for(i in 1:length(dir1)){ file_tot[,,i,1] - readBin(dir1[i], numeric(), size = 4 ,n = 1440 * 720 , signed = T) file_tot[,,i,2] - readBin(dir2[i], numeric(), size = 4 ,n = 1440 * 720 , signed = T) } resultsr-apply(file_tot,c(1,2),function(x){cor(x[,1],x[,2])}) to.write = file(paste(C:\\corrCCIandSMOS2010.bin,sep=),wb) writeBin(as.double(resultsr), to.write, size = 4) I am gratefull for any ideas -- View this message in context: http://r.789695.n4.nabble.com/How-to-add-a-function-to-another-written-code-tp4656499.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to re-project ease( Equal Area Scalable Earth) grid with a ~25 km cylindrical projection to WGS84 0.25 degree?
In fact,it is netcdf file(even if the extension is DBL). I am new to prjections and i just tried that spTransform,I did not know projectraster. Could you please tell me what is the command using project raster.I wonder if you have tried to re-project the file? -- View this message in context: http://r.789695.n4.nabble.com/How-to-re-project-ease-Equal-Area-Scalable-Earth-grid-with-a-25-km-cylindrical-projection-to-WGS84-0-tp4655977p4656055.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to calculate monthly average from daily files in R?
Thanks,I am using windows -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-monthly-average-from-daily-files-in-R-tp4655869p4655933.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to re-project ease( Equal Area Scalable Earth) grid with a ~25 km cylindrical projection to WGS84 0.25 degree?
I have nc files for global soil moisture,here is one file https://echange-fichiers.inra.fr/get?k=f9DDllPKdUKs5ZNQwfq from the metadata ,the projection is cylindrical and the resolution is 25 km(it is based on authalic sphere based on International 1924 ellipsoid).As I want to compare with other data, I have to make them identical. - my other data are in WGS84 with 0.25*0.25 degree resolution and extent -180, 180, -90, 90. So I want to re-project the data I have here from EASE grid, cylindrical,25 km ,1383 pixel 586 lines to WGS84,0.25*0.25 degree,1440 pixel 720 lines: f=open.ncdf(C:\\Users\\aalyaari\\Desktop\\SM_RE01_MIR_CLF31D_20100812T00_20100812T235959_246_001_7.DBL) A = get.var.ncdf(nc=f,varid=Soil_Moisture,verbose=TRUE) I tried this: d -raster(C:\\Users\\aalyaari\\Desktop\\SM_RE01_MIR_CLF31D_20100812T00_20100812T235959_246_001_7.nc, varname = Soil_Moisture) d: class : RasterLayer dimensions : 586, 1383, 810438 (nrow, ncol, ncell) resolution : 0.2603037, 0.2916659 (x, y) extent : -180, 180, -85.4581, 85.4581 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 names : Retrieved.soil.moisture.value zvar: Soil_Moisture a - spTransform(d, CRS (+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs)) but I got this error: Error in function (classes, fdef, mtable) : unable to find an inherited method for function ‘spTransform’ for signature ‘RasterLayer, CRS’ -- View this message in context: http://r.789695.n4.nabble.com/How-to-re-project-ease-Equal-Area-Scalable-Earth-grid-with-a-25-km-cylindrical-projection-to-WGS84-0-tp4655977.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to calculate monthly average from daily files in R?
I have 365 binary files: https://echange-fichiers.inra.fr/get?k=oy3CN1yV1Um7ouRWm2U ,I want to calculate the monthly average. So from the 365 files, I will get 12 files.I would like also to tell R not to take into account the no-data value (-32765).for example, for the first month, there are 31 records: 3 of these records has the value -32765,I want R to take the average of the rest records(28 records) and so on with all months. This code will take the average of every 30 files(any idea on how to make it according to number of days in a month?and not to take into account the no-data values) files- list.files(C:\\New folder (4)\\New folder, *.bin,full.names=TRUE) # assume that we want to take the average of every 30 files files.group- split(files , rep(seq_along(files), each = 30, length =length(files))) results- list() for (.files in files.group){ # read in the 30 files as a vector of numbers that you take the average of x- do.call(rbind,(lapply(.files, readBin , double() , size = 4 ,n =360 * 720 , signed =T))) ## take the means across the 30 files results[[length(results) + 1L]]- colMeans(x)} close(x) for (i in seq_along(results)){ fileName - sprintf(C:/New folder/glo_%d.flt, i) writeBin(as.double(results[[i]]), fileName, size = 4)} -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-monthly-average-from-daily-files-in-R-tp4655869.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to open grib file in R?
I have this https://echange-fichiers.inra.fr/get?k=6TdTdezNeZwAqJtpwVm grib file which is a regular lat-lon 0.25x0.25 degrees and contains 4 fields . I tried to open it using : grib - readGDAL(C:\\Users\\aalyaari\\Desktop\\Vol_025_H14_2010060700.grib) but I got this error: C:\Users\aalyaari\Desktop\Vol_025_H14_2010060700.grib has GDAL driver GRIB and has 721 rows and 1440 columns Error in validityMethod(as(object, superClass)) : Geographical CRS given to non-conformant data: -90.125 90.125 Any idea why I am getting this error? -- View this message in context: http://r.789695.n4.nabble.com/How-to-open-grib-file-in-R-tp4655497.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to open grib file in R?
I have just contacted the sources and I am told that he checked min/max lat/lon for the file and it was right. He is using Linux while I am using Windows, would that be problematic? -- View this message in context: http://r.789695.n4.nabble.com/How-to-open-grib-file-in-R-tp4655497p4655517.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] can R read a binary data block file (.DBL)?
Dear R users, I have been searching thru the documentation of R but did not find anything about reading or manipulating a binary data block file (.DBL).a link to one example https://echange-fichiers.inra.fr/get?k=5Hzw2B1wZDng9ztO34E -- View this message in context: http://r.789695.n4.nabble.com/can-R-read-a-binary-data-block-file-DBL-tp4652654.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to calculate the spatial correlation of several files?
So where is the final correlation map Can we write it: to.write = file(paste(C:\\Users\\aalyaari\\desktop\\corr1.bin,sep=),wb) writeBin(as.double(results[[.f]]), to.write, size = 4) -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-spatial-correlation-of-several-files-tp4651888p4652004.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to calculate the spatial correlation of several files?
dir1 - list.files(C:\\Users\\aalyaari\\Desktop\\cor, *.bin, full.names = TRUE) dir2 - list.files(C:\\Users\\aalyaari\\Desktop\\cor2, *.bin, full.names = TRUE) results - list() for (.files in dir1){ # read in the 365 files as a vector of numbers for dir1 file1 - do.call(rbind,(lapply(.files, readBin , integer() , size = 2 , n = 360 * 720 , signed = T)))} for (.files in dir2){ # read in the 365 files as a vector of numbers for dir2 file2- do.call(rbind,(lapply(.files, readBin , integer() , size = 2 , n = 360 * 720 , signed = T))) } # Now each file in both directories is a vector. I am not sure how to tell R to correlate the first column in dir1 to the correspond column from dir2. we will finally get only one spatial correlation map. I tried to this: # calculate the correlation so we will get a correlation map for (.files in seq_along(dir1)){ results[[length(results) + 1L]]- cor(file1 ,file2) } I got error:Error in cor(file1, file2) : allocMatrix: too many elements specified` -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-spatial-correlation-of-several-files-tp4651888.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to calculate the spatial correlation of several files?
Thanks you meant it shoud be: file1=do.call(rbind, lapply(dir1, readBin, integer(), size = 2, n = 360 * 720, signed = T)) file2=do.call(rbind, lapply(dir2, readBin, integer(), size = 2, n = 360 * 720, signed = T)) Please see the error for (.f in seq_along(dir1)){ + results[[.f]]- cor(file1[, .f] ,file2[, .f]) + } Error in file2[, .f] : incorrect number of dimensions -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-spatial-correlation-of-several-files-tp4651888p4651901.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] install the ggplot2 package
Has one try to install the ggplot2 package recently? I tried to install it on my new system and had trouble: I tried a different CRAN mirror but didn't work library(ggplot2) Error in loadNamespace(i, c(lib.loc, .libPaths())) : there is no package called ‘stringr’ In addition: Warning message: package ‘ggplot2’ was built under R version 2.15.2 Error: package/namespace load failed for ‘ggplot2’ -- View this message in context: http://r.789695.n4.nabble.com/install-the-ggplot2-package-tp4650935.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] install the ggplot2 package
I did upgrade but that did not solve the problem -- View this message in context: http://r.789695.n4.nabble.com/install-the-ggplot2-package-tp4650935p4650985.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How can I map numbers to colours with raster?
This code will read binary file and display it as a map. may problem is that this code is using a continuous colour scheme, even though I have discrete data (which is a classification scheme). How can I map numbers to colours with raster? Please require(raster) conne - file(C:\\lai.bin, rb) sd- readBin(conne, integer(), size=1, n=360*720, signed=F) y-t(matrix((data=sd), ncol=360, nrow=720)) r = raster(y) extent(r) = extent(c(xmn=-180,xmx=180,ymn=-90,ymx=90)) plot(r) -- View this message in context: http://r.789695.n4.nabble.com/How-can-I-map-numbers-to-colours-with-raster-tp4647742.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How can I map numbers to colours with raster?
Yes I did check but didn't know I modify the code accordingly. -- View this message in context: http://r.789695.n4.nabble.com/How-can-I-map-numbers-to-colours-with-raster-tp4647742p4647744.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] How to calculate the statistcs for extracted region?
I have a binary file(a) with size of (360 720 )for the globe.I wrote the code given below to read and extract an area (south america)from that file. when I use summary for the whole file I got: summary(a, na.rm=FALSE) Min. 1st Qu. MedianMean 3rd Qu.Max.NA's 0.001.003.004.157.00 20.00 200083 . But when I used summary for the region(b) which I extracted, I got many V1,V2, V3. Which is not right I should have got one line (as for a )not many V1,V2. Here is the code: X - c(200:300) Y - c(150:190) conne - file(C:\\initial-WTD.bin, rb) a=readBin(conne, numeric(), size=4, n=360*720, signed=TRUE) a[a == -] - NA y - matrix(data=a,ncol=360,nrow=720) image(t(t(y[X,Y])),ylim=c(1,0)) b = y[X,Y] summary(b,na.rm=TRUE) V1V2V3V4V5 V6 V7 Min. : 1.000 Min. : 1.000 Min. : 1.000 Min. : 1.000 Min. : 0.000 Min. : 0.000 1st Qu.: 4.000 1st Qu.: 4.000 1st Qu.: 4.000 1st Qu.: 3.000 1st Qu.: 2.000 1st Qu.: 2.000 Median : 5.000 Median : 6.000 Median : 5.000 Median : 5.000 Median : 5.000 Median : 5.000 Mean : 5.808 Mean : 5.962 Mean : 5.506 Mean : 4.946 Mean : 5.068 Mean : 4.829 3rd Qu.: 6.750 3rd Qu.: 7.000 3rd Qu.: 7.000 3rd Qu.: 6.000 3rd Qu.: 7.000 3rd Qu.: 6.000 Max. :18.000 Max. :19.000 Max. :17.000 Max. :13.000 Max. :19.000 Max. :18.000 -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-statistcs-for-extracted-region-tp4633295.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R does not recognise columns and rows as they are supposed to be
Yes I did so. Yes the first values are the right ones: - -. so this meant that I should consider my data as: double/4/little Is it so? file - C:\\Users\\aalyaari\\Documents\\INRA\\WFD_reprocessed\\dialyswco\\2001\\SWdown_200101_01.img for(what in c(double, integer)) { + for(size in c(4, 8)) { + for(endian in c(little, big)) { + cat(sep=, what, /, size, /, endian, :\n ); + print(readBin(file, what=what, size=size, endian=endian, n=6)) + } + } + } double/4/little: [1] - - - - - - double/4/big: [1] 5.520452e-39 5.520452e-39 5.520452e-39 5.520452e-39 5.520452e-39 5.520452e-39 double/8/little: [1] -5.592396e+29 -5.592396e+29 -5.592396e+29 -5.592396e+29 -5.592396e+29 -5.592396e+29 double/8/big: [1] 1.563804e-307 1.563804e-307 1.563804e-307 1.563804e-307 1.563804e-307 1.563804e-307 integer/4/little: [1] -971228160 -971228160 -971228160 -971228160 -971228160 -971228160 integer/4/big: [1] 3939526 3939526 3939526 3939526 3939526 3939526 integer/8/little: [1] -971228160 -971228160 -971228160 -971228160 -971228160 -971228160 integer/8/big: [1] 3939526 3939526 3939526 3939526 3939526 3939526 -- View this message in context: http://r.789695.n4.nabble.com/R-does-not-recognise-columns-and-rows-as-they-are-supposed-to-be-tp4631217p4631344.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R does not recognise columns and rows as they are supposed to be
The trick may be behind not reading the files properly is that all my 365 files donot have the same name. for example: files from number 1 to number9 are named for the first month: SWdown_200101_01.img SWdown_200101_09.img files from number 10 to number30 are named: SWdown_200101_10.img SWdown_200101_30.img And so on for the second month: files from number 1 to number9 are named: SWdown_200102_01.img SWdown_200102_09.img files from number 10 to number30 are named: SWdown_200102_10.img SWdown_200102_30.img and in my code I just set the dierctory: dir1- list.files(C:\\Users\\aalyaari\\Desktop\\New folder (11)\\, *.img, full.names = TRUE). assuming that R would read files in order.But I do not really know if this right or I shall specify the names. X-(82:85) ; Y-(364:367) # for sellected region extract - double() dir1- list.files(C:\\Users\\aalyaari\\Desktop\\New folder (11)\\, *.bin, full.names = TRUE) for (i in 1:365) { conne - file(dir1[i], rb) file1- readBin(conne, numeric(),size=4, n=360*720, endian=little) file2-matrix(data=file1,ncol=720,nrow=360) extract[i]-mean(file2[X,Y],na.rm=TRUE) close(conne) } write.table(as.double(extract),C:\\Users\\aalyaari\\Desktop\\New folder (10)\\new6.txt) -- View this message in context: http://r.789695.n4.nabble.com/R-does-not-recognise-columns-and-rows-as-they-are-supposed-to-be-tp4631217p4631352.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R does not recognise columns and rows as they are supposed to be
I tried to read only one file and get some information but this is what I got: sam=file(C:\\Users\\2001\\SWdown_200101_01.img, rb) file1- readBin(sam, double(),size=4, n=360*720) file.info(file1)$size Error in file.info(file1) : invalid filename argument dim(file1) NULL -- View this message in context: http://r.789695.n4.nabble.com/R-does-not-recognise-columns-and-rows-as-they-are-supposed-to-be-tp4631217p4631366.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R does not recognise columns and rows as they are supposed to be
Yes I exactly followed what you all suggested: X-(82:92) ; Y-(364:369) # for sellected region extract - double(365) setwd(C:\\Users\\aalyaari\\Desktop\\New folder (10)\\) listfile-dir() for (i in 1:365) { + conne - file(listfile[i], rb) + file1- readBin(conne, double(), n=360*720) + file2-matrix(data=file1,ncol=720,nrow=360) + extract[i]-mean(file2[X,Y],na.rm=TRUE) + close(conne) } write.table(extract,C:\\Users\\aalyaari\\Desktop\\New folder (10)\\samregion1.txt) But I am still getting(negative values) all values like: -3.75E+306 -1.30E+54 -1.22E+58 and the right ones should be like: 22.25 22.76 33.25 -- View this message in context: http://r.789695.n4.nabble.com/R-does-not-recognise-columns-and-rows-as-they-are-supposed-to-be-tp4631217p4631279.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] R does not recognise columns and rows as they are supposed to be
Dear All, The code given bellow is to extract values of one region and write that to a text file(there are 365 binary files in the directory). The problem which I am facing is that all my files are binary with size of 360 rows and 720 columns. I specified that in this line:file2-matrix(data=file,ncol=720,nrow=360) but I got an error : Error in mean(file2[X, Y], na.rm = TRUE) : subscript out of bounds. and then I rewrote the above line as :file2-matrix(data=file,ncol=360,nrow=720.I put ncol=360 and nrows =720 which is not right.But that worked and I didn't get any error.however,the results were not correct. Any help please dir1- list.files(C:\\Users\\aalyaari\\Desktop\\New folder (10)\\Climate_Rad_f_GAMMA_%d.img, full.names = TRUE) listfile-dir() for (i in c(1:365)) { conne - file(listfile[i], rb) file- readBin(conne, double(), size=4, n=720*360, signed=T) file2-matrix(data=file,ncol=720,nrow=360) extract[i]-mean(file2[X,Y],na.rm=TRUE) close(conne) write.table(extract,C:\\Users\\aalyaari\\Desktop\\New folder (10)\\sam.txt)} -- View this message in context: http://r.789695.n4.nabble.com/R-does-not-recognise-columns-and-rows-as-they-are-supposed-to-be-tp4631217.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R does not recognise columns and rows as they are supposed to be
Yes this helped a lot . I exactly followed what you suggested: X-(82:92) ; Y-(364:369) # for sellected region extract - double(365) setwd(C:\\Users\\aalyaari\\Desktop\\New folder (10)\\) listfile-dir() for (i in 1:365) { + conne - file(listfile[i], rb) + file1- readBin(conne, double(), n=360*720) + file2-matrix(data=file1,ncol=720,nrow=360) + extract[i]-mean(file2[X,Y],na.rm=TRUE) + close(conne) + write.table(extract,C:\\Users\\aalyaari\\Desktop\\New folder (10)\\samregion1.txt)} But I wonder why I got all values like -3.75E+306 -1.30E+54 -1.22E+58 and the right ones should be like: 22.25 22.76 33.25 -- View this message in context: http://r.789695.n4.nabble.com/R-does-not-recognise-columns-and-rows-as-they-are-supposed-to-be-tp4631217p4631241.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Replacing cretin value in a file
Dear All, I am trying to replace a value of 528.8933 to - in my file t- file(C:\\Users\\Amin\\Desktop\\1999n_Resample11.img, rb) e=readBin(t, double(), size=4,n=720*360, signed=TRUE) e[e != -] - e[e != -]*0.0099 + 477.65 -273.15 ##worked well and values heve been calculated e[e == 528.8933] - -## no changes made to 528.8933 any suggestions please -- View this message in context: http://r.789695.n4.nabble.com/Replacing-cretin-value-in-a-file-tp4629840.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replacing cretin value in a file
Dear Jim, You may forget to add your suggestions because what I see is just my post -- View this message in context: http://r.789695.n4.nabble.com/Replacing-cretin-value-in-a-file-tp4629840p4629843.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Replacing cretin value in a file
Dear All, I am trying to replace a value of 528.8933 to - in my file t- file(C:\\Users\\Amin\\Desktop\\1999n_Resample11.img, rb) e=readBin(t, double(), size=4,n=720*360, signed=TRUE) e[e != -] - e[e != -]*0.0099 + 477.65 -273.15 This code given above would read a binary file . Then It would do some clculations to e. That worked well. I opend the file again in order to look at the resultsand I found weird numbers ( 528.8933). I then used this line e[e == 528.8933] - -to replace the value of 528.8933 by -. I got no errors but when I looked again at the values, I found them as they were(528.8933 is still there) any idea on how to replace a value by another value?Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Replacing-cretin-value-in-a-file-tp4629840p4629846.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
Hi again, Sorry for connecting you again(I am still getting this error:subscripts out of bounds). the problem is that I am trying to apply R script for one file to many files. It succeeded to do that for the first file but not for the others.I think the problem in my code is that ( d ) (in the two last lines)is not correct. I think I shall write another loop for writing the results but didn't know how to do that. Merci d'avance library(Matrix) listfile- list.files(C:\\Users\\aalyaari\\Desktop\\New folder 7, *.bin, full.names = TRUE) long - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\inra.bin, rb) A=readBin(long, integer(), size=2,n=67420*1, signed=F) ta-t(A) close(long) lot - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\lat.img, rb) B=readBin(lot, integer(), size=2,n=67420*1, signed=F) tb-t(B) close(lot) for(h in length(listfile)) {b=file.info(listfile[h])$size/67420/4 wind - file(listfile[h]), rb) C=readBin(wind, double(), size=4 ,n=b*67420, signed=TRUE) close(wind) D-matrix(C,nrow=b,ncol=67420) for(d in 1:b) {M - Matrix(-, 360, 720) tm-t(M) for(i in 1:67420) {tm[ta[i],tb[i]]= round(10 * mean(D[(d-1)*8 + 1:8), i]) } to.write - sprintf(C:\\Users\\aalyaari\\Desktop\\New folder (6)\\Yar_%00d.bin, d) writeBin(as.integer(tm@x), size=2,to.write) } } -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4598289.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
Yes, But what is your opinion about d in sprintf(C:\\Users\\aalyaari\\Desktop\\New folder (6)\\Yar_%00d.bin, d), because will go from one to the number of rows(some files have 248 and others have 224) and my results will change accordingly. for example i will get from the first file 31 files while from the second i will get 28.at the end I will get 365 files. -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4598441.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
This code bellow will calculate average daily wind speed(measurements are taken every three hours). library(Matrix) setwd(C:\\Users\\aalyaari\\Desktop\\img) listfile-dir() long - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\inra.bin, rb) A=readBin(long, integer(), size=2,n=67420*1, signed=F) ta-t(A) lot - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\lat.img, rb) B=readBin(lot, integer(), size=2,n=67420*1, signed=F) tb-t(B) for (n in 1:length(listfile)) { #h[n]=listfile[n] h=listfile[n] #b[n]=file.info(h[n])$size/67420/4 b=file.info(h[n])$size/67420/4 wind - file(h, rb) C=readBin(wind, double(), size=4,n=67420*b, signed=TRUE) D-matrix(C,nrow=b,ncol=67420) for(d in 1:b) { M - Matrix(-, 360, 720) tm-t(M) for(i in 1:67420) { tm[ta[i],tb[i]]= round(10 * mean(D[(d-1)*8 + 1:8), i]) } to.write - sprintf(C:\\Users\\aalyaari\\Desktop\\New folder (6)\\Yar_%00d.bin, d) writeBin(as.integer(tm@x), size=2,to.write) } } the values are measured in kelvin so i want to convert them to degree,therefor I want to subtract all values by 273.15 before talking the average or max but did not work.In other words, i want this: every value is subtracted by 273.15 first ,then do other calculations(Min, Max)Can anyone tell me what is wrong.Thanks tm[ta[i],tb[i]]= round(10 * mean(D[((d-1)*8 + 1:8)-273.15), i])) -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4594774.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
Dear David, I think you meant tm[ta[i],tb[i]]= round(10 * mean(D[((d-1)*8 + 1:8),i]-273.15)) not tm[ta[i],tb[i]]= round(10 * mean(D[((d-1)*8 + 1:8)-273.15), i]-273.15)) . As you can see in the code above.I want to save the results as integer(16bit) thats why I multiplied by 10(as factor),in order to keep one dice-mil . Because I will later divide by 10.for example if I got 25 so this means 2.5. Thats why I used round. -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4594931.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
Hello again, I am still having the same problem with the main loop.it succeeded to finish reading and writing from the first file in my listfile but failed when it moves to the second file giving this error:Error: subscript out of bounds library(Matrix) setwd(C:\\Users\\aalyaari\\Desktop\\img) listfile-dir() long - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\inra.bin, rb) A=readBin(long, integer(), size=2,n=67420*1, signed=F) ta-t(A) lot - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\lat.img, rb) B=readBin(lot, integer(), size=2,n=67420*1, signed=F) tb-t(B) for (n in 1:length(listfile)) { h=listfile[n] b=file.info(h)$size/67420/4 wind - file(h, rb) C=readBin(wind, double(), size=4,n=67420*b, signed=TRUE) D-matrix(C,nrow=b,ncol=67420) for(d in 1:b) { M - Matrix(-, 360, 720) tm-t(M) for(i in 1:67420) { tm[ta[i],tb[i]]= round(10 * mean(D[(d-1)*8 + 1:8), i]) } to.write - sprintf(C:\\Users\\aalyaari\\Desktop\\New folder (6)\\Yar_%00d.bin, d) writeBin(as.integer(tm@x), size=2,to.write) } } -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4594985.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
Yes ,It worked. So how can I insert it in my code? Please for(h in listfile){ + print(h) + print(file.info(h)$size) + } [1] Wind_WFD_200101.nc.img [1] 66880640 [1] Wind_WFD_200102.nc.img [1] 60408320 [1] Wind_WFD_200103.nc.img [1] 66880640 [1] Wind_WFD_200104.nc.img [1] 64723200 [1] Wind_WFD_200105.nc.img [1] 66880640 [1] Wind_WFD_200106.nc.img [1] 64723200 [1] Wind_WFD_200107.nc.img [1] 66880640 [1] Wind_WFD_200108.nc.img [1] 66880640 [1] Wind_WFD_200109.nc.img [1] 64723200 [1] Wind_WFD_200110.nc.img [1] 66880640 [1] Wind_WFD_200111.nc.img [1] 64723200 [1] Wind_WFD_200112.nc.img [1] 66880640 -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4595216.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
I know this,but i meant how can i insert them here: for(h in listfile){ print(h)### Shall i keep this as it is print(file.info(h)$size) ### Shall i keep this as it is } b=file.info(h)$size/67420/4 ### Shall I keep this as it is?? wind - file(h, rb) ### Shall i keep this as it is?? C=readBin(wind, double(), size=4,n=67420*b, signed=TRUE) D-matrix(C,nrow=b,ncol=67420) last question: will it read them one by one and do calculations or will it read them all at once because we didn't indices them -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4595262.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Min , Max
Hellow everyone, This code bellow will calculate average daily wind speed(measurements are taken every three hours).Any ideas how to take the Min and Max instead of average. library(Matrix) setwd(C:\\Users\\aalyaari\\Desktop\\img) listfile-dir() long - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\inra.bin, rb) A=readBin(long, integer(), size=2,n=67420*1, signed=F) ta-t(A) lot - file(C:\\Users\\aalyaari\\Desktop\\New folder (5)\\lat.img, rb) B=readBin(lot, integer(), size=2,n=67420*1, signed=F) tb-t(B) for (n in 1:length(listfile)) { #h[n]=listfile[n] h=listfile[n] #b[n]=file.info(h[n])$size/67420/4 b=file.info(h[n])$size/67420/4 wind - file(h, rb) C=readBin(wind, double(), size=4,n=67420*b, signed=TRUE) D-matrix(C,nrow=b,ncol=67420) for(d in 1:b) { M - Matrix(-, 360, 720) tm-t(M) for(i in 1:67420) { tm[ta[i],tb[i]]= round(10 * ((D[(d-1)*8+1,i] + D[(d-1)*8+2,i] +D[(d-1)*8+3,i] +D[(d-1)*8+4,i] +D[(d-1)*8+5,i] +D[(d-1)*8+6,i] +D[(d-1)*8+7,i] +D[(d-1)*8+8,i] ) / 8)) } to.write - sprintf(C:\\Users\\aalyaari\\Desktop\\New folder (6)\\Yar_%00d.bin, d) writeBin(as.integer(tm@x), size=2,to.write) } } -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593065.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
Dear Rui, I really appreciate your help. This is exactly what I was looking for.I know there is simpler way than mine but I couldn't figure it out.So, many many thanks. I wonder what is the difference between This:tm[ta[i],tb[i]]= round(mean(D[(d-1)*8 + 1:8, i])) and This: (Why not use the parameter 'digits'?) tm[ta[i],tb[i]]= round(mean(D[(d-1)*8 + 1:8, i]), 1) -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593159.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
The piece of R code given above is meant to perform calculations for many files(12) in one folder.But I am getting this error:Error: subscript out of bounds.It finished reading the first file(successfully) but the error arose when it started to read the next file. Any suggestions why?. I think the problem is in this first loop: for (n in 1:length(listfile)) { h=listfile[n] b=file.info(h[n])$size/67420/4 -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593279.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
Yes this what I meant. I think no need for this line h=listfile[[n]],if I can just put it directly in the other line to do the calculations for every file in my list.they are not really elements.they are files. what I need is just this:get the size of each file in my list then /67420/4,to get the number of rows and assign that to b . so now b will be used later as a loop. I tried this also: for (n in 1:length(listfile)).{ h=listfile[[n]] b=file.info(h[[n]])$size/67420/4 } then I got this :Error in h[[n]] : subscript out of bounds -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593326.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
this is what happened with me:that when I get to the second element h[n] becomes h[2] which doesn't exist (h[1] obviously did). Any suggestions -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593401.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Min , Max
this is what happened with me:that when I get to the second element h[n] becomes h[2] which doesn't exist (h[1] obviously did). Any suggestions -- View this message in context: http://r.789695.n4.nabble.com/Min-Max-tp4593065p4593402.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Finding a vector position using names rather than values
Dear R gurus, I have been struggling with this for a while and thought you might be able to give me some guidance. I have a data frame, on which I apply a row function. The result looks to me like a vector that retains the old row names. I then sort the vector and subsequently need to be able to identify the rank of certain values by searching for their row names. See this mock example: dframe - data.frame(Col1=c(10,20,30), Col2=c(2,4,6), Col3=c(5,10,7), row.names=c(R1,R2,R3)) row_median-apply(dframe,1,median) row_median.sorted - sort(row_median, decreasing=TRUE) row_median.sorted R2 R3 R1 10 7 5 I now want to know the rank of, say R3 in this vector. I know I can use match() to find this using the vector values, however I need to use the header names (eg. R3) rather than the value (7). Many thanks for your help, Pall __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Finding a vector position using names rather than values
Thanks, that's very helpful! Also, many thanks to Dimitris Rizopoulos and Robin Williams who both came up with good solutions to my problem. Pall -Original Message- From: Henrique Dallazuanna [mailto:[EMAIL PROTECTED] Sent: 12 September 2008 12:26 To: Jonsson, Pall Cc: r-help@r-project.org Subject: Re: [R] Finding a vector position using names rather than values Try this: order(row_median)[names(row_median) == R3] On Fri, Sep 12, 2008 at 5:22 AM, Jonsson, Pall [EMAIL PROTECTED] wrote: Dear R gurus, I have been struggling with this for a while and thought you might be able to give me some guidance. I have a data frame, on which I apply a row function. The result looks to me like a vector that retains the old row names. I then sort the vector and subsequently need to be able to identify the rank of certain values by searching for their row names. See this mock example: dframe - data.frame(Col1=c(10,20,30), Col2=c(2,4,6), Col3=c(5,10,7), row.names=c(R1,R2,R3)) row_median-apply(dframe,1,median) row_median.sorted - sort(row_median, decreasing=TRUE) row_median.sorted R2 R3 R1 10 7 5 I now want to know the rank of, say R3 in this vector. I know I can use match() to find this using the vector values, however I need to use the header names (eg. R3) rather than the value (7). Many thanks for your help, Pall __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.