[R] Problem to load a shape file
Dear all, I've been puzzled why I not able to load a shapefile from a connection. Does anyone here can give a reasonable answer? When I try the following script I got this error: Error in getinfo.shape(fn) : Error opening SHP file #Reproduction temp - tempfile() download.file(http://metodologiapolitica.com/download/Brasil.zip,temp) shp.br - readShapePoly(unzip(temp, Brasil.shp)) unlink(temp) Just for acknowledgement, I successful can load the file whenever I open it from the disk. __ 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] expand time period
I'd like to expand the following data to perform a daily time series. It should cover from '2012-07-01' to '2012-10-06' with the values I actually have being the mean from one point measurement to another. Does anyone has a clue to perform this task. structure(list(Date.beg = structure(c(15635, 15617, 15615, 15610, 15609, 15605, 15604, 15601, 15593, 15593, 15586, 15581, 15580, 15577, 15572, 15565, 15552, 15540, 15530, 15516), class = Date), Date.end = structure(c(15619, 15619, 15616, 15615, 15610, 15607, 15604, 15602, 15595, 15594, 15587, 15582, 15581, 15579, 15572, 15567, 15554, 15541, 15533, 15517), class = Date), Pollster = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 4L, 1L, 2L, 1L, 1L, 2L, 1L, 4L, 1L, 2L, 2L, 1L, 3L, 1L), .Label = c(Datafolha, Ibope, Veritá, Vox Populi), class = factor), Serra.PSDB = c(24, 22, 23, 19, 22, 17, 17, 21, 19, 20, 21, 20, 22, 22, 27, 26, 26, 30, 31.4, 31), Russomanno.PRB = c(23, 22, 25, 27, 30, 34, 34, 35, 35, 32, 35, 31, 31, 31, 31, 26, 25, 26, 17.7, 24), Haddad.PT = c(20, 22, 19, 18, 18, 18, 17, 15, 15, 17, 16, 16, 14, 14, 8, 9, 6, 7, 9.5, 6), Chalita.PMDB = c(11, 11, 11, 10, 9, 7, 5, 8, 6, 8, 7, 5, 7, 5, 6, 5, 5, 6, 4.3, 6), Others = c(8, 7, 7, 4, 7, 6, 4, 5, 6, 7, 9, 5, 8, 6, 10, 12, 15, 15, 8.6, 16), Null = c(8, 8, 8, NA, 8, 10, 10, NA, 13, 9, 8, 12, 10, NA, 10, 12, 14, 11, 13.1, 11), Undecided = c(6, 8, 6, NA, 6, 8, 13, NA, 6, 7, 4, 9, 7, 13, 6, 10, 9, 6, 13.3, 5), Round = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L), .Label = c(year before, off campaign, first, second), class = factor), Stage = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c(0, 1, 2), class = factor), Serra.Haddad = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c(, 1, 0), class = factor), N = c(3959, 1204, 2099, 1204, 1799, 1204, 2000, 1802, 1001, 1221, 1078, 1001, 1069, 1200, 1077, 805, 805, 1075, 1331, 1081), Err = c(2, 3, 2, 3, 2, 3, 2.2, 2, 3, 3, 3, 3, 3, 2.8, 3, 3, 3, 3, 2.7, 3)), .Names = c(Date.beg, Date.end, Pollster, Serra.PSDB, Russomanno.PRB, Haddad.PT, Chalita.PMDB, Others, Null, Undecided, Round, Stage, Serra.Haddad, N, Err), row.names = c(6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25), class = data.frame) __ 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] dealing with Date vars
Hi all, I'm writing a function that summarizes objects, however, when a Date variable is present in the objects it doesn't work. How can I handle with this problem? D __ 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] Winsorisation function
Dear all, someone can find what I doing wrong with the following function. It is for winsorisation mean. At my eyes it is ok, but for reason I sometimes it is changing the results when I change the k value. wmean - function (x, na.rm = FALSE, k = 1) { if (any(i.na - is.na(x))) { if (na.rm) x - x[!i.na] else return(NA) } n - length(x) if (!(k %in% (0:n))) stop(Invalid argument for 'k'.) x - sort(x) x[1:k] - x[k+1] # Here I solve the lower values x[(n-k+1):n] - x[n-k] #Then I go over the higher ones return(mean(x)) } set.seed(51) a- sample(200,100) wmean(a, k=5) [1] 95.85 wmean(a, k=6) [1] 95.91 wmean(a, k=7) [1] 95.91 wmean(a, k=8) __ 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] biasing conditional sample
Hi all, I'm looking for some help to bias the sample function. Basically, I'd like to generate a data frame where the first column is completely random, the second, however, is conditional do the first, the third is conditional to the first and the second and so on. By conditional I mean that I shouldn't have repeated values in the line. I know it could be easily implemented using permutation, but it is not the case here. I need at least five columns. Any idea to achieve what do I need? set.seed(51) data - data.frame( id=as.factor(1:100), a=as.factor(sample(1:10, size=100, replace=TRUE)), b=as.factor(sample(1:10, size=100, replace=TRUE)), c=as.factor(sample(1:10, size=100, replace=TRUE)), d=as.factor(sample(1:10, size=100, replace=TRUE)), e=as.factor(sample(1:10, size=100, replace=TRUE)) ) __ 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] biasing conditional sample
Thanks for the solutions. Carlson's and Barradas's approaches give me what I need. Nonetheless, Carlson's proposal is slightly better for my purposes because it's shorter. Thanks Daniel Can't you just use sample() on each row without replacement to guarantee no matches among the five (or more) columns? set.seed(51) Data - sapply(1:100, function(x) sample(1:10, size=5)) Data - data.frame(t(Data)) names(Data) - letters[1:5] -- David L Carlson Associate Professor of Anthropology Texas AM University College Station, TX 77843-4352 -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Rui Barradas Sent: Sunday, November 11, 2012 4:36 PM To: d...@riseup.net Cc: r-help@r-project.org Subject: Re: [R] biasing conditional sample Hello, The function that follows returns a matrix, not a data.frame but does what you ask for. fun - function(x, y, n){ f - function(x, y){ while(TRUE){ rnd - sample(x, 1) if(!any(rnd %in% y)) break } rnd } for(i in seq_len(n)){ tmp - apply(y, 1, function(.y) f(x, .y)) y - cbind(y, tmp) } y } a - cbind(sample(1:10, 100, TRUE)) # must have dims fun(1:10, a, 4) # returns 5 columns, 'a' plus 4 Hope this helps, Rui Barradas Em 11-11-2012 19:06, d...@riseup.net escreveu: Hi all, I'm looking for some help to bias the sample function. Basically, I'd like to generate a data frame where the first column is completely random, the second, however, is conditional do the first, the third is conditional to the first and the second and so on. By conditional I mean that I shouldn't have repeated values in the line. I know it could be easily implemented using permutation, but it is not the case here. I need at least five columns. Any idea to achieve what do I need? set.seed(51) data - data.frame( id=as.factor(1:100), a=as.factor(sample(1:10, size=100, replace=TRUE)), b=as.factor(sample(1:10, size=100, replace=TRUE)), c=as.factor(sample(1:10, size=100, replace=TRUE)), d=as.factor(sample(1:10, size=100, replace=TRUE)), e=as.factor(sample(1:10, size=100, replace=TRUE)) ) __ 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. __ 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] merge( , by='row.names') slowness
I noticed that joining two data.frames in R using the merge function that using by='row.names' slows things down substantially when compared to just joining on a common index column. Using a dataframe size of ~10,000 rows: it's as slow as 10 minutes in the by='row.names' case versus merely 1 second using an index column. Beyond the 10^6 range, it's unusably slow. n - 5 a - data.frame(id=as.character(1:10^n), x=rnorm(10^n)); rownames(a) - a$id b - data.frame(id=as.character(1:10^n + 10^(n-1)), y=rnorm(10^n)); rownames(b) - b$id date() fast - merge(a, b, all=T) date() slow - merge(a, b, all=T, by='row.names') date() Has anybody else noticed this? __ 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] Rotated, Right-Justified Labels for Shortened Tick Marks
Hello R-help, I'm trying to make a fairly simple plot axis that goes something like this: plot(-10:10,-10:10, yaxt='n') axis(side=2, las=1, hadj=1, tck=-.01, cex.axis=.6) ...but as you can see, the labels are not close enough to the y-axis (where I want them... to save space for publication). Can anybody help me figure out how to move these labels over the the right a bit? Thanks, -D __ 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.