[R] How can I make this nested loop faster?
Hello everybody, I have written a nested for-loop, but as length(uc) > 170,000, this would take VERY long. I have tried to use sapply or something but I cannot get it to work, I would be happy if someone could point out to write this more efficiently. Thank you all, Ludwig ergsens <- data.frame(budget = numeric(500)) uc <- unique(rftab$startCell) for(i in 1:500){ uniquerates <- rlnorm(n = length(uc), mean = -1.6, sd = 1.7) for(j in 1:length(uc)){ rftab$masskg[rftab$startCell == uc[j]] <- uniquerates[j] } ergsens$budget[i] <- sum(rftab$masskg, na.rm = TRUE)/1000 } - Dipl. Geogr. Ludwig Hilger Wiss. MA Lehrstuhl für Physische Geographie Katholische Universität Eichstätt-Ingolstadt Ostenstraße 18 85072 Eichstätt -- View this message in context: http://r.789695.n4.nabble.com/How-can-I-make-this-nested-loop-faster-tp4690209.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] create Geotiff
Hi Karren, not sure if this is a problem of the software you are using to view the image after writing? I would first check the color scaling of the image in this software. I would interpret the black background as "no data". regards, Ludwig Karren wrote > Hi > > I am trying to export a raster as a Geotiff using - > > writeRaster(grazedmasstot, paste(pad, "grazedmass_total.tif"), "GTiff", > overwrite=TRUE) - > > But the resulting image is incorrect, the image is tiny and shows up as a > white object with a black background. > > Does anyone have any suggestions how I can rectify this? > > Thanks - Dipl. Geogr. Ludwig Hilger Wiss. MA Lehrstuhl für Physische Geographie Katholische Universität Eichstätt-Ingolstadt Ostenstraße 18 85072 Eichstätt -- View this message in context: http://r.789695.n4.nabble.com/create-Geotiff-tp4680188p4680203.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 x-value of intersections of mixture models
Dear list, I am trying to decompose a distribution into 4 normal distributions using the function normalmixEM from package mixtools. This works well, I get four mus and sds. I am now wondering how to get the 3 x values where the distributions intersect. I have tried the following: > summary(gemixed) summary of normalmixEM object: comp 1comp 2comp 3comp 4 lambda 0.0327861 0.206095 0.642772 0.118347 mu 5.4289201 15.885116 33.519228 59.564096 sigma 2.6662273 5.681445 9.337467 10.464027 loglik at estimate: -22760961 f.ints <- function(x, mu1, mu2, sd1, sd2) { f1 <- dnorm(x, mean = mu1, sd = sd1) f2 <- dnorm(x, mean = mu2, sd = sd2) f <- abs(f1 - f2) xs[which.min(f)]} xintersections <- numeric() # for(i in 1:(length(gemixed$mu)-1)){ xs <- seq(from = gemixed$mu[i], to = gemixed$mu[i+1], by = 0.001) xinter <- f.ints(x = xs, mu1 = gemixed$mu[i], mu2 = gemixed$mu[i+1], sd1 = gemixed$sigma[i], sd2 = gemixed$sigma[i+1]) xintersections <- rbind(xintersections, xinter)} Unfortunately, the x values I get this way are not near the intersections of the curves as plotted in: plot( # takes an object of class mixEM and returns various graphical output for select mixture models x = gemixed, # An object of class mixEM whichplots = 2, density = TRUE) Maybe I get this all wrong and there is an easy way to get the intersection x-values? Any help would be very much appreciated! Thank you and best regards, Ludwig ## > sessionInfo() R version 2.15.2 (2012-10-26) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252 LC_NUMERIC=C LC_TIME=German_Germany.1252 attached base packages: [1] grDevices datasets splines graphics stats tcltk utils methods base other attached packages: [1] rgdal_0.8-8 mixtools_0.4.6segmented_0.2-9.4 MASS_7.3-22 boot_1.3-7mclust_4.1RSAGA_0.93-2.1plyr_1.7.1 shapefiles_0.6foreign_0.8-52gstat_1.0-14 spacetime_0.8-0 [13] xts_0.8-8 zoo_1.7-9 sp_1.0-9 svSocket_0.9-53 TinnR_1.0-5 R2HTML_2.2Hmisc_3.9-3 survival_2.36-14 loaded via a namespace (and not attached): [1] cluster_1.14.3 grid_2.15.2 lattice_0.20-10 svMisc_0.9-65 tools_2.15.2 ----- Dipl. Geogr. Ludwig Hilger Wiss. MA Lehrstuhl für Physische Geographie Katholische Universität Eichstätt-Ingolstadt Ostenstraße 18 85072 Eichstätt -- View this message in context: http://r.789695.n4.nabble.com/Finding-x-value-of-intersections-of-mixture-models-tp483.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] Calling a .bat to set environment variables and subsequent .exe execution from within R
Dear list, I have found a thread dealing with similar problems with a Mac, but somehow I cannot get it to work and the problem is slightly different: I am using the shell() command to execute a program from within R. The problem is that I need to execute SDKShell.bat file first that sets the environment variables for the program ogr2ogr.exe: shell(cmd = "C:\\GDAL_1.9.2_MAPSERVER_6.2.0\\SDKShell_original.bat") Setting environment for using the GDAL and MapServer tools. Hiding the OCI plugin library. D:\R_working_directory\Koordinatenproblem>> source(.trPaths[5], echo=TRUE, max.deparse.length=150) shell( cmd="ogr2ogr -s_srs EPSG:31254 -t_srs EPSG:25832 D:\\R_working_directory\\Koordinatenproblem\\Fliessgewaesser_25832.shp D:\\R_working_directory\\Koordinatenproblem\\Fliessgewaesser.shp", invisible = FALSE, intern = TRUE ) shell(cmd="ogr2ogr -s_srs EPSG:31254 -t_srs EPSG:25832 D:\\R_working_directory\\Koordinatenproblem\\Fliessgewaesser_25832.shp D:\\R_working_director ..." ... [TRUNCATED] [1] "Der Befehl \"ogr2ogr\" ist entweder falsch geschrieben oder" "konnte nicht gefunden werden." attr(,"status") [1] 1 Warnmeldung: Ausführung von Kommando 'C:\Windows\system32\cmd.exe /c ogr2ogr -s_srs EPSG:31254 -t_srs EPSG:25832 D:\R_working_directory\Koordinatenproblem\Fliessgewaesser_25832.shp D:\R_working_directory\Koordinatenproblem\Fliessgewaesser.shp' ergab Status 1 The problem is that he cmd seems to "forget" the result of the command before, i.e. the cmd is shut between different calls by shell()!? As a result I cannot execute ogr2ogr.exe. Is there an argument or sth. to work in the same cmd-session using subsequent shell() executions from R? Thank you, best regards and have a nice Christmas, Ludwig ps: I have also tried to rewrite the .bat to set the environment variables permanently, but I did not manage that either. - Dipl. Geogr. Ludwig Hilger Wiss. MA Lehrstuhl für Physische Geographie Katholische Universität Eichstätt-Ingolstadt Ostenstraße 18 85072 Eichstätt -- View this message in context: http://r.789695.n4.nabble.com/Calling-a-bat-to-set-environment-variables-and-subsequent-exe-execution-from-within-R-tp4653814.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.