Re: [R] [FORGED] intersection of two polygons which are not shapefiles
Thanks! That's just what I was looking for! A On Mon, Jul 18, 2016 at 5:56 PM, Rolf Turner <r.tur...@auckland.ac.nz> wrote: > On 19/07/16 01:16, Adrienne Wootten wrote: > >> All, >> >> Greetings! I hope things are going well for all! I apologize if someone's >> already answered this and I'm just not finding it. Here's what I'd like >> to >> do, but I'm hitting a brick wall with it. >> >> I have two sets of points for which I've already determined which ones >> points for the boundaries with the chull function. What I need for what >> I'm doing is the coordinates where the two resulting polygons overlap. >> These are not raster grids, nor shapefiles. What I have to work with are >> the two data frames with the points at the vertices of each polygon >> (included below). >> >> chx1 >>> >> x y >> 1 0.5822569 -0.878 >> 2 0.5338428 -0.5883604 >> 3 -0.3442943 -0.6701115 >> 4 -0.7409293 0.2286962 >> 5 0.2147221 0.8061485 >> 6 0.4914146 0.4941865 >> 7 0.5822569 -0.878 >> >> chx2 >>> >> x y >> 1 0.7163506 -0.4357497 >> 2 0.6513128 -0.5395180 >> 3 0.1818315 -0.6317423 >> 4 -0.6394281 -0.5765610 >> 5 -0.6044681 0.1831627 >> 6 -0.5799485 0.3231473 >> 7 0.2248476 0.9601908 >> 8 0.7163506 -0.4357497 >> >> >> If anyone has any ideas on how to get what I'm after I'd appreciate it! >> I've tried a lot of things from the raster, rgeos, and more. Knowing me >> it's something obvious I'm just not seeing right now. >> > > You can do this very easily using the spatstat package: > > library(spatstat) > X1 <- read.table(textConnection(" > x y > 1 0.5822569 -0.878 > 2 0.5338428 -0.5883604 > 3 -0.3442943 -0.6701115 > 4 -0.7409293 0.2286962 > 5 0.2147221 0.8061485 > 6 0.4914146 0.4941865 > 7 0.5822569 -0.878")) > > X2 <- read.table(textConnection(" >x y > 1 0.7163506 -0.4357497 > 2 0.6513128 -0.5395180 > 3 0.1818315 -0.6317423 > 4 -0.6394281 -0.5765610 > 5 -0.6044681 0.1831627 > 6 -0.5799485 0.3231473 > 7 0.2248476 0.9601908 > 8 0.7163506 -0.4357497")) > > X1 <- reverse.xypolygon(X1) # Vertices are in the wrong > X2 <- reverse.xypolygon(X2) # (clockwise) order. > W1 <- owin(poly=X1) > W2 <- owin(poly=X2) > WI <- intersect.owin(W1,W2) > > plot(union.owin(W1,W2),col="blue",main="") > plot(WI,add=TRUE,col="red") > > HTH > > cheers, > > Rolf Turner > > P.S. To extract the coordinates of the intersection polygon you can do: > > WI$bdry[[1]] > > R. T. > > -- > Technical Editor ANZJS > Department of Statistics > University of Auckland > Phone: +64-9-373-7599 ext. 88276 > -- Adrienne Wootten Ph.D Candidate / Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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] intersection of two polygons which are not shapefiles
All, Greetings! I hope things are going well for all! I apologize if someone's already answered this and I'm just not finding it. Here's what I'd like to do, but I'm hitting a brick wall with it. I have two sets of points for which I've already determined which ones points for the boundaries with the chull function. What I need for what I'm doing is the coordinates where the two resulting polygons overlap. These are not raster grids, nor shapefiles. What I have to work with are the two data frames with the points at the vertices of each polygon (included below). > chx1 x y 1 0.5822569 -0.878 2 0.5338428 -0.5883604 3 -0.3442943 -0.6701115 4 -0.7409293 0.2286962 5 0.2147221 0.8061485 6 0.4914146 0.4941865 7 0.5822569 -0.878 > chx2 x y 1 0.7163506 -0.4357497 2 0.6513128 -0.5395180 3 0.1818315 -0.6317423 4 -0.6394281 -0.5765610 5 -0.6044681 0.1831627 6 -0.5799485 0.3231473 7 0.2248476 0.9601908 8 0.7163506 -0.4357497 If anyone has any ideas on how to get what I'm after I'd appreciate it! I've tried a lot of things from the raster, rgeos, and more. Knowing me it's something obvious I'm just not seeing right now. Thanks all! Adrienne -- Adrienne Wootten Ph.D Candidate / Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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.
Re: [R] projectRaster function no values
Ben, Thanks so for much for the heads up on R-sig-geo (totally forgot about that one myself). Thank you also for the clue, it gave me a poke in the right direction to figure it out. I'll share with the group just in case someone else runs across this. It was actually something off with the resolution and extent in the source data. > t=1 > testvar2 = raster("SE/test.nc",band=t,varname="TAMAX") # pulling first time slice of my netcdf > testvar2 class : RasterLayer band: 1 (of 4 bands) dimensions : 229, 234, 53586 (nrow, ncol, ncell) resolution : 15, 15 (x, y) extent : -1747.5, 1762.5, -1710, 1725 (xmin, xmax, ymin, ymax) coord. ref. : +proj=lcc +lon_0=-77 +lat_0=38 +lat_1=30 +lat_2=60 +ellps=WGS84 data source : /projdata/dcerp/DSdata/regcmdata/SE/test.nc names : Avg.Max.Aneom.Temperature z-value : 1960-01-01 zvar: TAMAX The CRS in the file was giving the resolution as 15, but in reality this is a 15km resolution dataset and should cover an area from 20N to 50N and about 100W to 50W. R was interpreting the resolution as 15m, which caused it to have the small extent, instead of what it should have been, which is covering Eastern North America. I had to mess with the resolution and extent a bit, but once I did it worked beautifully with projectRaster. Thanks again! Adrienne -- Adrienne Wootten Ph.D Candidate / Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University On Wed, May 18, 2016 at 9:05 AM, Ben Tupper <btup...@bigelow.org> wrote: > Hi Adrienne, > > You'll always get great help for these kinds of questions if you subscribe > and post to the R-SIG-Geo mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-geo I highly recommend that > you join! > > I have encountered this before, and usually it is because I have > mistakenly assumed that source and destination data are roughly > coincident. I'm not sure if that is true in your case. I have tried to > replicate your steps. I transformed the coordinates of your source and > destination rasters into SpatialPoints objects, and then I reprojected your > source coordinates to the projection of your destination coordinates. > Unless I have messed up a step, you can see in the plot generated that > there is a significant difference in the extent of the source and > destination rasters. Perhaps your destination coordinates are amiss? > > Cheers, > Ben > > > ### Start > library(sp) > library(raster) > > nc <- 234 > nr <- 229 > m <- matrix(runif(nc*nr), ncol = nc, nrow = nr) > r1 <- raster(m, xmn = -1747.5, xmx = 1762.5, ymn = -1710, ymx = 1725, > crs = CRS('+proj=lcc +lon_0=-77 +lat_0=38 +lat_1=30 +lat_2=60')) > > newlon <- c(-102.97288, -74.05399) > newlat <- c(25.13511, 40.27023) > newnr <- 113 > newnc <- 215 > template <- raster(nrows = newnr, ncol = newnc, > xmn = newlon[1], xmx = newlon[2], > ymn = newlat[1], ymx = newlat[2], > crs = CRS("+proj=longlat +datum=WGS84")) > > r2 <- setValues(template, runif(ncell(template))) > > xy_r1 <- SpatialPoints(coordinates(r1), > proj4string = CRS('+proj=lcc +lon_0=-77 +lat_0=38 +lat_1=30 > +lat_2=60')) > xy_r1_tr <- spTransform(xy_r1, CRS("+proj=longlat +datum=WGS84")) > > > xy_r2 <- SpatialPoints(coordinates(r2), > proj4string = CRS("+proj=longlat +datum=WGS84")) > > plot(xy_r2, pch = '.', axes = TRUE) > points(xy_r1_tr, pch = 1, col = 'orange') > > ### END > > > On May 17, 2016, at 1:02 PM, Adrienne Wootten <amwoo...@ncsu.edu> wrote: > > > > All, > > > > Greetings! Any help with this problem is appreciated! > > > > I'm working to get a netcdf file that has a Lambert Conformal Conic > > projection into geographic, but also a smaller area. Here's the issue > I'm > > having - essentially it looks like projectRaster is working, but the > > resulting raster has no values. > > > > The data itself is massive so I can't include that, but here's what's > going > > on. > > > > > >> testvar2 = raster("SE/test.nc",band=t,varname="TAMAX") # pulling first > > time slice of my netcdf > >> testvar2 > > > > class : RasterLayer > > band: 1 (of 4 bands) > > dimensions : 229, 234, 53586 (nrow, ncol, ncell) > > resolution : 15, 15 (x, y) > > extent : -1747.5, 1762.5, -1710, 1725 (xmin, xmax, ymin, ymax) > > coord. ref. : +proj=lcc +lon_0=-77 +lat_0=38 +lat_1=30 +lat_2=60 > > +ellps=WGS84 > > data source : /projdata/dcerp/DSdata/regcmdata/SE/test.nc > > name
[R] projectRaster function no values
74.18912, -74.05399) > extent(newlocs) = c(min(newlon),max(newlon),min(newlat),max(newlat)) # dummy raster put to the right extent > testproj2 = projectRaster(from=testvar2,to=newlocs,method="bilinear") # project raster itself > testproj2 # literally no values. class : RasterLayer dimensions : 113, 215, 24295 (nrow, ncol, ncell) resolution : 0.1345065, 0.1339391 (x, y) extent : -102.9729, -74.05399, 25.13511, 40.27023 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 data source : in memory names : Avg.Max.Aneom.Temperature values : NA, NA (min, max) I'm quite perplexed with this one, I feel like I'm doing everything right so I'm not sure what's failing. The R version is R 3.2.3 in a Linux/Unix environment. Many thanks for your help! Adrienne -- Adrienne Wootten Ph.D Candidate / Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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.
Re: [R] legend for vectorplot in rasterVis
All, Thanks anyway folks, but I'm going to call myself a bonehead and move on now that I've found it. The key.arrow argument in vectorplot will do what I need to make a scale legend. Thanks all! Adrienne On Tue, Mar 1, 2016 at 9:14 AM, Adrienne Wootten <amwoo...@ncsu.edu> wrote: > Jim, > > Thanks! Interestingly when working with lengthKey it gives me an error > that plot.new hasn't been called after the plot has been created with > vectorplot. Really bizarre to me. > > A > > On Tue, Mar 1, 2016 at 5:22 AM, Jim Lemon <drjimle...@gmail.com> wrote: > >> Hi Adrienne, >> I'm not sure if this will help, but lengthKey in the plotrix package >> will display a scale showing the relationship of vector length to >> whatever numeric value is being displayed. However, you do have to >> sort of the scaling manually. >> >> Jim >> >> >> On Tue, Mar 1, 2016 at 7:30 AM, Adrienne Wootten <amwoo...@ncsu.edu> >> wrote: >> > All, >> > >> > Your help with this is greatly appreciated! >> > >> > I'm working with the vectorplot function in rasterVis to produce wind >> > vector maps (pretty much like the code in here - >> > https://rpubs.com/alobo/vectorplot), but I was wondering about a >> legend. >> > >> > The vectors that vectorplot produces are wonderful. What I'm trying to >> do >> > is get a scale legend to go with it. In other words, have a vector >> outside >> > the main plot which provides a point of reference for the vectors in the >> > plot. Something alike to what GrADS does (for those familiar), is what >> I'm >> > hoping to get to (for an example, the first chart on this page >> > http://www.wishingwork.com/grads/graphics-controls/vector-graphics.html >> ) >> > >> > I haven't seen this done yet with R, at least not from what I could find >> > with all the forums. If anyone has an idea on how to do this, I >> > tremendously appreciate it! >> > >> > Thanks all! >> > >> > Adrienne >> > >> > -- >> > Adrienne Wootten >> > Ph.D Candidate / Graduate Research Assistant >> > State Climate Office of North Carolina >> > Department of Marine, Earth and Atmospheric Sciences >> > North Carolina State University >> > >> > [[alternative HTML version deleted]] >> > >> > __ >> > 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. >> > > > > -- > Adrienne Wootten > Ph.D Candidate / Graduate Research Assistant > State Climate Office of North Carolina > Department of Marine, Earth and Atmospheric Sciences > North Carolina State University > -- Adrienne Wootten Ph.D Candidate / Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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.
Re: [R] legend for vectorplot in rasterVis
Jim, Thanks! Interestingly when working with lengthKey it gives me an error that plot.new hasn't been called after the plot has been created with vectorplot. Really bizarre to me. A On Tue, Mar 1, 2016 at 5:22 AM, Jim Lemon <drjimle...@gmail.com> wrote: > Hi Adrienne, > I'm not sure if this will help, but lengthKey in the plotrix package > will display a scale showing the relationship of vector length to > whatever numeric value is being displayed. However, you do have to > sort of the scaling manually. > > Jim > > > On Tue, Mar 1, 2016 at 7:30 AM, Adrienne Wootten <amwoo...@ncsu.edu> > wrote: > > All, > > > > Your help with this is greatly appreciated! > > > > I'm working with the vectorplot function in rasterVis to produce wind > > vector maps (pretty much like the code in here - > > https://rpubs.com/alobo/vectorplot), but I was wondering about a legend. > > > > The vectors that vectorplot produces are wonderful. What I'm trying to > do > > is get a scale legend to go with it. In other words, have a vector > outside > > the main plot which provides a point of reference for the vectors in the > > plot. Something alike to what GrADS does (for those familiar), is what > I'm > > hoping to get to (for an example, the first chart on this page > > http://www.wishingwork.com/grads/graphics-controls/vector-graphics.html) > > > > I haven't seen this done yet with R, at least not from what I could find > > with all the forums. If anyone has an idea on how to do this, I > > tremendously appreciate it! > > > > Thanks all! > > > > Adrienne > > > > -- > > Adrienne Wootten > > Ph.D Candidate / Graduate Research Assistant > > State Climate Office of North Carolina > > Department of Marine, Earth and Atmospheric Sciences > > North Carolina State University > > > > [[alternative HTML version deleted]] > > > > __ > > 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. > -- Adrienne Wootten Ph.D Candidate / Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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] legend for vectorplot in rasterVis
All, Your help with this is greatly appreciated! I'm working with the vectorplot function in rasterVis to produce wind vector maps (pretty much like the code in here - https://rpubs.com/alobo/vectorplot), but I was wondering about a legend. The vectors that vectorplot produces are wonderful. What I'm trying to do is get a scale legend to go with it. In other words, have a vector outside the main plot which provides a point of reference for the vectors in the plot. Something alike to what GrADS does (for those familiar), is what I'm hoping to get to (for an example, the first chart on this page http://www.wishingwork.com/grads/graphics-controls/vector-graphics.html) I haven't seen this done yet with R, at least not from what I could find with all the forums. If anyone has an idea on how to do this, I tremendously appreciate it! Thanks all! Adrienne -- Adrienne Wootten Ph.D Candidate / Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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.
Re: [R] apply regression to an array
Almost forgot that function lmfunc is this: lmfunc = function(valist,input){ fitted.values(lm(valist~input)) } A On Tue, Oct 6, 2015 at 2:41 PM, Adrienne Wootten <amwoo...@ncsu.edu> wrote: > FYI I did try something like this: > > test = apply(test3,c(1,2),lmfunc,input=t) > > but that gives me an array that is 10 rows by 5 columns by 5 slices, and I > need it to keep the same dimensions as test3 (5x5x10) > > A > > On Tue, Oct 6, 2015 at 1:42 PM, Adrienne Wootten <amwoo...@ncsu.edu> > wrote: > >> R-Helpers, >> >> I've seen some similar threads about this question online, but not quite >> what I'm looking for. I apologize in advance if someone's already answered >> this and I just can't find it online. >> >> Say that I have an array like test3 in the little example code I have >> below: >> >> test1 = array(rep(1:10,each = 25),dim=c(5,5,10)) >> test2 = array(rnorm(250,0,0.35),dim=c(5,5,10)) >> test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices >> >> time=1:10 >> >> Where the dimensions are x, y, and time. What I'd like to do is run a >> regression (for the sake of this example, say lm) on each x,y in time. So >> for a single cell the formula might be test3[1,1,]~time, but I'd like to >> that for all cells. The only way I can immediately think of is to use a >> loop, but I'm wondering if there's a way to do this without a loop. >> Perhaps with tapply? >> >> I'm actually doing a fourth order regression with a much larger array, >> but this simple example illustrates the question I have. >> >> Many thanks for the help! Sorry if someone's already answered this and I >> can't find it. >> >> Adrienne >> >> -- >> Adrienne Wootten >> Graduate Research Assistant >> State Climate Office of North Carolina >> Department of Marine, Earth and Atmospheric Sciences >> North Carolina State University >> > > > > -- > Adrienne Wootten > Graduate Research Assistant > State Climate Office of North Carolina > Department of Marine, Earth and Atmospheric Sciences > North Carolina State University > -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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.
Re: [R] apply regression to an array
FYI I did try something like this: test = apply(test3,c(1,2),lmfunc,input=t) but that gives me an array that is 10 rows by 5 columns by 5 slices, and I need it to keep the same dimensions as test3 (5x5x10) A On Tue, Oct 6, 2015 at 1:42 PM, Adrienne Wootten <amwoo...@ncsu.edu> wrote: > R-Helpers, > > I've seen some similar threads about this question online, but not quite > what I'm looking for. I apologize in advance if someone's already answered > this and I just can't find it online. > > Say that I have an array like test3 in the little example code I have > below: > > test1 = array(rep(1:10,each = 25),dim=c(5,5,10)) > test2 = array(rnorm(250,0,0.35),dim=c(5,5,10)) > test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices > > time=1:10 > > Where the dimensions are x, y, and time. What I'd like to do is run a > regression (for the sake of this example, say lm) on each x,y in time. So > for a single cell the formula might be test3[1,1,]~time, but I'd like to > that for all cells. The only way I can immediately think of is to use a > loop, but I'm wondering if there's a way to do this without a loop. > Perhaps with tapply? > > I'm actually doing a fourth order regression with a much larger array, but > this simple example illustrates the question I have. > > Many thanks for the help! Sorry if someone's already answered this and I > can't find it. > > Adrienne > > -- > Adrienne Wootten > Graduate Research Assistant > State Climate Office of North Carolina > Department of Marine, Earth and Atmospheric Sciences > North Carolina State University > -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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.
Re: [R] apply regression to an array
Bill, Thanks a bunch that works great! A On Tue, Oct 6, 2015 at 2:56 PM, William Dunlap <wdun...@tibco.com> wrote: > Since the model matrix, cbind(1,time) is the same for all your > response variables, > you can calculate this on one call to lm, but you have to rearrange the > response > values so that each x,y set is in one column. I think the following > function does it: > > f <- function (time, y) > { > stopifnot(length(dim(y)) == 3, dim(y)[3] == length(time)) > yMatrix <- matrix(aperm(y, c(3, 1, 2)), dim(y)[3]) > fit <- lm(yMatrix ~ time) > aperm(array(fitted.values(fit), dim(y)[c(3, 1, 2)]), c(2, > 3, 1)) > } > > E.g., > > fitted.values(lm(test1[2,5,]~time)) > 1 2 3 4 5 6 7 8 9 10 > 1 2 3 4 5 6 7 8 9 10 > > f(time, test1)[2,5,] > [1] 1 2 3 4 5 6 7 8 9 10 > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > > On Tue, Oct 6, 2015 at 10:42 AM, Adrienne Wootten <amwoo...@ncsu.edu> > wrote: > > R-Helpers, > > > > I've seen some similar threads about this question online, but not quite > > what I'm looking for. I apologize in advance if someone's already > answered > > this and I just can't find it online. > > > > Say that I have an array like test3 in the little example code I have > below: > > > > test1 = array(rep(1:10,each = 25),dim=c(5,5,10)) > > test2 = array(rnorm(250,0,0.35),dim=c(5,5,10)) > > test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices > > > > time=1:10 > > > > Where the dimensions are x, y, and time. What I'd like to do is run a > > regression (for the sake of this example, say lm) on each x,y in time. > So > > for a single cell the formula might be test3[1,1,]~time, but I'd like to > > that for all cells. The only way I can immediately think of is to use a > > loop, but I'm wondering if there's a way to do this without a loop. > > Perhaps with tapply? > > > > I'm actually doing a fourth order regression with a much larger array, > but > > this simple example illustrates the question I have. > > > > Many thanks for the help! Sorry if someone's already answered this and I > > can't find it. > > > > Adrienne > > > > -- > > Adrienne Wootten > > Graduate Research Assistant > > State Climate Office of North Carolina > > Department of Marine, Earth and Atmospheric Sciences > > North Carolina State University > > > > [[alternative HTML version deleted]] > > > > __ > > 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. > -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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] apply regression to an array
R-Helpers, I've seen some similar threads about this question online, but not quite what I'm looking for. I apologize in advance if someone's already answered this and I just can't find it online. Say that I have an array like test3 in the little example code I have below: test1 = array(rep(1:10,each = 25),dim=c(5,5,10)) test2 = array(rnorm(250,0,0.35),dim=c(5,5,10)) test3 = test1+test2 # array with 5 rows, 5 columns, 10 slices time=1:10 Where the dimensions are x, y, and time. What I'd like to do is run a regression (for the sake of this example, say lm) on each x,y in time. So for a single cell the formula might be test3[1,1,]~time, but I'd like to that for all cells. The only way I can immediately think of is to use a loop, but I'm wondering if there's a way to do this without a loop. Perhaps with tapply? I'm actually doing a fourth order regression with a much larger array, but this simple example illustrates the question I have. Many thanks for the help! Sorry if someone's already answered this and I can't find it. Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[alternative HTML version deleted]] __ 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] big matrix reading and writing
All, Got a tricky situation and unfortunately because it's a big file I can't exactly provide an example, so I'll describe this as best I can for everyone. I have a distance matrix that we are using for a modeling calculation in space for multiple days. Since the matrix is never going to change for different dates, I want to keep the matrix in a file and refer to that so I don't have to repeat the calculation over and over again for that. The problem is it's a 32000 X 32000 matrix and roughly works out to 15GB of storage. This makes it a trick to read the file back into R, but it leaves me with two questions for the group. Is there anyway to have R write this out so that it takes up less space? I know R primarily treats numbers as doubles, but I'm trying to find a way to get R to write the values as floats or singles. with how big it is, it may not be wise to save it as an object in R when read in, so I'm wondering is there anyway to have R do the calculation it needs to do without saving the matrix as an object in R? Basically can I have it run the calculation off the file itself? Thanks! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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.
Re: [R] big matrix reading and writing
Jim At the moment I'm using write.table. I tried using write.matrix from the MASS package, but that failed. Integers are not appropriate here because we are working with fractions of miles for some locations and that needs to be retained. The range is from 0 to about 3.5 (it's a little less than that with the digits) I haven't tried the save function yet, but I wasn't aware of that one previously. Thanks for pointing that out. The bigger concern is reading and working with that dataset in the other calculation though. Adrienne On Thu, Jun 5, 2014 at 9:37 AM, jim holtman jholt...@gmail.com wrote: How are you writing it out now? Are you using 'save' which will compress the file? What are the range of numbers in the matrix? Can you scale them to integers (what is the range of the numbers) which might save some space? You did not provide enough information to make a definitive solution. Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Thu, Jun 5, 2014 at 9:26 AM, Adrienne Wootten amwoo...@ncsu.edu wrote: All, Got a tricky situation and unfortunately because it's a big file I can't exactly provide an example, so I'll describe this as best I can for everyone. I have a distance matrix that we are using for a modeling calculation in space for multiple days. Since the matrix is never going to change for different dates, I want to keep the matrix in a file and refer to that so I don't have to repeat the calculation over and over again for that. The problem is it's a 32000 X 32000 matrix and roughly works out to 15GB of storage. This makes it a trick to read the file back into R, but it leaves me with two questions for the group. Is there anyway to have R write this out so that it takes up less space? I know R primarily treats numbers as doubles, but I'm trying to find a way to get R to write the values as floats or singles. with how big it is, it may not be wise to save it as an object in R when read in, so I'm wondering is there anyway to have R do the calculation it needs to do without saving the matrix as an object in R? Basically can I have it run the calculation off the file itself? Thanks! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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.
Re: [R] big matrix reading and writing
Jim, There are not going to be additional copies of the distance matrix. The distance matrix is what is needed for a geographically weighted regression, so I can estimate the results from that to be a SpatialPointsDataFrame at roughly 3 rows by 7 columns. Much smaller size than the distance matrix, though I'm not sure given the class of that object. Potentially lots of room taken up, at the moment the machine in question has 32GB to work with, but we may be able to shift this onto machines with more RAM. A On Thu, Jun 5, 2014 at 10:00 AM, jim holtman jholt...@gmail.com wrote: The real question is how much memory does the machine that you are working on have. The 32000x32000 matrix will take up ~8GB of physical memory, so how much memory will the rest of your objects take up. Are any of them going to be copies of the distance matrix, or is it always going to be unchanged? Normally my rule of thumb is that I should have 3-4 times the largest object I am working with since I may be making copies of it. So it is important to understand how the rest of your program will be using this large matrix and what type of operations you will be doing on it. Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Thu, Jun 5, 2014 at 9:48 AM, Adrienne Wootten amwoo...@ncsu.edu wrote: Jim At the moment I'm using write.table. I tried using write.matrix from the MASS package, but that failed. Integers are not appropriate here because we are working with fractions of miles for some locations and that needs to be retained. The range is from 0 to about 3.5 (it's a little less than that with the digits) I haven't tried the save function yet, but I wasn't aware of that one previously. Thanks for pointing that out. The bigger concern is reading and working with that dataset in the other calculation though. Adrienne On Thu, Jun 5, 2014 at 9:37 AM, jim holtman jholt...@gmail.com wrote: How are you writing it out now? Are you using 'save' which will compress the file? What are the range of numbers in the matrix? Can you scale them to integers (what is the range of the numbers) which might save some space? You did not provide enough information to make a definitive solution. Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Thu, Jun 5, 2014 at 9:26 AM, Adrienne Wootten amwoo...@ncsu.edu wrote: All, Got a tricky situation and unfortunately because it's a big file I can't exactly provide an example, so I'll describe this as best I can for everyone. I have a distance matrix that we are using for a modeling calculation in space for multiple days. Since the matrix is never going to change for different dates, I want to keep the matrix in a file and refer to that so I don't have to repeat the calculation over and over again for that. The problem is it's a 32000 X 32000 matrix and roughly works out to 15GB of storage. This makes it a trick to read the file back into R, but it leaves me with two questions for the group. Is there anyway to have R write this out so that it takes up less space? I know R primarily treats numbers as doubles, but I'm trying to find a way to get R to write the values as floats or singles. with how big it is, it may not be wise to save it as an object in R when read in, so I'm wondering is there anyway to have R do the calculation it needs to do without saving the matrix as an object in R? Basically can I have it run the calculation off the file itself? Thanks! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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.
Re: [R] issues with krige function
My apologies to all, the package is gstat that I am using not geoR. thanks! A On Tue, Oct 30, 2012 at 1:35 PM, Adrienne Wootten amwoo...@ncsu.edu wrote: Greetings all, Ran into a strange problem with the krige function from geoR. The problem that I am having is that while the krige function seems to work well, the resulting predicted values are all NAs. Given the size of the datasets I am working with can't attach it, but I can provide snippets of the datasets. casedata station year month day obs mpebias type lat lon 140 319147 2011 8 28 0.00 0.0 0. COOP 35.48667 -82.96833 141 319354 2011 8 28 0.02 0.001305799 -0.01869420 COOP 34.25722 -78.68722 142 319357 2011 8 28 0.00 0.045194085 0.04519409 COOP 34.40944 -78.79139 143 319440 2011 8 28 6.92 5.530313974 -1.38968603 COOP 35.85278 -77.03056 144 319461 2011 8 28 0.98 0.949224103 -0.03077590 COOP 34.14250 -77.87861 145 319467 2011 8 28 0.30 1.492852601 1.19285260 COOP 34.32083 -77.92056 146 319476 2011 8 28 2.95 3.284362345 0.33436235 COOP 35.69389 -77.94556 147 319555 2011 8 28 0.00 0.0 0. COOP 36.13083 -81.22750 148 319667 2011 8 28 0.00 0.014472943 0.01447294 COOP 35.85583 -80.36083 149 319675 2011 8 28 0.00 0.0 0. COOP 36.13056 -80.54806 v.fit = vgm(phi=1.2914,model=Mat,range=14.1611,nugget=0.23,kappa=83.1047) krige.pred.out = krige(formula=bias~1,locations=~lon+lat,data=casedata,newdata=gridmeta,model=v.fit) [using ordinary kriging] krige.pred.out lon lat var1.pred var1.var 1 -86.28771 33.91335 NaN NaN 2 -86.27388 33.94722 NaN NaN 3 -86.26003 33.98111 NaN NaN 4 -86.24615 34.01500 NaN NaN 5 -86.23225 34.04890 NaN NaN 6 -86.21835 34.08280 NaN NaN 7 -86.20441 34.11672 NaN NaN 8 -86.19045 34.15064 NaN NaN 9 -86.17648 34.18457 NaN NaN 10 -86.16249 34.21851 NaN NaN summary(krige.pred.out) lon lat var1.predvar1.var Min. :-86.29 Min. :30.51 Min. : NA Min. : NA 1st Qu.:-82.38 1st Qu.:33.73 1st Qu.: NA 1st Qu.: NA Median :-79.88 Median :35.22 Median : NA Median : NA Mean :-79.91 Mean :35.26 Mean :NaN Mean :NaN 3rd Qu.:-77.48 3rd Qu.:36.76 3rd Qu.: NA 3rd Qu.: NA Max. :-73.16 Max. :40.16 Max. : NA Max. : NA NA's :44408 NA's :44408 Any ideas are greatly appreciated, I'm not getting any warnings or errors from krige, so this is perplexing to me. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] issues with krige function
Thierry, To answer your first question summary(casedata) station year monthday obs mpe Length:236 Min. :2011 Min. :8 Min. :28 Min. :0. Min. :0.0 Class :character 1st Qu.:2011 1st Qu.:8 1st Qu.:28 1st Qu.:0. 1st Qu.:0.0 Mode :character Median :2011 Median :8 Median :28 Median :0. Median :0.0 Mean :2011 Mean :8 Mean :28 Mean :0.6119 Mean :0.60163 3rd Qu.:2011 3rd Qu.:8 3rd Qu.:28 3rd Qu.:0.0225 3rd Qu.:0.03953 Max. :2011 Max. :8 Max. :28 Max. :9.3600 Max. :9.05766 bias typelat lon city Min. :-4.75583 Length:236 Min. :33.53 Min. :-84.84 Length:236 1st Qu.: 0.0 Class :character 1st Qu.:34.37 1st Qu.:-82.71 Class :character Median : 0.0 Mode :character Median :35.06 Median :-81.18 Mode :character Mean :-0.01028 Mean :35.18 Mean :-81.00 3rd Qu.: 0.03rd Qu.:35.99 3rd Qu.:-79.37 Max. : 2.82117 Max. :36.98 Max. :-76.33 statepreciptime Length:236 Length:236 Class :character Class :character Mode :character Mode :character summary(gridmeta) lon latxcell ycell row col Min. :-86.29 Min. :30.51 Min. :1895112 Min. :-6545105 Min. : 1.00 Min. : 1.0 1st Qu.:-82.38 1st Qu.:33.73 1st Qu.:2184464 1st Qu.:-6330770 1st Qu.: 61.75 1st Qu.: 46.0 Median :-79.88 Median :35.22 Median :2473816 Median :-6114053 Median :122.50 Median : 91.5 Mean :-79.91 Mean :35.26 Mean :2473816 Mean :-6114053 Mean :122.50 Mean : 91.5 3rd Qu.:-77.48 3rd Qu.:36.76 3rd Qu.:2763169 3rd Qu.:-5897337 3rd Qu.:183.25 3rd Qu.:137.0 Max. :-73.16 Max. :40.16 Max. :3052521 Max. :-5683002 Max. :244.00 Max. :182.0 To answer your second question, I'm not quite sure why, but apparently constraining nmax like that was exactly what it needed, because now it is producing values. v.fit = vgm(psill=1.2914,model=Mat,range=14.1611,nugget=0.23,kappa=83.1047) krige.pred.out = krige(formula=bias~1,locations=~lon+lat,data=casedata,newdata=gridmeta,model=v.fit) summary(krige.pred.out) lon lat var1.pred var1.var Min. :-86.29 Min. :30.51 Min. :-1.083 Min. :0.253 1st Qu.:-82.38 1st Qu.:33.73 1st Qu.:-0.044 1st Qu.:0.253 Median :-79.88 Median :35.22 Median : 0.000 Median :0.253 Mean :-79.91 Mean :35.26 Mean :-0.051 Mean :0.253 3rd Qu.:-77.48 3rd Qu.:36.76 3rd Qu.: 0.000 3rd Qu.:0.253 Max. :-73.16 Max. :40.16 Max. : 1.252 Max. :0.254 NA's :14925NA's :31483 Thanks for the help. Adrienne On Wed, Oct 31, 2012 at 10:28 AM, ONKELINX, Thierry thierry.onkel...@inbo.be wrote: Dear Adrienne, What is the output of summary(casestudy) and summary(gridmeta)? What happens if you set nmax to 10? krige(formula=bias~1,locations=~lon+lat,data=casedata,newdata=gridmeta ,model=v.fit, nmax = 10) ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest team Biometrie Kwaliteitszorg / team Biometrics Quality Assurance Kliniekstraat 25 1070 Anderlecht Belgium + 32 2 525 02 51 + 32 54 43 61 85 thierry.onkel...@inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -Oorspronkelijk bericht- Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens Adrienne Wootten Verzonden: dinsdag 30 oktober 2012 18:36 Aan: r-help@r-project.org Onderwerp: [R] issues with krige function Greetings all, Ran into a strange problem with the krige function from geoR. The problem that I am having is that while the krige function seems to work well, the resulting predicted values are all NAs. Given the size of the datasets I am working with can't attach it, but I can provide snippets of the datasets. casedata station year month day obs mpebias type lat lon 140 319147 2011 8 28 0.00 0.0 0. COOP 35.48667 -82.96833 141 319354 2011 8 28 0.02 0.001305799 -0.01869420 COOP 34.25722 -78.68722 142 319357 2011 8 28 0.00 0.045194085 0.04519409 COOP 34.40944 -78.79139 143 319440 2011 8 28 6.92 5.530313974 -1.38968603 COOP 35.85278
[R] issues with krige function
Greetings all, Ran into a strange problem with the krige function from geoR. The problem that I am having is that while the krige function seems to work well, the resulting predicted values are all NAs. Given the size of the datasets I am working with can't attach it, but I can provide snippets of the datasets. casedata station year month day obs mpebias type lat lon 140 319147 2011 8 28 0.00 0.0 0. COOP 35.48667 -82.96833 141 319354 2011 8 28 0.02 0.001305799 -0.01869420 COOP 34.25722 -78.68722 142 319357 2011 8 28 0.00 0.045194085 0.04519409 COOP 34.40944 -78.79139 143 319440 2011 8 28 6.92 5.530313974 -1.38968603 COOP 35.85278 -77.03056 144 319461 2011 8 28 0.98 0.949224103 -0.03077590 COOP 34.14250 -77.87861 145 319467 2011 8 28 0.30 1.492852601 1.19285260 COOP 34.32083 -77.92056 146 319476 2011 8 28 2.95 3.284362345 0.33436235 COOP 35.69389 -77.94556 147 319555 2011 8 28 0.00 0.0 0. COOP 36.13083 -81.22750 148 319667 2011 8 28 0.00 0.014472943 0.01447294 COOP 35.85583 -80.36083 149 319675 2011 8 28 0.00 0.0 0. COOP 36.13056 -80.54806 v.fit = vgm(phi=1.2914,model=Mat,range=14.1611,nugget=0.23,kappa=83.1047) krige.pred.out = krige(formula=bias~1,locations=~lon+lat,data=casedata,newdata=gridmeta,model=v.fit) [using ordinary kriging] krige.pred.out lon lat var1.pred var1.var 1 -86.28771 33.91335 NaN NaN 2 -86.27388 33.94722 NaN NaN 3 -86.26003 33.98111 NaN NaN 4 -86.24615 34.01500 NaN NaN 5 -86.23225 34.04890 NaN NaN 6 -86.21835 34.08280 NaN NaN 7 -86.20441 34.11672 NaN NaN 8 -86.19045 34.15064 NaN NaN 9 -86.17648 34.18457 NaN NaN 10 -86.16249 34.21851 NaN NaN summary(krige.pred.out) lon lat var1.predvar1.var Min. :-86.29 Min. :30.51 Min. : NA Min. : NA 1st Qu.:-82.38 1st Qu.:33.73 1st Qu.: NA 1st Qu.: NA Median :-79.88 Median :35.22 Median : NA Median : NA Mean :-79.91 Mean :35.26 Mean :NaN Mean :NaN 3rd Qu.:-77.48 3rd Qu.:36.76 3rd Qu.: NA 3rd Qu.: NA Max. :-73.16 Max. :40.16 Max. : NA Max. : NA NA's :44408 NA's :44408 Any ideas are greatly appreciated, I'm not getting any warnings or errors from krige, so this is perplexing to me. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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 read netcdf file in R
Yogesh, The package I most commonly use for reading and working with netcdfs is called ncdf. It has a collection of commands for working with netcdf files, but the open command is open.ncdf in this package Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University On Mon, Apr 2, 2012 at 4:54 AM, Yogesh Tiwari yogesh@googlemail.com wrote: Dear R Users, I am using R 2.14.1 on windows How to read netcdf files in R ? Which packeges do we need to install for this, and what commands are used for reading netcdf files. Thanks, Best Regards, Yogesh Tiwari -- Yogesh K. Tiwari (Dr.rer.nat), Scientist, Centre for Climate Change Research, Indian Institute of Tropical Meteorology, Homi Bhabha Road, Pashan, Pune-411008 INDIA Phone: 0091-99 2273 9513 (Cell) : 0091-20-25904452 (O) Fax : 0091-20-258 93 825 [[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. __ 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] windrose color ramp issue
An update on this particular problem I've been having. It seems that the number of pedals (the bins argument of the windrose command) is somehow effecting the number of colors that will be plotted. This makes no sense since it only controls the number of pedals in the windrose not the number of colors. A On Mon, Dec 12, 2011 at 4:20 PM, Adrienne Wootten amwoo...@ncsu.edu wrote: Greetings! I'm having an issue with the windrose produced by the windrose function from the circular package. For our weather stations in North Carolina I'm helping with a script which takes hourly wind speed and direction data to create windroses for our end users. One of the stations in the mountains frequently reaches wind speed of 40 to 60 mph and in storms can reach wind speed over 80 mph. This has brought up an issue with the color ramp. Currently the color ramp works one of two ways: #1) Holding the pedal colors fixed: incrspeeds = 2 # fills out the increments argument pedalcolors = c(darkblue,blue,royalblue,darkturquoise,forestgreen,green,yellowgreen,yellow4,yellow,orange,red,darkred,violetred,mediumorchid,purple) #or 2) Making the pedal colors dynamic based on the increments and the max wind speed. incrspeeds = 2 # fills out the increments argument breaks=seq(0,round(maxwind),incrspeeds) # maxwind is the maximum wind speed numcolors=length(breaks) if(numcolorslength(pedalcolors)){ pedalcolors=rev(rainbow(numcolors)) } # In either case the result ends up in the wind rose function rose=windrose(wr,breaks=NULL,bins=numpedals,increment=incrspeeds,main=NULL,fill.col=pedalcolors,plot.mids=FALSE,units=degrees,template=geographics,ticks=FALSE,osize=0.05,cir.ind =0.05,cex=0.9,zero=NULL,rotation=NULL,right=FALSE,shrink=NULL,label.freq=TRUE,calm=c(0),number.calm=TRUE) The issue when there are really large wind speeds in the first case, the windrose wraps the color ramp twice in both the legend and the windrose itself, and then gives only white for the remaining bands of each pedal. For the second case, when pedalcolors will have the same number of colors as numcolors, the legend has the right color ramp, but the pedals of the windrose don't have all the colors that are passed to it. Say pedalcolors has 40 colors, which are included in the legend, but the windrose only includes the first 16 colors in pedalcolors. Any thoughts as to why this might be happening in either case? Thanks! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] windrose color ramp issue
The issue is solved. For those who wish to use windrose out of the circular package and have more groups magnitude than they do pedals, please note the following line in the windrose function - fill.col = rep(fill.col,length.out=bins) # where bins is the number of pedals, fill.col is the fill colors for the pedals This line the function forces the number of colors in the windrose to be equal to the number of pedals, which frankly makes no sense to me. I have since fixed the windrose function in the circular package, replacing the above line with the following: if(length(fill.col)J){ fill.col - rep(fill.col,length.out=J) # J is the number of groups } This correction forces the number of colors to be equal to the number of groups of magnitudes, when the number of colors is smaller than the number of groups - otherwise it functions as normal for too many colors. This is my correction for issues regarding the number of colors, submitted so it will hopefully stay in an archive somewhere for the next person who has this problem. A On Tue, Dec 13, 2011 at 12:12 PM, Adrienne Wootten amwoo...@ncsu.edu wrote: An update on this particular problem I've been having. It seems that the number of pedals (the bins argument of the windrose command) is somehow effecting the number of colors that will be plotted. This makes no sense since it only controls the number of pedals in the windrose not the number of colors. A On Mon, Dec 12, 2011 at 4:20 PM, Adrienne Wootten amwoo...@ncsu.edu wrote: Greetings! I'm having an issue with the windrose produced by the windrose function from the circular package. For our weather stations in North Carolina I'm helping with a script which takes hourly wind speed and direction data to create windroses for our end users. One of the stations in the mountains frequently reaches wind speed of 40 to 60 mph and in storms can reach wind speed over 80 mph. This has brought up an issue with the color ramp. Currently the color ramp works one of two ways: #1) Holding the pedal colors fixed: incrspeeds = 2 # fills out the increments argument pedalcolors = c(darkblue,blue,royalblue,darkturquoise,forestgreen,green,yellowgreen,yellow4,yellow,orange,red,darkred,violetred,mediumorchid,purple) #or 2) Making the pedal colors dynamic based on the increments and the max wind speed. incrspeeds = 2 # fills out the increments argument breaks=seq(0,round(maxwind),incrspeeds) # maxwind is the maximum wind speed numcolors=length(breaks) if(numcolorslength(pedalcolors)){ pedalcolors=rev(rainbow(numcolors)) } # In either case the result ends up in the wind rose function rose=windrose(wr,breaks=NULL,bins=numpedals,increment=incrspeeds,main=NULL,fill.col=pedalcolors,plot.mids=FALSE,units=degrees,template=geographics,ticks=FALSE,osize=0.05,cir.ind =0.05,cex=0.9,zero=NULL,rotation=NULL,right=FALSE,shrink=NULL,label.freq=TRUE,calm=c(0),number.calm=TRUE) The issue when there are really large wind speeds in the first case, the windrose wraps the color ramp twice in both the legend and the windrose itself, and then gives only white for the remaining bands of each pedal. For the second case, when pedalcolors will have the same number of colors as numcolors, the legend has the right color ramp, but the pedals of the windrose don't have all the colors that are passed to it. Say pedalcolors has 40 colors, which are included in the legend, but the windrose only includes the first 16 colors in pedalcolors. Any thoughts as to why this might be happening in either case? Thanks! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] windrose color ramp issue
Greetings! I'm having an issue with the windrose produced by the windrose function from the circular package. For our weather stations in North Carolina I'm helping with a script which takes hourly wind speed and direction data to create windroses for our end users. One of the stations in the mountains frequently reaches wind speed of 40 to 60 mph and in storms can reach wind speed over 80 mph. This has brought up an issue with the color ramp. Currently the color ramp works one of two ways: #1) Holding the pedal colors fixed: incrspeeds = 2 # fills out the increments argument pedalcolors = c(darkblue,blue,royalblue,darkturquoise,forestgreen,green,yellowgreen,yellow4,yellow,orange,red,darkred,violetred,mediumorchid,purple) #or 2) Making the pedal colors dynamic based on the increments and the max wind speed. incrspeeds = 2 # fills out the increments argument breaks=seq(0,round(maxwind),incrspeeds) # maxwind is the maximum wind speed numcolors=length(breaks) if(numcolorslength(pedalcolors)){ pedalcolors=rev(rainbow(numcolors)) } # In either case the result ends up in the wind rose function rose=windrose(wr,breaks=NULL,bins=numpedals,increment=incrspeeds,main=NULL,fill.col=pedalcolors,plot.mids=FALSE,units=degrees,template=geographics,ticks=FALSE,osize=0.05,cir.ind =0.05,cex=0.9,zero=NULL,rotation=NULL,right=FALSE,shrink=NULL,label.freq=TRUE,calm=c(0),number.calm=TRUE) The issue when there are really large wind speeds in the first case, the windrose wraps the color ramp twice in both the legend and the windrose itself, and then gives only white for the remaining bands of each pedal. For the second case, when pedalcolors will have the same number of colors as numcolors, the legend has the right color ramp, but the pedals of the windrose don't have all the colors that are passed to it. Say pedalcolors has 40 colors, which are included in the legend, but the windrose only includes the first 16 colors in pedalcolors. Any thoughts as to why this might be happening in either case? Thanks! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] difftime producing NA values in R 2.12.2
R-listers, I have noticed several posts on issues with difftime producing NA's but they have been for older versions of R. Here's the issue associated with difftime that I am dealing with in R 2.12.2. preciptime = strptime(01/10/2007 14:00,format=%m/%d/%Y %H:%M) class(preciptime) [1] POSIXlt POSIXt # Now using difftime, this is what happens difftime(strptime(03/11/2007 01:00,format=%m/%d/%Y %H:%M),preciptime,units=hours) Time difference of 1427 hours difftime(strptime(03/11/2007 02:00,format=%m/%d/%Y %H:%M),preciptime,units=hours) Time difference of NA hours difftime(strptime(03/11/2007 03:00,format=%m/%d/%Y %H:%M),preciptime,units=hours) Time difference of 1428 hours This doesn't make sense to me since both times used in difftime are in the same format after using strptime, but the differences are coming out wrong. It should be 1427, 1428, and 1429, so I'm confused as to how to fix this. The idea with the program is to compute the time in hours since last rainfall, so everything gets thrown off with this producing NA's. For reference, Operating system is Windows 7 Enterprise, R is version 2.12.2 (64-bit), any guidance is appreciated. Thanks in advance! A -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] difftime producing NA values in R 2.12.2
Ben, Thanks! That really helped. Turns out all the data being used was in EST and currently the system timezone is EDT. Thanks for the tip! A On Tue, Oct 25, 2011 at 2:27 PM, Ben Bolker bbol...@gmail.com wrote: Adrienne Wootten amwootte at ncsu.edu writes: R-listers, I have noticed several posts on issues with difftime producing NA's but they have been for older versions of R. Here's the issue associated with difftime that I am dealing with in R 2.12.2. difftime(strptime(03/11/2007 02:00,format=%m/%d/%Y %H:%M),preciptime,units=hours) Time difference of NA hours I haven't checked, but I strongly suspect that this time falls during the leap forward daylight-savings-time switch in your current time zone, so that time doesn't actually exist in your time zone ... __ 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. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] Wind roses with circular package
R-listers, My colleagues and I are working on wind roses using station information and we have a question regarding the plotting of rings of the wind rose (see attached for example). What we would like to do is to add an extra frequency ring to the wind rose, so it goes up to 15% for instance and that none of the pedals going beyond the outside frequency ring. Anyone have ideas on how to do this? The package used is the circular package. Thanks in advance! -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] foreach not recognizing functions in memory
Roger, I think I know why you are having a problem. Your foreach is using %dopar% and judging from your output you have active parallel processing. When running something in paralell, the master is the processor used by the Rgui, and the slaves are in the background. My guess is the the function you made is the memory of the master, but not in the memory for the slaves. You need to pass the function TestFun to your slaves. If the slave don't know about it, using foreach with %dopar% will fail since the function doesn't exist on the slave processors. Without knowing what package you used to do your parallel processing I can't tell you how to pass the function you created to your slave processors. Try running the foreach command with %do% in place of %dopar%. If the command works, then it is probably a problem that function isn't in memory on the slave processors. Adrienne On Tue, Jul 12, 2011 at 5:28 PM, Bos, Roger roger@rothschild.com wrote: All, I am not understanding the scoping used in foreach when it is used inside a function. I keep getting could not find function errors for functions that are in memory when I try to use foreach within a function call. I have a simple example below. testFun is in memory and works when called by foreach directly, but when I place foreach in a function called helper and then call helper, foreach cannot find testFun. Can anyone provide a fix? I hope its just a simple mistake on my part and not a limitation of foreach. source(.trPaths[5], echo=TRUE, max.deparse.length=150) testFun - function(m) { + out - m*m + } out - foreach(m=1:10, .combine=rbind) %dopar% testFun(m) helper - function(i) { + + out - foreach(m=1:i, .combine=rbind) %dopar% testFun(m) + } out - helper(10) Error in testFun(m) : task 1 failed - could not find function testFun Thanks, Roger *** This message is for the named person's use only. It may\...{{dropped:20}} __ 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. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] For help in R coding
Kings College London From: David Winsemius [dwinsem...@comcast.net] Sent: Monday, July 04, 2011 12:11 AM To: Bansal, Vikas Cc: Dennis Murphy; r-help@r-project.org Subject: Re: [R] For help in R coding On Jul 3, 2011, at 6:10 PM, Bansal, Vikas wrote: From: David Winsemius [dwinsem...@comcast.net] Sent: Sunday, July 03, 2011 7:08 PM the code is same i just want to add a condition so that it should check that if in column 3, the character is A then make number of A equal to total number of . and , Should I explain better or can you please tell me which thing is not clear? My second posting today had a solution. -- David. Can you please help me how to use this if condition in your coding or we can also do it by using some other condition rather than if condition? David Winsemius, MD West Hartford, CT __ 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-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. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] Still have problems with tcltk in R 64 bit
Arnaud, I've had a similar problem with several packages in R 2.13.0 recently, which was more caused because the package installation failed in the new version. Judging from the error messages you have up here, I'd guess that it's a similar problem surrounding the package installation, specifically that something happened which may have damaged the .dll file. So to answer your last question, I'd try re-installing the Tcl/Tk package and see if that fixes the problem. Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University On Tue, Jun 14, 2011 at 11:46 AM, Arnaud Mosnier a.mosn...@gmail.com wrote: Dear R users, Since a long time now, I have the following error when I want to load the tcltk library in R 64 bit. Loading Tcl/Tk interface ...Error : .onLoad failed in loadNamespace() for 'tcltk', details: call: inDL(x, as.logical(local), as.logical(now), ...) error: unable to load shared object 'C:/Program Files/R/R-2.13.0/library/tcltk/libs/x64/tcltk.dll': LoadLibrary failure: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. Error: package/namespace load failed for 'tcltk' sessionInfo() gives R version 2.13.0 (2011-04-13) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252 [3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C [5] LC_TIME=English_Canada.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.13.0 The problem does not occur with the 32 bit version of R, thus, it is probably linked to the 64 version of Tcltk... I already tried: - to uninstall then reinstall R (changing version too...) - install ActiveTcl8.5.9.2.294317-win32-x86_64-threaded.exe (I also tried installing previous version of ActiveTcl) ... without results ! Reinstalling the application may fix this problem ... I agree, but what should I reinstall ? Any suggestions ? Arnaud __ 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-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] Still have problems with tcltk in R 64 bit
Taking a quick look for it, it seems that they have replaced it with tcltk2. I just did the installation with the same version in windows and it auto loaded the tcltk package and I never installed that package to begin with. I would try it with tcltk2 and see if you get the package to install appropriately. I'm not sure why tcltk isn't on CRAN anymore, it makes no sense not to have both tcltk and tcltk2, but here's hoping this helps you out. A On Tue, Jun 14, 2011 at 3:35 PM, Arnaud Mosnier a.mosn...@gmail.com wrote: Thanks for your help, but I can not reinstall tcltk with this command line because tcltk is not available on CRAN. I have tested it (removed tcltk folder from the R/library then tried to reinstall it) You will have an error message... Warning message: In getDependencies(pkgs, dependencies, available, lib) : package ‘tcltk’ is not available (for R version 2.13.0) Arnaud 2011/6/14 Adrienne Wootten amwoo...@ncsu.edu: First thing is to delete the old package from your system, you can do this simply by deleting the files associated with the package. Second, you can re-install the package using this command in R install.packages(tcltk,dependencies=TRUE) That should do the trick, but you will probably want to delete the old package first so that R doesn't continue to refer to the package that doesn't work. Adrienne On Tue, Jun 14, 2011 at 1:39 PM, Arnaud Mosnier a.mosn...@gmail.com wrote: I agree but how to reinstall the tcltk package ? You can not download it from CRAN ! 2011/6/14 Adrienne Wootten amwoo...@ncsu.edu: Arnaud, I've had a similar problem with several packages in R 2.13.0 recently, which was more caused because the package installation failed in the new version. Judging from the error messages you have up here, I'd guess that it's a similar problem surrounding the package installation, specifically that something happened which may have damaged the .dll file. So to answer your last question, I'd try re-installing the Tcl/Tk package and see if that fixes the problem. Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University On Tue, Jun 14, 2011 at 11:46 AM, Arnaud Mosnier a.mosn...@gmail.com wrote: Dear R users, Since a long time now, I have the following error when I want to load the tcltk library in R 64 bit. Loading Tcl/Tk interface ...Error : .onLoad failed in loadNamespace() for 'tcltk', details: call: inDL(x, as.logical(local), as.logical(now), ...) error: unable to load shared object 'C:/Program Files/R/R-2.13.0/library/tcltk/libs/x64/tcltk.dll': LoadLibrary failure: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. Error: package/namespace load failed for 'tcltk' sessionInfo() gives R version 2.13.0 (2011-04-13) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_Canada.1252 LC_CTYPE=English_Canada.1252 [3] LC_MONETARY=English_Canada.1252 LC_NUMERIC=C [5] LC_TIME=English_Canada.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.13.0 The problem does not occur with the 32 bit version of R, thus, it is probably linked to the 64 version of Tcltk... I already tried: - to uninstall then reinstall R (changing version too...) - install ActiveTcl8.5.9.2.294317-win32-x86_64-threaded.exe (I also tried installing previous version of ActiveTcl) ... without results ! Reinstalling the application may fix this problem ... I agree, but what should I reinstall ? Any suggestions ? Arnaud __ 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. -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] Crouts algorithm
R-listers Quick question for the group. Is there any LU decomposition that makes use of Crout's algorithm in R. I've been looking for it and I really haven't seen it among the R packages. A -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] error message
It's not immediately evident to me just looking at this, but I do have one suggestion. Run this again and see if you get the same error and if you do, use this command to help you find where - traceback() traceback is extremely helpful when you are debugging these things, it will go through your function and find where your function crashed if not why exactly. Give it a try. A -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University On Tue, Jan 18, 2011 at 11:32 AM, wangwallace talentt...@gmail.com wrote: I was running a sampling syntax based on a data frame (ago) of 160 rows and 25 columns. Below are the column names: names(ago) [1] SubID AGR1 AGR2 AGR3 AGR4 AGR5 AGR6 AGR7 AGR8 [10] AGR9 AGR10 WAGR1 WAGR2 WAGR3 WAGR4 WAGR5 WAGR6 WAGR7 [19] WAGR8 WAGR9 WAGR10 ocbi ocbo cwbi cwbo dim(ago) [1] 160 25 Here below is the syntax: crossed1-function(df){ + s1-df[sample(nrow(df),16,replace=F),] + s2-t(apply(s1[,2:11],1,sample,10)) + s2-data.frame(s2) + s3-df[-s1$SubID,] + ind-t(replicate(nrow(s3),sample(10))) + s4-matrix(nrow=nrow(s3),ncol=10) + rownames(s4)-rownames(s3) + s4-data.frame(s4) + for(i in seq(nrow(s3))){ + s4[i,1]-s3[i,1+ind[i,1]] + s4[i,2:10]-s3[i,11+ind[i,2:10]] + } + colnames(s2)-colnames(s4) + rbind(s2,s4)[order(as.numeric(rownames(rbind(s2,s4,] + } result1-vector(list,1000) for(i in 1:1000)result1[[i]]-crossed1(ago) These syntaxes worked out perfectly. I successfully drew 1000 random samples. I applied the syntax above to another data set called agr, which basically has the same column names: names(agr) [1] SubID AGR1 AGR2 AGR3 AGR4 AGR5 AGR6 AGR7 AGR8 [10] AGR9 AGR10 WAGR1 WAGR2 WAGR3 WAGR4 WAGR5 WAGR6 WAGR7 [19] WAGR8 WAGR9 WAGR10 ocbi ocbo cwbi cwbo agr and ago are basically the same except that agr has more rows than ago: dim(agr) [1] 300 25 here below is the same syntax I've used for agr: crossed1-function(df){ + s1-df[sample(nrow(df),30,replace=F),] # note I have changed 16 to 30 + s2-t(apply(s1[,2:11],1,sample,10)) + s2-data.frame(s2) + s3-df[-s1$SubID,] + ind-t(replicate(nrow(s3),sample(10))) + s4-matrix(nrow=nrow(s3),ncol=10) + rownames(s4)-rownames(s3) + s4-data.frame(s4) + for(i in seq(nrow(s3))){ + s4[i,1]-s3[i,1+ind[i,1]] + s4[i,2:10]-s3[i,11+ind[i,2:10]] + } + colnames(s2)-colnames(s4) + rbind(s2,s4)[order(as.numeric(rownames(rbind(s2,s4,] + } result1-vector(list,1000) for(i in 1:1000)result1[[i]]-crossed1(agr) Error in xj[i] : only 0's may be mixed with negative subscripts However, I got an error message shown above. It is kinda weird. Could anybody please explain what I've did wrong? Many thanks in advance!! :) -- View this message in context: http://r.789695.n4.nabble.com/error-message-tp3223412p3223412.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-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] parLapply - Error in do.call(fun, lapply(args, enquote)) : could not find function fun
Alright, I found the solution on my own at least it seems that way. Here is what I discovered for future reference in the R-help archive. The issue with parLapply comes in with the docall function in parLapply, parLapply function(cl, x, fun, ...){ docall(c, clusterApply(cl, splitList(x, length(cl)), lapply, fun, ...)) } What I found to be happening is although I had done nothing to assign a value to c (i.e. c - something), c was altered to be a numerical value. Taking the docall function out of parLapply and modifying it to the following allowed it to function properly docall(c,clusterApply(cl,splitList(x,length(cl)),lapply,fun,...)) docall will find the right function like this and it works appropriately as well. I don't know how exactly that c, without quotes, was modified to begin with, since my code showed nothing visibly modifying it. Regardless, this is what worked for me. A On Thu, Dec 2, 2010 at 4:36 PM, Adrienne Wootten amwoo...@ncsu.edu wrote: Hello everybody, I've got a bit of a problem with parLapply that's left me scratching my head today. I've tried this in R 2.11 and the 23 bit Revolution R Enterprise and gotten the same result, OS in question is Windows XP, the package involved is the snow package. I've got a list of 20 rain/no rain (1/0) situations for these two stations i and j, all the items in this list look like this, each of these have a different order: setin[[20]] ri_1 rj_1 2900 1500 3 10 3100 4 00 1 11 1811 2100 2700 2400 6 01 9 11 5 00 3000 2500 2800 1911 8 00 1711 1200 1000 2211 2300 7 11 1400 2600 1300 2 01 1111 2011 1611 What I'm trying to do is to apply the same function I crafted to all the items in the list with parLapply. I'm using this instead of apply since it allows me to test using parallel running R, and because it runs faster than lapply. M_set - parLapply(cl,setin,M.set.find,month=month,n1=n1,n2=n2,MC_1st_obs2=MC_1st_obs2) using this it gives me the follow error: Error in do.call(fun, lapply(args, enquote)) : could not find function fun but, this works correctly when I just use lapply (it's just a bit slower than I need it to be). Also, I know that the clusterCall function works fine with my homemade function because all the nodes of the cluster return the appropriate results when I try this: clusterCall(cl,M.set.find,setin=setin[[1]],month=month,n1=n1,n2=n2,MC_1st_obs2=MC_1st_obs2) but that will only let me do this calculation one at a time. I perused the earlier post about this error, and that doesn't work for me, I don't do anything anywhere in my code to mess with c. I also know that my code is producing the rest of the required parts for the function correctly. I wish I could provide more on what's happened, but the code involved is somewhat extensive. Any ideas all of you have would be wonderful. Thanks in advance! A -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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.
[R] parLapply - Error in do.call(fun, lapply(args, enquote)) : could not find function fun
Hello everybody, I've got a bit of a problem with parLapply that's left me scratching my head today. I've tried this in R 2.11 and the 23 bit Revolution R Enterprise and gotten the same result, OS in question is Windows XP, the package involved is the snow package. I've got a list of 20 rain/no rain (1/0) situations for these two stations i and j, all the items in this list look like this, each of these have a different order: setin[[20]] ri_1 rj_1 2900 1500 3 10 3100 4 00 1 11 1811 2100 2700 2400 6 01 9 11 5 00 3000 2500 2800 1911 8 00 1711 1200 1000 2211 2300 7 11 1400 2600 1300 2 01 1111 2011 1611 What I'm trying to do is to apply the same function I crafted to all the items in the list with parLapply. I'm using this instead of apply since it allows me to test using parallel running R, and because it runs faster than lapply. M_set - parLapply(cl,setin,M.set.find,month=month,n1=n1,n2=n2,MC_1st_obs2=MC_1st_obs2) using this it gives me the follow error: Error in do.call(fun, lapply(args, enquote)) : could not find function fun but, this works correctly when I just use lapply (it's just a bit slower than I need it to be). Also, I know that the clusterCall function works fine with my homemade function because all the nodes of the cluster return the appropriate results when I try this: clusterCall(cl,M.set.find,setin=setin[[1]],month=month,n1=n1,n2=n2,MC_1st_obs2=MC_1st_obs2) but that will only let me do this calculation one at a time. I perused the earlier post about this error, and that doesn't work for me, I don't do anything anywhere in my code to mess with c. I also know that my code is producing the rest of the required parts for the function correctly. I wish I could provide more on what's happened, but the code involved is somewhat extensive. Any ideas all of you have would be wonderful. Thanks in advance! A -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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.
Re: [R] excluding factors from sampling
From the way you describe it, I'd wager that you could probably use the sample function. For Example: group = c(a,b,c,d,e,f,g,h,i,j) sample(group,length(group),replace=FALSE) [1] h j g f d i e b a c but if you change replace to be TRUE sample(group,length(group),replace=TRUE) [1] j h e d i a j h e b for more information check out the help file for sample ?sample A -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University On Tue, Nov 30, 2010 at 12:45 PM, Emma Moran emma.r.mo...@gmail.com wrote: Hello, I am trying to write a function that first requires randomly sampling items from a set of factors. I need to be able to sample from that same set of factors, but exclude the ones that have already been sampled previously. For example, suppose I have a set of items a-j (a,b,c,d,e,f,g,h,i, and j) and randomly sample a, c, and f from that group. How do I sample again from the larger group (a-j) but exclude the items (a,c,f) that I have already sampled. I want this to be a function, so I don't want to just manually exclude a,c, and f. Thanks! -- Emma Moran Washington University in St Louis Biology Department McDonnell Hall Rm 419 One Brookings Drive, St. Louis, MO 63130 emo...@wustl.edu [[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. [[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.
Re: [R] How to extract Friday data from daily data.
Hey, This should work, but after you read in your data make sure that your day, date and time are separate, this should work just fine, or something like it. testdata views number timestamp day date time 1 views 910401 1246192687 Sun 6/28/2009 12:38 2 views 921537 1246278917 Mon 6/29/2009 12:35 3 views 934280 1246365403 Tue 6/30/2009 12:36 4 views 986463 1246888699 Mon 7/6/2009 13:58 5 views 995002 1246970243 Tue 7/7/2009 12:37 6 views 1005211 1247079398 Wed 7/8/2009 18:56 7 views 1011144 1247135553 Thu 7/9/2009 10:32 8 views 1026765 1247308591 Sat 7/11/2009 10:36 9 views 1036856 1247436951 Sun 7/12/2009 22:15 10 views 1040909 1247481564 Mon 7/13/2009 10:39 11 views 1057337 1247568387 Tue 7/14/2009 10:46 12 views 1066999 1247665787 Wed 7/15/2009 13:49 13 views 1077726 1247778752 Thu 7/16/2009 21:12 14 views 1083059 1247845413 Fri 7/17/2009 15:43 15 views 1083059 1247845824 Fri 7/17/2009 18:45 16 views 1089529 1247914194 Sat 7/18/2009 10:49 testdata$date = as.Date(testdata$date,%m/%d/%Y) Thudat = subset(testdata,day==Thu) Fridat = subset(testdata,day==Fri) Friday_dates = Thudat$date+1 Friday_info = NULL for(i in 1:length(Friday_dates)){ temp = subset(Fridat,date==Friday_dates[i]) # select the Friday dates from Fridat if(nrow(temp)0){ # if that Friday date value exists in Friday Friday_info = rbind(Friday_info,temp[nrow(temp),]) # by saying nrow(temp) with the data organized chronologically already, you don't have to add an additional if statement for multiple measurements in the same day. } else { # if that Friday date value doesn't exist in Fridat Friday_info = rbind(Friday_info,Thudat[i,]) # choosing the date from Thudat instead. } } Friday_info views number timestamp day date time 7 views 1011144 1247135553 Thu 2009-07-09 10:32 15 views 1083059 1247845824 Fri 2009-07-17 18:45 Also, for other things involving getting data out to monthly or weekly, you might want to try working with some functions from the chron package. Things like seq.dates can allow you to get the appropriate dates for a specific day of the week for every week that you want. something like this for instance: as.Date(seq.dates(7/3/2009,7/24/2009,by=weeks),%m/%d/%Y) for all the Fridays in July 2009. Hope this helps! A -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University On Fri, Nov 5, 2010 at 1:22 PM, thornbird huachang...@gmail.com wrote: I am new to Using R for data analysis. I have an incomplete time series dataset that is in daily format. I want to extract only Friday data from it. However, there are two problems with it. First, if Friday data is missing in that week, I need to extract the data of the day prior to that Friday (e.g. Thursday). Second, sometimes there are duplicate Friday data (say Friday morning and afternoon), but I only need the latest one (Friday afternoon). My question is how I can only extract the Friday data and make it a new dataset so that I have data for every single week for the convenience of data analysis. Your help and time will be appreciated. Thanks. Kevin Below is what my dataset looks like: views number timestamp daytime 1 views 910401 1246192687 Sun 6/28/2009 12:38 2 views 921537 1246278917 Mon 6/29/2009 12:35 3 views 934280 1246365403 Tue 6/30/2009 12:36 4 views 986463 1246888699 Mon 7/6/2009 13:58 5 views 995002 1246970243 Tue 7/7/2009 12:37 6 views 1005211 1247079398 Wed 7/8/2009 18:56 7 views 1011144 1247135553 Thu 7/9/2009 10:32 8 views 1026765 1247308591 Sat 7/11/2009 10:36 9 views 1036856 1247436951 Sun 7/12/2009 22:15 10 views 1040909 1247481564 Mon 7/13/2009 10:39 11 views 1057337 1247568387 Tue 7/14/2009 10:46 12 views 1066999 1247665787 Wed 7/15/2009 13:49 13 views 1077726 1247778752 Thu 7/16/2009 21:12 14 views 1083059 1247845413 Fri 7/17/2009 15:43 15 views 1083059 1247845824 Fri 7/17/2009 18:45 16 views 1089529 1247914194 Sat 7/18/2009 10:49 -- View this message in context: http://r.789695.n4.nabble.com/How-to-extract-Friday-data-from-daily-data-tp3029050p3029050.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. [[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.
[R] ggplot map bounds
To all, I'm working with code below to produce a map with station data plotted in points, but right now I'm having trouble with the mapping portion of this code states - data.frame(map(state, plot=FALSE,xlim= c(-85,-75),ylim=c(33,37))[c(x,y)]) usamap- ggplot(states)+geom_path(aes(x,y)) usamap When I plot this the problem is that the bounds of the plot is from 31N to 38N and 90W to 75W. The problem is that I only need the bounds of the plot to be from 33N to 37N and 85W to 75W. The way this is now, if I try to subset the states object, I get a garbled mess of lines. The rest of the code provides what I'm trying to do with the attached data. usamap + geom_point(data=obsmeans,aes(x=lon,y=lat,colour = month_1),size=5) + scale_colour_gradientn(data=obsmeans,colour=rev(rainbow(17)),breaks=seq(5,21,by=1),limits=c(5,21)) Any ideas for how I can fix this map would be appreciated! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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] ggplot map bounds
Oh this is excellent, Thanks! Adrienne On Mon, Nov 1, 2010 at 11:25 AM, Ista Zahn iz...@psych.rochester.eduwrote: Hi Adrienne, I think usamap + xlim(c(-85, -75)) + ylim(c(33,37)) will do what you want. Best, Ista On Mon, Nov 1, 2010 at 10:52 AM, Adrienne Wootten amwoo...@ncsu.edu wrote: To all, I'm working with code below to produce a map with station data plotted in points, but right now I'm having trouble with the mapping portion of this code states - data.frame(map(state, plot=FALSE,xlim= c(-85,-75),ylim=c(33,37))[c(x,y)]) usamap- ggplot(states)+geom_path(aes(x,y)) usamap When I plot this the problem is that the bounds of the plot is from 31N to 38N and 90W to 75W. The problem is that I only need the bounds of the plot to be from 33N to 37N and 85W to 75W. The way this is now, if I try to subset the states object, I get a garbled mess of lines. The rest of the code provides what I'm trying to do with the attached data. usamap + geom_point(data=obsmeans,aes(x=lon,y=lat,colour = month_1),size=5) + scale_colour_gradientn(data=obsmeans,colour=rev(rainbow(17)),breaks=seq(5,21,by=1),limits=c(5,21)) Any ideas for how I can fix this map would be appreciated! Adrienne -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University __ 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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org -- Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina Department of Marine, Earth and Atmospheric Sciences North Carolina State University [[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.
Re: [R] Conditional looping over a set of variables in R
David, here I'm referring to your data as testmat, a matrix of 140 columns and 1500 rows, but the same or similar notation can be applied to data frames in R. If I understand correctly, you are looking for the first response (column) where you got a value of 1. I'm assuming also that since your missing values are characters then your two numeric values are also characters. keeping all this in mind, try something like this. first = c() # your extra variable which will eventually contain the first correct response for each case for(i in 1:nrow(testmat)){ c = 1 while( c=ncol(testmat) | testmat[i,c] != 1 ){ if( testmat[i,c] == 1){ first[i] = c break # will exit the while loop once it finds the first correct answer, and then jump to the next case } else { c=c+1 # procede to the next column if not } } } Hope this helps you out a bit. Adrienne Wootten NCSU On Fri, Oct 22, 2010 at 11:33 AM, David Herzberg dav...@wpspublish.comwrote: Here's the problem I'm trying to solve in R: I have a data frame that consists of about 1500 cases (rows) of data from kids who took a test of listening comprehension. The columns are their scores (1 = correct, 0 = incorrect, . = missing) on 140 test items. The items are numbered sequentially and are ordered by increasing difficulty as you go from left to right across the columns. I want R to go through the data and find the first correct response for each case. Because of basal and ceiling rules, many cases have missing data on many items before the first correct response appears. For each case, I want R to evaluate the item responses sequentially starting with item 1. If the score is 0 or missing, proceed to the next item and evaluate it. If the score is 1, stop the operation for that case, record the item number of that first correct response in a new variable, proceed to the next case, and restart the operation. In SPSS, this operation would be carried out with LOOP, VECTOR, and DO IF, as follows (assuming the data set is already loaded): * DECLARE A NEW VARIABLE TO HOLD THE ITEM NUMBER OF THE FIRST CORRECT RESPONSE, SET IT EQUAL TO 0. numeric LCfirst1. comp LCfirst1 = 0 * DECLARE A VECTOR TO HOLD THE 140 ITEM RESPONSE VARIABLES. vector x=LC1a_score to LC140a_score. * SET UP A LOOP THAT WILL RUN FROM 1 TO 140, AS LONG AS LCfirst1 = 0. #i IS AN INDEX VARIABLE THAT INCREASES BY 1 EACH TIME THE LOOP RUNS. loop #i=1 to 140 if (LCfirst1 = 0). * SET UP A CONDITIONAL TRANSFORMATION THAT IS EVALUATED FOR EACH ELEMENT OF THE VECTOR. THUS, WHEN #i = 1, THE EXPRESSION EVALUATES THE FIRST ELEMENT OF THE VECTOR (THAT IS, THE FIRST OF THE 140 ITEM RESPONSES). AS THE LOOP RUNS AND #i INCREASES, SUBSEQUENT VECTOR ELELMENTS ARE EVALUATED. THE do if STATEMENT RETAINS CONTROL AND KEEPS LOOPING THROUGH THE VECTOR UNTIL A '1' IS ENCOUNTERED. + do if x(#i) = 1. * WHEN A '1' IS ENCOUNTERED, CONTROL PASSES TO THE NEXT STATEMENT, WHICH RECODES THE VALUE OF THAT VECTOR ELEMENT TO '99'. + comp x(#i) = 99. * AND THEN CONTROL PASSES TO THE NEXT STATEMENT, WHICH RECODES THE VALUE OF LCfirst1 TO THE CURRENT INDEX VALUE, THUS CAPTURING THE ITEM NUMBER OF THE FIRST CORRECT RESPONSE FOR THAT CASE. CHANGING THE VALUE OF LCfirst1 ALSO CAUSE S THE LOOP TO STOP EXECUTING FOR THAT CASE, AND THE PROGRAM MOVES TO THE NEXT CASE AND RESTARTS THE LOOP. + comp LCfirst1 = #i. + end if. end loop. exe. After several hours of trying to translate this procedure to R, I'm stumped. I played around with creating a list to hold the item responses variables (analogous to 'vector' in SPSS), but when I tried to use the list in an R procedure, I kept getting a warning along the lines of 'the list contains 1 element, only the first element will be used'. So perhaps a list is not the appropriate class to 'hold' these variables? It seems that some nested arrangement of 'for' 'while' and/or 'lapply' will allow me to recreate the operation described above? How do I set up the indexing operation analogous to 'loop #i' in SPSS? Any help is appreciated, and I'm happy to provide more information if needed. David S. Herzberg, Ph.D. Vice President, Research and Development Western Psychological Services 12031 Wilshire Blvd. Los Angeles, CA 90025-1251 Phone: (310)478-2061 x144 FAX: (310)478-7838 email: dav...@wpspublish.com [[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. [[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
Re: [R] data.frame query
Try this: before your loop put this output1 - NULL in the loop at the end put this temp - data.frame(cbind(k,n,lam,Q)) output1 - rbind(output1,temp) this should do the trick for you Adrienne NCSU On Thu, Oct 21, 2010 at 12:52 PM, Etn 2nuzz...@gmail.com wrote: Hi All, Apologies for the simplicity of my question, but I would be grateful for any advice. Thanks I'm trying to put the output from a for loop into a data frame, however I have not been successful. The steps I have taken are: *R-code:* for (k in 1:(nt-1-n0) ){ n- n0-1+k lam=n/nt Q=x[n] output1-data.frame(cbind(k,n,lam,Q)) output1 } output1 *R-Output * kn lam Q 1 14 18 0.9 18 I would like the output in this format, but for all the values of k (i.e. 1-14 as opposed to just the last value) I have also tried *R-code:* nt=20 n0=5 x=c(1:20) for (k in 1:(nt-1-n0) ){ n- n0-1+k lam=n/nt Q=x[n] output1-data.frame(cbind(k,n,lam,Q)) print(output1) } *R-Output * k n lam Q 1 1 5 0.25 5 k n lam Q 1 2 6 0.3 6 k n lam Q 1 3 7 0.35 7 k n lam Q 1 4 8 0.4 8 k n lam Q 1 5 9 0.45 9 k n lam Q 1 6 10 0.5 10 k n lam Q 1 7 11 0.55 11 [[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. [[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.
Re: [R] printing a variable during a loop
instead of print use this message(i) the message command is used for things like this and it will print the value of i as you are looping through, but you can also do this: message(Counter value is: ,i) which returns for i = 20 for example Counter value is 20 for more check out the message help section in the html ? message Adrienne Wootten NCSU On Thu, Oct 21, 2010 at 2:05 PM, Antonio Olinto aolint...@bignet.com.brwrote: Hello, About looping, consider the example: for (i in 1:23194) { dat.stat[i,c(2:8)]-quantile(dat.bat[BL==block[i],2],prob=c(0,0.025,0.25,0.5,0.75,0.975,1)) print(i) } I'd like to have the value of i printed for each loop (step). As I could see the values of i are shown on screen only after all the work is done. Thanks in advance for any suggestion. Best regards, Antonio Webmail - iBCMG Internet http://www.ibcmg.com.br __ 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. [[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.
Re: [R] Help saving multiple graphics
You can get all the plots into one file of any name using the pdf command pdf(filename,onefile=TRUE) place this before your loop and say this after your loop dev.off() to stop writing pictures to that pdf file. In this way you can capture lots of images, but be careful. They will be really high quality, and therefore you might get a really large file size. for more say this in R ? pdf Adrienne Wootten NCSU On Thu, Oct 21, 2010 at 2:13 PM, briandavispdx brida...@pdx.edu wrote: I'm a new-ish user using a for loop to create many hundreds of plots. I'm using the par function to create them in a 2x2 graphics window. What I'd like to do is output this 4-up graphic to a file every four plots. I really don't care what the filename is, but it would be useful to be able to specify something like plotnum_1. Any tips on how to go about this? -- View this message in context: http://r.789695.n4.nabble.com/Help-saving-multiple-graphics-tp3006044p3006044.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. [[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.
Re: [R] Error: subscript out of bounds
IRD, It would certainly be more helpful to us if you showed more of your code or what the dataset IWJR.missing is. Here's a couple of suggestions though. I can see what you are doing is picking a random sample of this dataset. Is the IWJR.missing object a data frame or a vector or what? Typically you use length with vectors and nrow with data frames. Also if you have a vector then the appropriate line for this is, IWJR.missing[sample(1:length(IWJR.missing),1)] Since vectors have no columns. Remember that in data frames and matrices you can refer to a data point by its row and column number [ row , column ] , but you don't have columns in vector objects so you would refer to a data point by it's location in the vector [ location ] You can check this with is.vector or is.data.frame if you're not sure. Also, generally this error can occur if R thinks you are referring to a data point that is outside the range of your data. For example in a data frame of 100 rows, R would give this error if you asked for the 101 st row. Check to make sure that you're aren't select something outside your dataset. Still my first guess is that you have mistaken what the object IWJR.missing is. Try looking at that first. Adrienne Wootten NCSU On Mon, Oct 18, 2010 at 12:35 AM, IRD ird_u...@hotmail.com wrote: Dear All I have problem with this: IWJR.temp -IWJR.missing[sample(1:length(IWJR.missing),1),] Error: subscript out of bounds How I can solved this. Thanks IRD __ 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. [[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.
Re: [R] Question
Margaret, I'm not so sure about the pseudo or quasi random value, and without more information about what you are trying to do I can't provide any more than the following. in your R console, ? rnorm rnorm is the function for pulling random variables from a normal distribution, and defaults to have mean 0 and standard deviation 1. There are many other distributions you can pull random values from, include uniform and gamma distributions. ? runif ? rgamma For the other distributions, please explore the html help from the R console to find more. Also if you are looking at pulling a random line from a dataset consider using the sample function ? sample Adrienne Wootten NCSU On Mon, Oct 11, 2010 at 1:32 PM, Margaretta 2014 margaretta_...@rambler.ruwrote: Hello. I would be very grateful if you could help me in using R. I need R commands of pseudo random value and qvazi (quazi) random value. I found commands qnorm and pnorm, but I am not sure that this is the same as I am looking for. Looking forward to hearing from you. Thank you Margaret __ 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. [[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.
[R] cwhmisc package error
R-listers, I'm working on a project where I need to get the lowercase of the county variable in a dataset alike to the example below (only difference is that the full dataset has all the states and counties in the southeast United States). I keep getting this strange error with the lowerize function (which didn't occur the first few times I use the code below), and oddly enough the error doesn't happen with the function CapLeading (which is in the same package). correlfile[1:3,] state statecounty county 1AL AL_AUTAUGA AUTAUGA 2AL AL_BALDWIN BALDWIN 3AL AL_BARBOUR BARBOUR library(cwhmisc) correlfile$county[correlfile$state==AL]=lowerize(correlfile$county[correlfile$state==AL]) Error in get(as.character(FUN), mode = function, envir = envir) : object 'f' of mode 'function' was not found correlfile$county[correlfile$state==AL]=CapLeading(correlfile$county[correlfile$state==AL]) System specs involved R version 2.9.2 cwhmisc package version 2.1 OS - Redhat Enterprise Linux version 5.5 Another strange thing that seems to happen is that when I open R in a different working directory of the server, the lowerize function works without giving this error, for a short while. Before long, it begins giving the aforementioned error message again. I haven't found anything in the archives on this so I turn to the R-listers. Any ideas on why this keeps happening would be very helpful! Thanks! Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina North Carolina State University [[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.
Re: [R] If then else with command for
Marcio, Try this instead: x-c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123,4! 1,54,207,64,562,295,226,63,233) R-142 color-rep(0,142) for(i in 1:R){ y-sample(x,142,replace=FALSE) if (!3471 %in% y !6263 %in% y !6720 %in% y){color[i]-1} else if (3471 %in% y !6263 %in% y !6720 %in% y){color[i]-2} else if (!3471 %in% y 6263 %in% y !6720 %in% y){color[i]-3} else if (!3471 %in% y !6263 %in% y 6720 %in% y){color[i]-4} else if (3471 %in% y 6263 %in% y !6720 %in% y){color[i]-5} else if (3471 %in% y !6263 %in% y 6720 %in% y){color[i]-6} else if (!3471 %in% y 6263 %in% y 6720 %in% y){color[i]-7} else if (3471 %in% y 6263 %in% y 6720 %in% y){color[i]-8} else{color[i]-0} } Before one of the commands in your loop was: x - sample(x,142,replace=FALSE) when you did this you altered your assignment of x to be the sample of your original x with a length of 142. Subsequent iterations of the loop had the same data as the original iteration, but sampled randomly. Your if statements were good, you just redefined x in your first iteration of the loop. Adrienne Adrienne Wootten Graduate Research Assistant State Climate Office of North Carolina North Carolina State University On Tue, Sep 14, 2010 at 1:29 PM, Mestat mes...@pop.com.br wrote: Hey listers, I am trying to do something simple... Check the program below... I would like to create a variable named COLOR according to the conditions that I stablished... But the problem is that it seems that my variable COLOR is checking just on sample, may be last in the loop... Certainly, I am missing something... Thanks in advance, Marcio x-c(288,139,196,159,536,134,623,517,96,467,277,155,386,241,422,6263,612,532,250,412,339,55,290,249,164,97,74,144,1277,240,163,63,488,111,128,230,720,179,37,24,65,37,89,187,60,939,1008,81,310,58,169,38,68,190,78,807,220,226,69,179129,119,73,59,92,127,104,75,505,183,49,41,76,113,90,79,408,140,200,284,103,58,654,118,431,192,233,102,97,56,69,73,86,53,105,81,77,472,129,194,299,81,122,113,186,91,145,133,114,78,78,72,70,3471,641,275,815,149,185,172,240,67,526,122,229,298,317,179,233,66,129,87,82,63,65,72,6720,381,240,118,396,66,35,43,166,216,53,82,90,62,77,207,68,52,277,396,220,751,146,95,37,35,39,46,59,44,105,87,66,62,175,252,128,330,57,83,208,74,63,109,37,105,38,82,76,63,86,603,209,100,121,191,130,63,128,90,79,50,1025,121,87,309,75,189,36,82,84,60,132,46,965,155,132,219,112,53,90,66,100,77,52,60,100,153,418,392,76,130,197,262,49,105,87,70,147,720,342,233,203,249,92,134,231,782,184,182,432,49,63,94,124,69,53,91,451,53,21,42,50,40,32,58,26,28,61,60,35,764,105,592,55,28,46,34,123! ,4! 1,54,207,64,562,295,226,63,233) R-142 color-rep(0,142) for(i in 1:R){ x-sample(x,142,replace=FALSE) if (!3471 %in% x !6263 %in% x !6720 %in% x){color[i]-1} else if (3471 %in% x !6263 %in% x !6720 %in% x){color[i]-2} else if (!3471 %in% x 6263 %in% x !6720 %in% x){color[i]-3} else if (!3471 %in% x !6263 %in% x 6720 %in% x){color[i]-4} else if (3471 %in% x 6263 %in% x !6720 %in% x){color[i]-5} else if (3471 %in% x !6263 %in% x 6720 %in% x){color[i]-6} else if (!3471 %in% x 6263 %in% x 6720 %in% x){color[i]-7} else if (3471 %in% x 6263 %in% x 6720 %in% x){color[i]-8} else{color[i]-0} } -- View this message in context: http://r.789695.n4.nabble.com/If-then-else-with-command-for-tp2539341p2539341.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. [[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.
Re: [R] cannot allocate vector of size 381.5 Mb
I would suggest two things here: check on the size of other object you may have stored in memory, and get rid of what you don't need. ? ls ? rm also, consider running garbage collection to help free up memory in R gc() I hope this helps! A On Tue, Aug 31, 2010 at 1:56 AM, rusers.sh rusers...@gmail.com wrote: Hi, I read some posts from the mailing list on the same problem, but it seems that i still cannot solve this problem. I only want to generate some simulated data. #Generate 2500 observations-it works without errors coords-as.matrix(expand.grid(seq(0,100,length.out=50), seq(0,100,length.out=50))) #SimData is a user-written function SimBinData-SimData(n=2500,coords=coords, x.mu =c(0,1),covar.cor=matrix(c(1,0.25,0.25,1), 2, 2),beta=c(1,0.5,5)) #Generate 1 observations-errors appeared #Following some posts, i set the memory to the maximum value. But i still got errors. memory.limit(size = 4095) [1] 4095 coords-as.matrix(expand.grid(seq(0,100,length.out=100), seq(0,100,length.out=100))) SimBinData-SimData(n=1,coords=coords, x.mu =c(0,1),covar.cor=matrix(c(1,0.25,0.25,1), 2, 2),beta=c(1,0.5,5)) Error: cannot allocate vector of size 381.5 Mb memory.size(TRUE) [1] 1276.31 memory.limit() [1] 4095 object.size(SimBinData) 170568 bytes Any suggestions? Thanks very much. -- - Jane Chang Queen's [[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. [[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.
Re: [R] Running something without a loop when the result from the previous iteration is require for the current iteration
){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[1],1, 0),ifelse(ranortho_obs_used$Pr[4],1,0)) } if(genmat[r,use1]==0 genmat[r,use2]==1){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[2],1, 0),ifelse(ranortho_obs_used$Pr[5],1,0)) } if(genmat[r,use1]==1 genmat[r,use2]==0){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[3],1, 0),ifelse(ranortho_obs_used$Pr[7],1,0)) } if(genmat[r,use1]==1 genmat[r,use2]==1){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[6],1, 0),ifelse(ranortho_obs_used$Pr[8],1,0)) } gc() } genmat } ortho_obs_used is a data frame that contains the probablity of precipitation occurring on a given day for a specific set of condtions. For instance ortho_obs_used$Pr[1] is the probablity of rain at station s for day d, given that there was no rain at station s for day d-1 and no rain at either of the other two stations for day d. The event.gen2 function handles the generation, and it runs quickly for the 5 remaining stations and one month, but I have to run this for 317 stations over 48 months or more, and it becomes a really bad bottleneck. So what I'd like to know is if there is anyway that I can re-write this function to work without a loop. I couldn't find anything from previous posts about getting out of loops where the previous iteration is required to determine the next calculation. Sorry for the length of the post, but I thought it best to try to explain what I was doing first, before diving into my question Thanks in advance! Adrienne Wootten Graduate Research Assistant/Environmental Meteorologist M.S. Atmospheric Science NC State University State Climate Office of North Carolina Raleigh, NC 27695 [[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. [[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.
Re: [R] Running something without a loop when the result from the previous iteration is require for the current iteration
NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA NA NA NA0 NA1 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA num # station to generate [1] 2 use1 # 1st station to use in generation [1] 6 use2 # 2nd station to use in generation [1] 4 genmat = event.gen2(genmat,use1,use2,num,ortho_obs_used) # Generation function shown below genmat # genmat - after it has gone through station 2 [,1] [,2] [,3] [,4] [,5] [,6] [,7] line_before1110111 NA0 NA1 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA1 NA0 NA NA1 NA1 NA1 NA NA1 NA1 NA1 NA NA1 NA0 NA0 NA NA0 NA0 NA0 NA NA1 NA1 NA1 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA1 NA1 NA1 NA NA0 NA0 NA0 NA NA1 NA1 NA1 NA NA0 NA1 NA1 NA NA1 NA1 NA1 NA NA0 NA0 NA0 NA NA1 NA0 NA1 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA1 NA1 NA1 NA NA1 NA1 NA1 NA NA1 NA1 NA1 NA NA0 NA0 NA0 NA Where event.gen2 is this function: event.gen2 = function(genmat,use1,use2,num,ortho_obs_used){ for(r in 2:nrow(genmat)){ ran = runif(1,0,1) if(genmat[r,use1]==0 genmat[r,use2]==0){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[1],1, 0),ifelse(ranortho_obs_used$Pr[4],1,0)) } if(genmat[r,use1]==0 genmat[r,use2]==1){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[2],1, 0),ifelse(ranortho_obs_used$Pr[5],1,0)) } if(genmat[r,use1]==1 genmat[r,use2]==0){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[3],1, 0),ifelse(ranortho_obs_used$Pr[7],1,0)) } if(genmat[r,use1]==1 genmat[r,use2]==1){ genmat[r,num]-ifelse(genmat[r-1,num]==0,ifelse(ranortho_obs_used$Pr[6],1, 0),ifelse(ranortho_obs_used$Pr[8],1,0)) } gc() } genmat } ortho_obs_used is a data frame that contains the probablity of precipitation occurring on a given day for a specific set of condtions. For instance ortho_obs_used$Pr[1] is the probablity of rain at station s for day d, given that there was no rain at station s for day d-1 and no rain at either of the other two stations for day d. The event.gen2 function handles the generation, and it runs quickly for the 5 remaining stations and one month, but I have to run this for 317 stations over 48 months or more, and it becomes a really bad bottleneck. So what I'd like to know is if there is anyway that I can re-write this function to work without a loop. I couldn't find anything from previous posts about getting out of loops where the previous iteration is required to determine the next calculation. Sorry for the length of the post, but I thought it best to try to explain what I was doing first, before diving into my question Thanks in advance! Adrienne Wootten Graduate Research Assistant/Environmental Meteorologist M.S. Atmospheric Science NC State University State Climate Office of North Carolina Raleigh, NC 27695 [[alternative HTML version deleted
Re: [R] Running something without a loop when the result from the previous iteration is require for the current iteration
Thanks everyone for your help and advice. For the R-help archives, here is what I ended up doing. First creating a separate function to handle one day at a time - byrow.gen2 - function(genmat,rownum,use1,use2,num,ortho_obs_used){ prev = rownum-1 ran = runif(length(rownum),0,1) if(genmat[rownum,use1]==0 genmat[rownum,use2]==0 genmat[prev,num]==0) { if(ranortho_obs_used$Pr[1]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } if(genmat[rownum,use1]==0 genmat[rownum,use2]==0 genmat[prev,num]==1) { if(ranortho_obs_used$Pr[4]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } if(genmat[rownum,use1]==0 genmat[rownum,use2]==1 genmat[prev,num]==0) { if(ranortho_obs_used$Pr[2]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } if(genmat[rownum,use1]==0 genmat[rownum,use2]==1 genmat[prev,num]==1) { if(ranortho_obs_used$Pr[5]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } if(genmat[rownum,use1]==1 genmat[rownum,use2]==0 genmat[prev,num]==0) { if(ranortho_obs_used$Pr[3]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } if(genmat[rownum,use1]==1 genmat[rownum,use2]==0 genmat[prev,num]==1) { if(ranortho_obs_used$Pr[7]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } if(genmat[rownum,use1]==1 genmat[rownum,use2]==1 genmat[prev,num]==0) { if(ranortho_obs_used$Pr[6]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } if(genmat[rownum,use1]==1 genmat[rownum,use2]==1 genmat[prev,num]==1) { if(ranortho_obs_used$Pr[8]){ genmat[rownum,num] = 1 }else{ genmat[rownum,num] = 0} } genmat } Then applying the foreach package in the original function event.gen3 = function(genmat,use1,use2,num,ortho_obs_used){ rownum = 2:nrow(genmat) test = foreach(r=iter(rownum,by='row')) %dopar% { genmat = byrow.gen2(genmat,r,use1,use2,num,ortho_obs_used) } rm(test) genmat } The final results were exactly as I needed them to be in my initial post, but the processing time dropped from 2 seconds per station to 0.05 seconds per station. Thanks to everyone for giving me the advice and the idea to try this! Adrienne On Thu, Aug 12, 2010 at 8:15 AM, Adrienne Wootten amwoo...@ncsu.edu wrote: Not quite what I was trying to say. The process generates a random uniform number between 0 and 1 and compares to a specific conditional probability. It is looking for this in particular: random number Pr( rain(station=i,day=d)=1 | rain(station=i,day=d-1)=0 rain(station=j,day=d)=0 rain(station=k,day=d)=0) In this particular example, if the random number is less than the probability the value for station i and day d will be given as 1, otherwise it will be zero. There are 8 possible combinations. i is the station to be generated, j and k are the two stations most strongly correlated with station i. Stations j and k have already been generated in the example I gave previously. So I want to know given what is going on at stations j and k during day d and at station i for day d-1 if the value for station i day d will be 1 or 0. Hope this provides some clarification. A On Thu, Aug 12, 2010 at 3:21 AM, Petr PIKAL petr.pi...@precheza.czwrote: Hi without toy example it is rather complicated to check your function. So only few remarks: Instead of generating 1 random number inside a loop generate whole vector of random numbers outside a loop and choose a number Do not mix ifelse with if. ifelse is intended to work with whole vector. Work with matrices instead of data frames whenever possible if speed is an issue. If I understand correctly you want to put 1 or 0 into one column based on: previous value in the same column comparison of some random number with predefined probabilities in vector of 6 values So here is vectorised version of your 4 ifs based on assumption 0 in col1 0 in col 2 = 5 0 in col1 1 in col 2 = 9 1 in col1 0 in col 2 = 6 1 in col1 1 in col 2 =10 col1-sample(1:2, 20, replace=T) col2-sample(c(4,8), 20, replace=T) col1+col2 [1] 5 6 9 6 6 5 9 10 9 9 6 9 10 6 10 9 10 9 5 5 cols-as.numeric(as.factor(col1+col2)) cols [1] 1 2 3 2 2 1 3 4 3 3 2 3 4 2 4 3 4 3 1 1 And here is computed comparison of six values p (ortho obs used) with 20 generated random values ran-runif(20) p-runif(8) comparison - outer(ran,p, ) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [2,] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [3,] FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE [4,] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [5,] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [6,] FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE [7,] FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE [8,] FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [9,] FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE [10,] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [11,] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE [12,] FALSE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
[R] Running something without a loop when the result from the previous iteration is require for the current iteration
and no rain at either of the other two stations for day d. The event.gen2 function handles the generation, and it runs quickly for the 5 remaining stations and one month, but I have to run this for 317 stations over 48 months or more, and it becomes a really bad bottleneck. So what I'd like to know is if there is anyway that I can re-write this function to work without a loop. I couldn't find anything from previous posts about getting out of loops where the previous iteration is required to determine the next calculation. Sorry for the length of the post, but I thought it best to try to explain what I was doing first, before diving into my question Thanks in advance! Adrienne Wootten Graduate Research Assistant/Environmental Meteorologist M.S. Atmospheric Science NC State University State Climate Office of North Carolina Raleigh, NC 27695 [[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.
Re: [R] Running something without a loop when the result from the previous iteration is require for the current iteration
If it were just one loop by itself and I was doing the calculation for just one month for all of my 317 stations, I would agree with. However, this function itself is inside another loop which goes through each month and year that I need the calculation for each of the stations. If you have any suggestions for how I could speed up the loop that is welcome, but I would like to try to remove it given that it is nested inside another loop. A On Wed, Aug 11, 2010 at 2:59 PM, Greg Snow greg.s...@imail.org wrote: What is wrong with using a loop? It used to be that loops were much slower than some of the alternatives, but now days a well crafted loop runs almost as fast (sometime faster) than the apply functions. So if the loop is working for you, use it and don't worry about it (though there may be ways to speed up the loop). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.s...@imail.org 801.408.8111 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of Adrienne Wootten Sent: Wednesday, August 11, 2010 10:36 AM To: r-help@r-project.org Subject: [R] Running something without a loop when the result from the previous iteration is require for the current iteration Hello Everyone! Here's what I'm trying to do. I'm working on generating occurrences of precipitation based upon precipitation occurrence for a station during the previous day and two stations that have already been generated by joint probablities and 1st order Markov chains or by the same generation process. This has to be done for each remaining stations for each month. genmat # 7 stations in this example, line_before is the climatology of the last day of the previous month. Stations 4 and 6 have been generated already in this example [,1] [,2] [,3] [,4] [,5] [,6] [,7] line_before1110111 NA NA NA1 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA1 NA0 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA NA NA NA0 NA1 NA NA NA NA0 NA0 NA NA NA NA0 NA0 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA1 NA1 NA NA NA NA0 NA0 NA num # station to generate [1] 2 use1 # 1st station to use in generation [1] 6 use2 # 2nd station to use in generation [1] 4 genmat = event.gen2(genmat,use1,use2,num,ortho_obs_used) # Generation function shown below genmat # genmat - after it has gone through station 2 [,1] [,2] [,3] [,4] [,5] [,6] [,7] line_before1110111 NA0 NA1 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA1 NA0 NA NA1 NA1 NA1 NA NA1 NA1 NA1 NA NA1 NA0 NA0 NA NA0 NA0 NA0 NA NA1 NA1 NA1 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA0 NA0 NA0 NA NA1 NA1 NA1 NA NA0 NA0 NA0 NA NA1 NA1 NA1 NA NA0 NA1 NA1 NA NA1 NA