Paulo E. Cardoso
2009-Jul-10 10:07 UTC
[R] while( ) to get a vector until the sum of elements are <= X ?
I have a vector of values X = seq(1:10) I want to get another vector V of with sample (with replacement) of X but with a constrain: V will have as much elements as those necessary to V sum exactly 10. If the N-th value of V make the sum greater than 10, it's is subtracted with the difference to achieve the constrain. I don't know how to achieve this. with a while? ____________ Paulo E. Cardoso [[alternative HTML version deleted]]
Petr PIKAL
2009-Jul-10 11:43 UTC
[R] Odp: while( ) to get a vector until the sum of elements are <= X ?
Hi r-help-bounces at r-project.org napsal dne 10.07.2009 12:07:03:> I have a vector of values > > > > X = seq(1:10) > > > > I want to get another vector V of with sample (with replacement) of Xbut> with a constrain: V will have as much elements as those necessary to Vsum> exactly 10. > > > > If the N-th value of V make the sum greater than 10, it's is subtractedwith> the difference to achieve the constrain. > > > > I don't know how to achieve this. with a while?Maybe. X = seq(1:10) fff <- function(x, threshold=10) { v<- sample(X, replace=T) v.<-cumsum(v) logic <- v. <= threshold difer <- 10-sum(v[logic]) if (difer>0) c(v[logic], difer) else v[logic] } Regards Petr> > > > ____________ > > Paulo E. Cardoso > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Paulo E. Cardoso
2009-Jul-10 12:04 UTC
[R] how to export image (TIF, BMP or JPG) from SpatialGridDataFrame or SpatialPixelDataFrame
I'm trying to export a SPDF to a image. The original file, when imported into R (JPG with rGDAL) have dimension = 997x997. When I save as bmp the image(spdf), i get a 672x672 image. ____________ Paulo E. Cardoso