[R] two active RGL device / linked devices
Dear list! My question is, if there is an option in RGL, that two devices can be active. It means, when eg. points in first device is rotated also points in second devies is rotated. For example: library(rgl) x - rnorm(1000) y - rnorm(1000) z - rnorm(1000) open3d() points3d(x,y,z) open3d() points3d(x,y,z) rgl.set(c(1,2), silent=TRUE) #does not work Thanks for suggestions. OV __ 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] truncated cone / frustum in R rgl
Dear list! Here is a simplified script for cone in RGL (from rgl demos). I would like to draw a truncated cone / frustum using RGL in R. Do you have any suggestion how to modify code? Thanks a lot, OV library(rgl) cone3d - function(base=c(0,0,0), tip=c(0,0,1), rad=1, n=30) { trans - diag(4) p1 - c(0,1,0) p2 - c(1,0,0) degvec - seq(0, 2*pi,length = n+1)[-1] ecoord2 - function(theta) {base + rad * (cos(theta) * p1 + sin(theta) * p2)} i - rbind(1:n, c(2:n,1), rep(n+1, n)) v - cbind(sapply(degvec, ecoord2), tip) i - rbind(i, rep(n+1 ,n)) v - cbind(v, base) i.x - rbind(c(2:n, 1), 1:n, rep(n+2, n)) i.x - rbind(i.x, rep(n+2, n)) i - cbind(i, i.x) v - rbind(v, rep(1, ncol(v))) return(rotate3d(qmesh3d(v, i), matrix=trans)) } open3d() q1 - cone3d(tip = c(0,0,2)) shade3d(q1, col = green); axes3d(c('x','y','z')); box3d() __ 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] kruskalmc, significant differences while median values are the same
Thank for help! My ''problem'' is a little bit complicated. I have a dataset of trees (five tree species) and I need to calculate if there are the significant differences in the period of suppressed growth among tree species (length in years, e.g. 1, 2, 3, 6, 10, 50, 80, etc.). Because data are not normally distributed (Levene test), my idea was to use kruskalmc test. So, my question is, how to evaluate the differences in the duration of suppressed growth among groups? Is ''bootstrap confidence interval'' for each tree species right solution? thanks, OV From: Thomas Lumley tlum...@uw.edu To: Omphalodes Verna omphalodes.ve...@yahoo.com Cc: R Help r-help@r-project.org Sent: Sunday, December 16, 2012 11:59 PM Subject: Re: [R] kruskalmc, significant differences while median values are the same On Sat, Dec 15, 2012 at 10:04 PM, Omphalodes Verna omphalodes.ve...@yahoo.com wrote: Dear list! I work with multiple Kruskal-Wallis test (kruskalmc, package pgirmess), which evaluates differences in medians among groups (5 groups). A result of a test is significant differences among some groups, while median values are the same for 4 groups (using tapply). Why? The Kruskal-Wallis test *doesn't* evaluate differences in medians, so there is quite likely nothing wrong in a formal sense. However, this does suggest that your groups may not be stochastically ordered, which means the results of the Kruskal-Wallis test could be quite misleading. I'd suggest that you at least look at pairwise Wilcoxon tests to make sure the direction agrees with what the Kruskal-Wallis test implies. Box plots might also be a good idea. Or, if you really want differences in medians, look at differences in medians. A permutation test or a bootstrap confidence interval is probably the best way to do this. -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland __ 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] kruskalmc, significant differences while median values are the same
Dear list! I work with multiple Kruskal-Wallis test (kruskalmc, package pgirmess), which evaluates differences in medians among groups (5 groups). A result of a test is significant differences among some groups, while median values are the same for 4 groups (using tapply). Why? p.s.: number of samples in groups vary from 50 to 4900. Thanks to all, OV . __ 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] correct function formation in R
Dear list! I have question of 'correct function formation'. Which function (fun1 or fun2; see below) is written more correctly? Using ''structure'' as output or creating empty ''data.frame'' and then transform it as output? (fun1 and fun1 is just for illustration). Thanks a lot, OV code: input - data.frame(x1 = rnorm(20), x2 = rnorm(20), x3 = rnorm(20)) fun1 - function(x) { ID - NULL; minimum - NULL; maximum - NULL for(i in seq_along(names(x))) { ID[i] - names(x)[i] minimum[i] - min(x[, names(x)[i]]) maximum[i] - max(x[, names(x)[i]]) } output - structure(list(ID, minimum, maximum), row.names = seq_along(names(x)), .Names = c(ID, minimum, maximum), class = data.frame) return(output) } fun2 - function(x) { output - data.frame(ID = character(), minimum = numeric(), maximum = numeric(), stringsAsFactors = FALSE) for(i in seq_along(names(x))) { output[i, ID] -names(x)[i] output[i, minimum] - min(x[, names(x)[i]]) output[i, maximum] - max(x[, names(x)[i]]) } return(output) } fun1(input) fun2(input) __ 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] transform input argument (matrix) of function
Dear list! I would like to write a function to transform matrix, which is input argument of a written function. It is easy with new matrix (see below), but my idea is to transform input argument (matrix) of function without any additional matrixes. Here is an example: fun1 - function(xy) { xy - cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) return(xy) } df1 - matrix(c(1,2,3,1,2,3), ncol = 2) fun1(df1) fun2 - function(xy) { xy - cbind(xy[,1], xy[,2], xy[,1] + xy[,2]) return(invisible(xy)) } fun2(df1) df1 should be [,1] [,2] [,3] [1,] 1 1 2 [2,] 2 2 4 [3,] 3 3 6 Thanks to all for help, OV [[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] row index for max values of row groups
Thansk for help to all. OV From: William Dunlap wdun...@tibco.com Cc: R help r-help@r-project.org; Omphalodes Verna omphalodes.ve...@yahoo.com Sent: Wednesday, November 7, 2012 8:38 PM Subject: RE: [R] row index for max values of row groups Note that the unlist(tapply()) algorithm depends on the groups column being in order. Here is one that works no matter how the data frame is ordered. which( with(df1, {tmp - logical(length(groups)) ; split(tmp, groups) - lapply(split(values, groups), function(x)x==max(x)) ; tmp})) [1] 4 8 11 df1[.Last.value, ] groups values 4 1 2 8 2 3 11 3 4 Try reordering the data.frame and you get essentially the same result: df1a - df1[c(12,1,11,2,10,3,9,4,8,5,7,6),] which( with(df1a, {tmp - logical(length(groups)) ; split(tmp, groups) - lapply(split(values, groups), function(x)x==max(x)) ; tmp})) [1] 3 8 9 df1a[.Last.value, ] groups values 11 3 4 4 1 2 8 2 3 Where unlist(tapply()) gives: which(unlist(tapply(df1a$values, df1a$groups, FUN=function(x) x == max(x)), use.names=FALSE)) [1] 4 6 10 df1a[.Last.value, ] groups values 2 1 1 3 1 1 5 1 1 You could sort the data.frame and unsort the result from the tapply approach: ord - with(df1a, order(groups)) with(df1a[ord,], which(unlist(tapply(values, groups, FUN=function(x) x == max(x)), use.names=FALSE)[order(ord)])) [1] 3 8 9 df1a[.Last.value, ] groups values 11 3 4 4 1 2 8 2 3 (The split()-split() is close to what ave() does, but ave() requires that the first argument to FUN have the same type as FUN's output but we want a numeric input and a logical output. Perhaps ave() could use a new argument to handle this kind of thing.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Rui Barradas Sent: Wednesday, November 07, 2012 11:13 AM To: arun Cc: R help; Omphalodes Verna Subject: Re: [R] row index for max values of row groups Hello, Though my function is equal to Arun's, it's wrapped by a different way of returning the index. which(unlist(tapply(df1$values, df1$groups, FUN=function(x) x == max(x Hope this helps, Rui Barradas Em 07-11-2012 18:54, arun escreveu: Hi, One method will be: row.names(df1[unlist(tapply(df1$values,df1$groups,FUN=function(x) x==max(x))),]) #[1] 4 8 11 #or row.names(df1[as.logical(ave(df1$values,df1$groups,FUN=function(x) x==max(x))),]) #[1] 4 8 11 A.K. - Original Message - To: r-help@r-project.org r-help@r-project.org Cc: Sent: Wednesday, November 7, 2012 1:41 PM Subject: [R] row index for max values of row groups [[elided Yahoo spam]] I am looking for ''nice solution'' for (maybe) simple problem. I need a code (small program) to calculate row index for max value (example below: df1$values) by groups (example below: df1$groups). df1 - data.frame( groups = c(1,1,1,1,1,2,2,2,3,3,3,3), values = c(1,1,1,2,1,1,2,3,2,1,4,3) ) df1 expected results 4 8 11 # row index of max values by group Thansk to all for help, OV [[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. __ 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] row index for max values of row groups
Dear list members! I am looking for ''nice solution'' for (maybe) simple problem. I need a code (small program) to calculate row index for max value (example below: df1$values) by groups (example below: df1$groups). df1 - data.frame( groups = c(1,1,1,1,1,2,2,2,3,3,3,3), values = c(1,1,1,2,1,1,2,3,2,1,4,3) ) df1 expected results 4 8 11 # row index of max values by group Thansk to all for help, OV [[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] mapply instead for loop
Thanks for help. But, I am surprised, that mapply is slower than for loop? OV From: Uwe Ligges lig...@statistik.tu-dortmund.de Cc: r-help@r-project.org r-help@r-project.org Sent: Saturday, November 3, 2012 4:32 PM Subject: Re: [R] mapply instead for loop On 30.10.2012 20:01, Omphalodes Verna wrote: Hi all! My question in about using mapply instead for loop. Below is a example with for loop: Is it posible to give same results with mapply function? Thanks for help! OV x - 1:10 y - 1:10 xyz - data.frame(expand.grid(x,y)[1], expand.grid(x,y)[2], z = rnorm(100)) names(xyz) - c(x, y, z) head(xyz) size - 2 output - NULL ### for loop for(i in 1:dim(xyz)[1]){ x0 - xyz[i, x] y0 - xyz[i, y] xyzSel - xyz[xyz$x = (x0 - size) xyz$x (x0 + size) xyz$y = (y0 - size) xyz$y (y0 + size), ] output[i] - min(xyzSel$z) } output Yes: output - mapply(function(x0, y0) min(xyz[(xyz$x = (x0 - size) xyz$x (x0 + size)) (xyz$y = (y0 - size) xyz$y (y0 + size)), z]), xyz$x, xyz$y) Uwe Ligges [[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.
[R] mapply instead for loop
Hi all! My question in about using mapply instead for loop. Below is a example with for loop: Is it posible to give same results with mapply function? Thanks for help! OV x - 1:10 y - 1:10 xyz - data.frame(expand.grid(x,y)[1], expand.grid(x,y)[2], z = rnorm(100)) names(xyz) - c(x, y, z) head(xyz) size - 2 output - NULL ### for loop for(i in 1:dim(xyz)[1]){ x0 - xyz[i, x] y0 - xyz[i, y] xyzSel - xyz[xyz$x = (x0 - size) xyz$x (x0 + size) xyz$y = (y0 - size) xyz$y (y0 + size), ] output[i] - min(xyzSel$z) } output [[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] chisq.test
Dear all! Thanks for clarification. OV To: Rolf Turner rolf.tur...@xtra.co.nz Sent: Wednesday, June 27, 2012 1:33 PM Subject: Re: [R] chisq.test Hi Rolf, Thanks for spotting the mistake. A.K. - Original Message - From: Rolf Turner rolf.tur...@xtra.co.nz .org Sent: Wednesday, June 27, 2012 12:58 AM Subject: Re: [R] chisq.test On 27/06/12 08:54, arun wrote: Hi, The error is due to less than 5 observations in some cells. NO, NO, NO It's not the observations that matter, it is the ***EXPECTED COUNTS***. These must all be at least 5 in order for the null distribution of the test statistic to be adequately approximated by a chi-squared distribution. cheers, Rolf Turner You can try, fisher.test(tabele) Fisher's Exact Test for Count Data data: tabele p-value = 0.0998 alternative hypothesis: two.sided A.K. - Original Message - To: r-help@r-project.org r-help@r-project.org Cc: Sent: Tuesday, June 26, 2012 2:27 PM Subject: [R] chisq.test Dear list! I would like to calculate chisq.test on simple data set with 70 observations, but the output is ''Warning message:'' Warning message: In chisq.test(tabele) : Chi-squared approximation may be incorrect Here is an example: tabele - matrix(c(11, 3, 3, 18, 3, 6, 5, 21), ncol = 4, byrow = TRUE) dimnames(tabela) - list( SEX = c(M,F), HAIR = c(Brown, Black, Red, Blonde)) addmargins(tabele) prop.table(tabele) chisq.test(tabele) Please, give me an advice / suggestion / recommendation. Thanks a lot to all, OV [[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. [[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] chisq.test
Dear list! I would like to calculate chisq.test on simple data set with 70 observations, but the output is ''Warning message:'' Warning message: In chisq.test(tabele) : Chi-squared approximation may be incorrect Here is an example: tabele - matrix(c(11, 3, 3, 18, 3, 6, 5, 21), ncol = 4, byrow = TRUE) dimnames(tabela) - list( SEX = c(M,F), HAIR = c(Brown, Black, Red, Blonde)) addmargins(tabele) prop.table(tabele) chisq.test(tabele) Please, give me an advice / suggestion / recommendation. Thanks a lot to all, OV [[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] match values from data.frame and vector
Dear R helpers! I have a vector 'x1' and data.frame 'df1'. Do you have any suggestion how to get vector x2, which will be a result of matching values from vector 'x1' and values from 'df1'? Please, see the example: x1 - c(rep(1,3), rep(NA,2), rep(2,4)) df1 - data.frame(c1 = c(1,2), c2 = c(5,6)) I would like to get vector x2: x2 [1] 5 5 5 NA NA 6 6 6 6 Thanks a lot, OV [[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] rgeos - gBuffer, width by z-value
Dear list! I have problem with buffer size (width) in package rgeos. I would like to expand given geometry (points) to specified width based on the z value from attribute table. Here is example: point - data.frame(x=c(10,20), y=c(10, 10), z = c(2,7)) point_shp - SpatialPointsDataFrame(point[,1:2],point) plot(point_shp, xlim = c(0,30), ylim = c(0,20)) plot(gBuffer(point_shp, width = 5, byid=TRUE, capStyle=ROUND), add = TRUE, col = red) plot(gBuffer(point_shp, width = point_shp@data$z, byid=TRUE, capStyle=ROUND), add = TRUE, col = green) 'gBuffer' function ''take into account'' always only first line/cell from table. Any suggestion? Thanks a lot, OV [[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] rgeos - gBuffer, width by z-value
Thanks for help... do anybody also have an idea how to export object l (which is list class) to .*shp file. I use SpatialPolygons, but output in console is : Error in SpatialPolygons(list(l)) : cannot get a slot (area) from an object of type list Cheers, OV From: Michael Sumner mdsum...@gmail.com Cc: r-help@r-project.org r-help@r-project.org Sent: Saturday, April 7, 2012 2:54 PM Subject: Re: [R] rgeos - gBuffer, width by z-value Just use a loop and collect the results in a list. BTW, R-Sig-Geo is a special interest group mailing list specifically for these sorts of questions regarding sp and rgeos packages (which you did not mention). library(sp) library(rgeos) point - data.frame(x=c(10,20), y=c(10, 10), z = c(2,7)) point_shp - SpatialPointsDataFrame(point[,1:2],point) l - vector(list, nrow(point_shp)) for (i in seq_len(nrow(point_shp))) { l[[i]] - gBuffer(point_shp[i,], width = point_shp[i,]$z, byid=TRUE, capStyle=ROUND) } plot(point_shp, xlim = bbox(point_shp)[1,] + c(-1, 1) * max(point_shp$z), ylim = bbox(point_shp)[2,] + c(-1, 1) * max(point_shp$z)) invisible(lapply(l, plot, add = TRUE)) Cheers, Mike. On Sat, Apr 7, 2012 at 9:14 PM, Omphalodes Verna Dear list! I have problem with buffer size (width) in package rgeos. I would like to expand given geometry (points) to specified width based on the z value from attribute table. Here is example: point - data.frame(x=c(10,20), y=c(10, 10), z = c(2,7)) point_shp - SpatialPointsDataFrame(point[,1:2],point) plot(point_shp, xlim = c(0,30), ylim = c(0,20)) plot(gBuffer(point_shp, width = 5, byid=TRUE, capStyle=ROUND), add = TRUE, col = red) plot(gBuffer(point_shp, width = point_shp@data$z, byid=TRUE, capStyle=ROUND), add = TRUE, col = green) 'gBuffer' function ''take into account'' always only first line/cell from table. Any suggestion? Thanks a lot, OV [[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. -- Michael Sumner Institute for Marine and Antarctic Studies, University of Tasmania Hobart, Australia e-mail: mdsum...@gmail.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.
[R] colour by z value, persp in raster package
Hi all! My question is how to colour pixels by z value in persp plot in raster package. Here is an example: x - seq(-1.95, 1.95, length = 30) y - seq(-1.95, 1.95, length = 35) z - outer(x, y, function(a,b) a*b^2) r1 - raster(nrows=35, ncols=30, xmn=0, xmx=30, ymn = 0, ymx = 35) r1[] - c(z) persp(r1) There already exist some function to produce persp plot for anothe classes, but I have no idea how deal with RasterLayer object. Thanks all! OV [[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] colour by z value, persp in raster package
David thanks! Maybe is better ''orginal'' example from raster package: r - raster(system.file(external/test.grd, package=raster)) persp(r) class(r) It is not a problem to colour plot by z value in perspective plot (persp) in package graphic - there is an example in help files: par(bg = white) x - seq(-1.95, 1.95, length = 30) y - seq(-1.95, 1.95, length = 35) z - outer(x, y, function(a,b) a*b^2) nrz - nrow(z) ncz - ncol(z) # Create a function interpolating colors in the range of specified colors jet.colors - colorRampPalette( c(blue, green) ) # Generate the desired number of colors from this palette nbcol - 100 color - jet.colors(nbcol) # Compute the z-value at the facet centres zfacet - z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz] # Recode facet z-values into color indices facetcol - cut(zfacet, nbcol) persp(x, y, z, col=color[facetcol], phi=30, theta=-30) par(op) But question is how specify right sequences of colours (e.g. terrain.colors) for RasterLayer object. The idea of code is persp(r, col = terrain.color(n)), where r is class raster. Thanks all! OV From: David Winsemius dwinsem...@comcast.net Cc: r-help@r-project.org r-help@r-project.org Sent: Tuesday, February 28, 2012 8:47 PM Subject: Re: [R] colour by z value, persp in raster package On Feb 28, 2012, at 4:34 AM, Omphalodes Verna wrote: Hi all! My question is how to colour pixels by z value in persp plot in raster package. Here is an example: x - seq(-1.95, 1.95, length = 30) y - seq(-1.95, 1.95, length = 35) z - outer(x, y, function(a,b) a*b^2) r1 - raster(nrows=35, ncols=30, xmn=0, xmx=30, ymn = 0, ymx = 35) r1[] - c(z) persp(r1) There already exist some function to produce persp plot for anothe classes, but I have no idea how deal with RasterLayer object. Is that really an example? After going to the trouble of correcting the error in this code from failing to load the raster package, I now see that there is no connection between the values of x, y, or x with the raster-call. r1[] - as.matrix(z) Error in .local(x, values) : cannot use a matrix with these dimensions Error in .local(x, i, j, ..., value) : cannot replace values on this raster (it is too large persp(r1) Error: hasValues(x) is not TRUE # Post some useful code. --David Winsemius, MD Heritage Laboratories West Hartford, CT [[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] eliminate rows with all NA values in matrix of special class
Dear list! I have a problem with eliminate rows with all NA values in matrix of special class (my.class). Belowe is a example: #class definition setClass(my.class, representation(ID=character, years=integer, my.mat=matrix)) data.1 - new(my.class, ID = c(tA, tB, tC), years = as.integer(c(2000, 2005, 2010, 2015, 2020)), my.mat = matrix(c(NA, 1, NA, NA, 4, 3, 1, 5, 6, 2, NA, 5, 1, NA, 3), ncol = 3, byrow = TRUE)) data.1 #selection definition setMethod([, c(my.class, numeric, character), function(x, i, j, ..., drop=TRUE) {    IDx - match(j, x@ID)    if (any(is.na(IDx))) stop(invalid 'j')    initialize(x, ID = x@ID[IDx], years = x@years[i], my.mat = x...@my.mat[i, IDx, drop=FALSE])}) setMethod([, c(my.class, missing, character), function(x, i, j, ..., drop=TRUE) {    x[(1:length(x@years)), j, ..., drop=drop] }) setMethod([, c(my.class, missing, numeric), function(x, i, j, ..., drop=TRUE) {    x[, x@ID[j], ..., drop=drop] }) #R output data.1[, c(1,3)] # Extract Part of an Object (column 1 3) An object of class âmy.classâ Slot ID: [1] tA tC Slot years: [1] 2000 2005 2010 2015 2020 Slot my.mat:     [,1] [,2] [1,]  NA  NA [2,]  NA   3 [3,]   1   6 [4,]   2   5 [5,]   1   3 But I would like to get like this: data.1[,c(1,3)] # Extract Part of an Object (column 1 3) An object of class âmy.classâ Slot ID: [1] tA tC Slot years: ### without year 2000 because there is no data (all are NA) [1] 2005 2010 2015 2020 Slot my.mat: ### without first row because there is no data (all are NA)     [,1] [,2] [1,]  NA   3 [2,]   1   6 [3,]   2   5 [4,]   1   3 With 'apply' I define which row has all NA values: which(apply(MATRIX, 1, function(x) all(is.na(x Please give any suggestions. Thanks a lot, OV [[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] setMethod [ - extract by names within Slot
Hi R-helper! I have problem with setMethods for [. Here is example : setClass(myClass, representation(ID.r = numeric, ID.c = character, DAT = matrix)) to.myClass - function(ID.r, ID.c, DAT) { out - new(myClass, ID.r = ID.r, ID.c = ID.c, DAT = DAT) return(out) } setMethod([, myClass, function(x, i, j, drop) { x@ID.r - x@ID.r[i] x@ID.c - x@ID.c[j] out.0 - x@DAT[i,j] out.1 - to.myClass(x@ID.r, x@ID.c, as.matrix(out.0)) return(out.1) }) a - to.myClass(seq(1,25), c(A,A,B,B), matrix(rnorm(100), nrow = 25)) a a[1:20, ] #works a[, 1:3] #works a[1:10, 1:3] #works a[, A] #not works What is solution to write [ methods for extraction by names of Slot ID.c Thanks all. OV [[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] setMethod [ - extract by names within Slot
Thanks Martin. Here is my ''updated'' code. setClass(myClass, representation(ID.r = numeric, ID.c = character, DAT = matrix)) to.myClass - function(ID.r, ID.c, DAT) { out - new(myClass, ID.r = ID.r, ID.c = ID.c, DAT = DAT) return(out) } setMethod([, myClass, function(x, i, j, drop = TRUE) { x@ID.r - x@ID.r[i] x@ID.c - x@ID.c[j] out.0 - x@DAT[i,j] out.1 - to.myClass(x@ID.r, x@ID.c, as.matrix(out.0)) return(out.1) }) setMethod([, c(myClass, ANY, character), function(x, i, j, ..., drop = TRUE) { if(missing(i)) {x@ID.r - x@ID.r} else {x@ID.r - x@ID.r[i]} j - which(j == x@ID.c) x@ID.c - x@ID.c[j] out.0 - x@DAT[i, j] out.1 - to.myClass(x@ID.r, x@ID.c, as.matrix(out.0)) return(out.1) }) a - to.myClass(seq(1,25), c(A,A,B,B), matrix(rnorm(100), nrow = 25)) a a[1:20, ] #works a[, 1:3] #works a[1:10, 1:3] #works a[, A] #works a[5:20, B] #works It works, but Is it normal to write two codes for setMethod??? Nice weekend, OV From: Martin Morgan mtmor...@fhcrc.org Cc: r-help@r-project.org r-help@r-project.org Sent: Saturday, October 22, 2011 3:50 PM Subject: Re: [R] setMethod [ - extract by names within Slot On 10/22/2011 02:11 AM, Omphalodes Verna wrote: Hi R-helper! I have problem with setMethods for [. Here is example : setClass(myClass, representation(ID.r = numeric, ID.c = character, DAT = matrix)) to.myClass- function(ID.r, ID.c, DAT) { out- new(myClass, ID.r = ID.r, ID.c = ID.c, DAT = DAT) return(out) } setMethod([, myClass, function(x, i, j, drop) { x@ID.r- x@ID.r[i] x@ID.c- x@ID.c[j] out.0- x@DAT[i,j] out.1- to.myClass(x@ID.r, x@ID.c, as.matrix(out.0)) return(out.1) }) a- to.myClass(seq(1,25), c(A,A,B,B), matrix(rnorm(100), nrow = 25)) a a[1:20, ] #works a[, 1:3] #works a[1:10, 1:3] #works a[, A] #not works thinking about your code, this is the same as ID.c = c(A,A,B,B) j = A ID.c[j] [1] NA What is solution to write [ methods for extraction by names of Slot ID.c Maybe (untested) setMethod([, c(myClass, ANY, character), function(x, i, j, ..., drop=TRUE) { j = match(j, x@ID.c) x[i, j, ..., drop=TRUE] }) Thanks all. OV [[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. -- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793 [[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] class definition
Hi everybody! I have a matrix of class myClass, for example: myMat - matrix(rnorm(30), nrow = 6) attr(myMat, class) - myClass class(myMat) When I extract part of ''myMat'', the corresponding class ''myClass'' unfortunately disappear: myMat.p - myMat[,1:2] class(myMat.p) Please for any advice / suggestions, how define class, that during an operation does not disappear. Thanks, OV [[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.