[R] interior and exterior colors of a map
Hi useR's, I have an image plot I want to overlay a map of the United States on, via map('usa'). The image is basically a large rectangular of various colors. When I overlay the United States map, the full rectangle of the image plot is visible, but I only want to display the image WITHIN the United States boundary. Does anyone know a way to do this? Thanks in advance, dxc13 -- View this message in context: http://old.nabble.com/interior-and-exterior-colors-of-a-map-tp26507134p26507134.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] Deleting columns from a matrix
useR's, I have a matrix given by the code: mat - matrix(c(rep(NA,10),1,2,3,4,5,6,7,8,9,10,10,9,8,NA,6,5,4,NA,2,1,rep(NA,10),1,2,3,4,NA,6,7,8,9,10),10,5) This is a 10x5 matrix containing missing values. All columns except the second contain missing values. I want to delete all columns that contain ALL missing values, and in this case, it would be the first and fourth columns. Any column that has at least one real number would remain. I know I can use mat[,-1] to delete the first column, but I have a much larger matrix where it is impossible to tell how many columns contain all missing values and which don't. Is there a function or something else that may be able to help me accomplish this? Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/Deleting-columns-from-a-matrix-tp23695656p23695656.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Deleting columns from a matrix
Thanks, both of these methods work great! Dimitris Rizopoulos-4 wrote: one way is: mat - matrix(c(rep(NA,10),1,2,3,4,5,6,7,8,9,10,10,9,8,NA,6,5,4,NA,2,1,rep(NA,10),1,2,3,4,NA,6,7,8,9,10), 10, 5) ind - colSums(is.na(mat)) != nrow(mat) mat[, ind] I hope it helps. Best, Dimitris dxc13 wrote: useR's, I have a matrix given by the code: mat - matrix(c(rep(NA,10),1,2,3,4,5,6,7,8,9,10,10,9,8,NA,6,5,4,NA,2,1,rep(NA,10),1,2,3,4,NA,6,7,8,9,10),10,5) This is a 10x5 matrix containing missing values. All columns except the second contain missing values. I want to delete all columns that contain ALL missing values, and in this case, it would be the first and fourth columns. Any column that has at least one real number would remain. I know I can use mat[,-1] to delete the first column, but I have a much larger matrix where it is impossible to tell how many columns contain all missing values and which don't. Is there a function or something else that may be able to help me accomplish this? Thanks in advance. dxc13 -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ 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. -- View this message in context: http://www.nabble.com/Deleting-columns-from-a-matrix-tp23695656p23696294.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to calculate means of matrix elements
Easy enough. What if some of the matrix elements contained missing values? Then how could you still calculate the means? Example code below: mat1 - matrix(c(1,2,3,4,5,NA,7,8,9),3,3) mat2 - matrix(c(NA,6,1,9,0,5,8,2,7),3,3) mat3 - matrix(c(5,9,1,8,NA,3,7,2,4),3,3) Gabor Grothendieck wrote: Try this: (mat1 + mat2 + mat3) / 3 On Mon, May 18, 2009 at 8:40 PM, dxc13 dx...@health.state.ny.us wrote: useR's, I have several matrices of size 4x4 that I want to calculate means of their respective positions with. For example, consider I have 3 matrices given by the code: mat1 - matrix(sample(1:20,16,replace=T),4,4) mat2 - matrix(sample(-5:15,16,replace=T),4,4) mat3 - matrix(sample(5:25,16,replace=T),4,4) The result I want is one matrix of size 4x4 in which position [1,1] is the mean of position [1,1] of the given three matrices. The same goes for all other positions of the matrix. If these three matrices are given in separate text files, how can I write code that will get this result I need? Thanks in advance, dxc13 -- View this message in context: http://www.nabble.com/how-to-calculate-means-of-matrix-elements-tp23607694p23607694.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. -- View this message in context: http://www.nabble.com/how-to-calculate-means-of-matrix-elements-tp23607694p23615755.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] how to calculate means of matrix elements
useR's, I have several matrices of size 4x4 that I want to calculate means of their respective positions with. For example, consider I have 3 matrices given by the code: mat1 - matrix(sample(1:20,16,replace=T),4,4) mat2 - matrix(sample(-5:15,16,replace=T),4,4) mat3 - matrix(sample(5:25,16,replace=T),4,4) The result I want is one matrix of size 4x4 in which position [1,1] is the mean of position [1,1] of the given three matrices. The same goes for all other positions of the matrix. If these three matrices are given in separate text files, how can I write code that will get this result I need? Thanks in advance, dxc13 -- View this message in context: http://www.nabble.com/how-to-calculate-means-of-matrix-elements-tp23607694p23607694.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] can I plot a matrix of colors?
Hi all, I have a 36x14 matrix of hexadecimal coded colors (see attached file) that was created with the following code: cellcol[is.na(cellcol)] - #00 #Aliceblue cellcol[cellcol 5] - #F0F8FF #Skyblue1 cellcol[cellcol = 5 cellcol 20] - #87CEFF #Blue cellcol[cellcol = 20 cellcol 35] - #FF #Green3 cellcol[cellcol = 35 cellcol 50] - #00CD00 #Yellow2 cellcol[cellcol = 50 cellcol 60] - #00 #Orange2 cellcol[cellcol = 60 cellcol 75] - #EE9A00 #Red cellcol[cellcol = 75 cellcol 90] - #EE #Firebrick cellcol[cellcol = 90] - #B2 My question is, can I plot this matrix using image() or do the colors need to be in a different format? Thank you. dxc13 http://www.nabble.com/file/p23542487/colors.txt colors.txt -- View this message in context: http://www.nabble.com/can-I-plot-a-matrix-of-colors--tp23542487p23542487.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting a grid with color on a map
Thanks, Jim. This seems to be what I am looking for. Just have to fine tune the colors to get some distinctive greens, blues, yellows and oranges in there and I should be good to go. Jim Lemon-2 wrote: It was the NAs that fooled color2D.matplot. This gets your colors, although not exactly what you want. Look at the help for color2D.matplot to get that. I think fiddling with the x and y limits on the map() call will get the positions right. temp1-read.table(time1test.dat,header=TRUE) library(plotrix) # reverse the row order, as color2D.matplot reverses it color2D.matplot(temp1[36:1,],c(0.5,1),c(0.5,0),c(1,0),axes=FALSE) # don't erase the above plot par(new=TRUE) # do a ghost plot with just the axes plot(0,xlim=maplim[1:2],ylim=maplim[3:4],type=n) # add the map on top in black map(add=TRUE,col=black) Jim __ 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. -- View this message in context: http://www.nabble.com/plotting-a-grid-with-color-on-a-map-tp23514804p23521213.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] Overlaying two plots
Hi useR's, I want to overlay an image plot over a world map and I can do it, but just not the way I need to do it. Here is the code I am using (with data file attached) to create my baseline map: library(sp) load(TM_WORLD_BORDERS_SIMPL-0.2.RData) par(bty=l) plot(wrld_simpl, axes = TRUE, ylim = c(-90, 90), xlim=c(-180, 180), asp=1.5) lim - par(usr) abline(v=-180, h=90, col=red) #Image plot needs to be inside this square# abline(v=180, h=-90, col=red) The last two statements create a square on the map between the latitude and longitude limits of the world. I want to place the following image plot (data matrix attached) inside that square region and then overlay the world map again. When I use the following code, the image plot takes up the whole plot screen and is not within the square region and thus when I overlay the world map, the coordinates will be off. df - as.matrix(read.table(kz_time1.txt, header=F)) dff - apply(df, 2, rev) image(t(dff), xlab=, ylab=, axes=FALSE) par(new=TRUE) plot(wrld_simpl, axes = TRUE, ylim = c(-90, 90), xlim=c(-180, 180), asp = 1.5, xlab=Longitude, ylab=Latitude) abline(v=-180, h=90, col=red) abline(v=180, h=-90, col=red) Can anyone help? Thanks in advance. dxc13 http://www.nabble.com/file/p23526662/TM_WORLD_BORDERS_SIMPL-0.2.RData TM_WORLD_BORDERS_SIMPL-0.2.RData http://www.nabble.com/file/p23526662/kz_time1.txt kz_time1.txt -- View this message in context: http://www.nabble.com/Overlaying-two-plots-tp23526662p23526662.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Plotting colors on a world map
So, this would allow me to assign a color to each of the 2592 rectangular grid cells on a map of the world? I guess I am having trouble finding a suitable method of doing this. Also, to be able to distinguish country boundaries from the grid lines (which are both black color), could the grid lines be transparent so that all we see is the map of the world in various colors? I was thinking that 10 degrees would be light blue, 10-30 degrees would be blue, 31-40 be green, 41-50 be yellow, 51-60 be dark yellow, 61-70 be orange, 71-80 be red, 81-90 be dark red. Any help is appreciated in getting this task accomplished. Thanks Duncan Murdoch-2 wrote: On 5/11/2009 10:32 AM, dxc13 wrote: Hi useR's I have created a simple map of the world using the following code: m - map(xlim=c(-180,180), ylim=c(-90,90)) map.axes() I then create a grid of dimension 36x72 using the code: map.grid(m, nx=72, ny=36, labels=FALSE, col=black) This gives 2592 grid cells. In a separate data set of dimension 36x72, I have 2592 temperature values (some missing values are present) ranging from -20 degrees F to 90 degrees F. My question is, how can I create a reasonable color scheme (low temperatures in light blue to higher temperatures in dark red) and plot the temperature colors in their respective grid cells on the map? Take this data matrix as some example data: T - sample(-10:90, 2592, replace=TRUE) mat - matrix(T, nrow=36, ncol=72) The colorspace package can make nice color sequences for this sort of thing, and image() can add them to a plot. In colorspace, look at the examples for diverge_hcl. Duncan Murdoch __ 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. -- View this message in context: http://www.nabble.com/Plotting-colors-on-a-world-map-tp23484524p23495370.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] plotting a grid with color on a map
Hi all, I have posted similar questions regarding this topic, but I just can't seem to get over the hump and find a straightforward way to do this. I have attached my file as a reference. Basically, the attached file is a 5 degree by 5 degree grid of the the world (2592 cells), most of them are NA's. I just want to be able to plot this grid over a world map and color code the cells. For example, if a cell has a temperature less than 20 degrees it will be blue, 21 to 50 green color, 51-70 orange, 71+ red colored cells. For any NAs, they should be colored white. I know how to create a map of the world using map() and add a grid to it using map.grid(), but I can't color code the cells the way I need. Is there a way to do this in R? Thanks again. dxc13 http://www.nabble.com/file/p23514804/time1test.txt time1test.txt -- View this message in context: http://www.nabble.com/plotting-a-grid-with-color-on-a-map-tp23514804p23514804.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] Plotting colors on a world map
Hi useR's I have created a simple map of the world using the following code: m - map(xlim=c(-180,180), ylim=c(-90,90)) map.axes() I then create a grid of dimension 36x72 using the code: map.grid(m, nx=72, ny=36, labels=FALSE, col=black) This gives 2592 grid cells. In a separate data set of dimension 36x72, I have 2592 temperature values (some missing values are present) ranging from -20 degrees F to 90 degrees F. My question is, how can I create a reasonable color scheme (low temperatures in light blue to higher temperatures in dark red) and plot the temperature colors in their respective grid cells on the map? Take this data matrix as some example data: T - sample(-10:90, 2592, replace=TRUE) mat - matrix(T, nrow=36, ncol=72) Thanks in advance, dxc13 -- View this message in context: http://www.nabble.com/Plotting-colors-on-a-world-map-tp23484524p23484524.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] A variation on the bar plot
Hi all, I cannot think of the technical name of this plot, but I want to create a plot with the data below that looks like two back-to-back horizontal bar plots. Ideally, there would be a vertical line in the center of the plot at zero, and on the right hand side would be 4 bars representing the values for 2007, and on the left side of the vertical line would be the corresponding values for 2005. Even though values would be on the left side of the zero line, it doesn't mean they are negative, I just want to compare changes in time. data 2005 2007 11.42 11.76 11.93 11.97 4.06 4.53 4.29 4.47 Hope this is clear, Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/A-variation-on-the-bar-plot-tp23374466p23374466.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] gridding values in a data frame
Hi all, I have a data frame that looks like such: LATITUDE LONGITUDE TEMPERATURE TIME 36.73 -176.4358.32 1 50.9590.0074.39 1 -30.425.4523.26 1 15.81 -109.3152.44 1 -80.75-144.9566.19 2 90.00 100.5537.50 2 65.41 -4.49 29.83 2 and this goes on for a A LOT more records, until time=1200 I want to create a 5 degree by 5 degree grid of this data, with the value of temperature in the appropriate grid cell. I want one grid for each time value. For each time value, this works out to be a 36x72 grid with 2592 cells because the longitude spans -180 to 180 and latitude spans 90 to -90 and they would be in increments of 5 degrees. If there are no temperatures available to be put into a grid cell, than that cell should get a missing value, NA, put into it. Also, could the gridded result for each time be written to a text file before processing the next time value? Hope this is clear. Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/gridding-values-in-a-data-frame-tp23319190p23319190.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] gridding values in a data frame
Thank you for your input. I will give it a try and see how it works out. I always have the same problem when programming...I always make things more complicated than they really are :-). Much appreciated. jdeisenberg wrote: dxc13 wrote: Hi all, I have a data frame that looks like such: LATITUDE LONGITUDE TEMPERATURE TIME 36.73 -176.4358.32 1 and this goes on for a A LOT more records, until time=1200 I want to create a 5 degree by 5 degree grid of this data, with the value of temperature in the appropriate grid cell. I want one grid for each time value. dxc13 The following appears to work, but is most definitely *not* in the spirit of R -- it's more like a C program written in R. Undoubtedly someone will come up with a much more clever method. (This is always my problem; I come up with a workable solution, but it's not elegant.) Note: the grid has to have 37 rows, since latitude -90 to +90 (inclusive) takes 37 5-degree increments; since -180 and +180 longitude are the same, you would never have them as separate numbers. d - read.csv(weather.csv) for (i in 1:1200) { x - d[d$TIME==i,] if (length(x$TIME) 0) { print(sprintf(# of elements for time %d: %d, i, length(x$TIME))) grid - matrix(NA, nrow=37, ncol=72) for (j in 1:length(x$TIME)) { lat - 1 + trunc((90 + x$LATITUDE[j]) / 5) long - 1 + trunc((180 + x$LONGITUDE[j]) / 5) grid[lat,long] - x$TEMPERATURE[j] } write(t(grid), file=sprintf(time%d.csv, i), ncolumns=72, sep=,) } } -- View this message in context: http://www.nabble.com/gridding-values-in-a-data-frame-tp23319190p23325394.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] how to cite R data sets
Hi, As part of a project I am doing, I am using the airquality data set that comes built into R as a means of showing an example of how my analysis will be carried out. I know that citation(package) will produce a citation for any R package, but is there a proper way to cite this data set? Thanks, ~D -- View this message in context: http://www.nabble.com/how-to-cite-R-data-sets-tp20325205p20325205.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] smoothing with the Gaussian kernel
useR's, Does anyone know what function or package allows one to specify use of the Gaussian kernel to smooth a data set, or is this just the default for most functions? Thanks, dxc13 -- View this message in context: http://www.nabble.com/smoothing-with-the-Gaussian-kernel-tp19323294p19323294.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] how to draw a perspective pyramid
Dear list, I am trying to construct a perspective plot of a 2D triangle, or pyramid. I can easily do the one dimensional plot (code below), but I can't figure out how to do it in a perspective plot for two input variables. Does anyone have a simple solution? Thanks! Derek #code for a triangle u - seq(-1,1,.001) tri - (1-abs(u)) plot(tri~u,type=l,main=Second Iteration Kernel: Two Input Variables,lwd=2, xlab=x,ylab=y,col=blue) -- View this message in context: http://www.nabble.com/how-to-draw-a-perspective-pyramid-tp19247124p19247124.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 with persp function
Dear List, I am trying to draw a rectangular plane using the persp function, however I can't seem to get it to work. I want the length along x1 axis to be between 1 and 3 and the length along the x2 axis to be between 1 and 4. All z values for x1 should be equal (say, z=1) because this is a plane, not a cube. Below is my current code. Any help is appreciated Thanks in advance dxc13 x - seq(from=1,to=3,by=.1) y - seq(1,4,by=.1) f - array(1,dim=c(21,1)) z - as.matrix(cbind(x,y,f)) persp(x=x,y=y,z) -- View this message in context: http://www.nabble.com/help-with-%22persp%22-function-tp19235739p19235739.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] help with persp function
Yes, this works nicely. Thank you! jholtman wrote: Is this what you want to do: x - seq(from=1,to=3,by=.1) y - seq(1,4,by=.1) z - matrix(1, length(x), length(y)) persp(x=x,y=y,z,zlim=c(0,2)) On Sat, Aug 30, 2008 at 2:08 PM, dxc13 [EMAIL PROTECTED] wrote: Dear List, I am trying to draw a rectangular plane using the persp function, however I can't seem to get it to work. I want the length along x1 axis to be between 1 and 3 and the length along the x2 axis to be between 1 and 4. All z values for x1 should be equal (say, z=1) because this is a plane, not a cube. Below is my current code. Any help is appreciated Thanks in advance dxc13 x - seq(from=1,to=3,by=.1) y - seq(1,4,by=.1) f - array(1,dim=c(21,1)) z - as.matrix(cbind(x,y,f)) persp(x=x,y=y,z) -- View this message in context: http://www.nabble.com/help-with-%22persp%22-function-tp19235739p19235739.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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ 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. -- View this message in context: http://www.nabble.com/help-with-%22persp%22-function-tp19235739p19239104.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] finding a faster way to do an iterative computation
useR's, I am trying trying to find out if there is a faster way to do a certain computation. I have successfully used FOR loops and the apply function to do this, but it can take some time to fully compute, but I was wondering if anyone may know of a different function or way to do this: x [1] 1 2 3 4 5 xk [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 I want to do: abs(x-xk[i]) where i = 1 to length(xk)=13. It should result in a 13 by 5 matrix or data frame. Does anyone have a quicker solution than FOR loops or apply()? Much appreciation and thanks, dxc13 -- View this message in context: http://www.nabble.com/finding-a-faster-way-to-do-an-iterative-computation-tp18718233p18718233.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] finding a faster way to do an iterative computation
Thank you to all who applied. These all seem to work the way I want them to. The outer function seems really useful, I probably could use that for a lot of my work. Thanks! Rolf Turner-3 wrote: On 30/07/2008, at 6:12 AM, dxc13 wrote: useR's, I am trying trying to find out if there is a faster way to do a certain computation. I have successfully used FOR loops and the apply function to do this, but it can take some time to fully compute, but I was wondering if anyone may know of a different function or way to do this: x [1] 1 2 3 4 5 xk [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 I want to do: abs(x-xk[i]) where i = 1 to length(xk)=13. It should result in a 13 by 5 matrix or data frame. Does anyone have a quicker solution than FOR loops or apply()? outer(xk,x,function(a,b){abs(a-b)}) ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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. -- View this message in context: http://www.nabble.com/finding-a-faster-way-to-do-an-iterative-computation-tp18718233p18723369.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] simple random number generation
useR's, I want to randomly generate 500 numbers from the standard normal distribution, i.e. N(0,1), but I only want them to be generated in the range -1.5 to 1.5. Does anyone have a simple way to do this? Thanks, dxc13 -- View this message in context: http://www.nabble.com/simple-random-number-generation-tp18642611p18642611.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] including data frames in R packages
useR's, Does any one know if there is a size limitation on the data frames that can be included in R packages. I have a data set in a text file that I would like to include in a package I am building and it is 8.5 MB in size. Will this be problematic? Is the process for including data sets in packages documented in WRE? Thanks, dxc -- View this message in context: http://www.nabble.com/including-data-frames-in-R-packages-tp15672705p15672705.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] applying a function to data frame columns
useR's, I want to apply this function to the columns of a data frame: u[u = range(v)[1] u = range(v)[2]] where u is the n column data frame under consideration and v is a data frame of values with the same number of columns as u. For example, v1 - c(1,2,3) v2 - c(3,4,5) v3 - c(2,3,4) v - as.data.frame(cbind(v1,v2,v3)) uk1 - seq(min(v1) - .5, max(v1) + .5, .5) uk2 - seq(min(v2) - .5, max(v2) + .5, .5) uk3 - seq(min(v3) - .5, max(v3) + .5, .5) u - do.call(expand.grid, list(uk1,uk2,uk3)) Here, there are 3 columns; instead of hard-coding this, can the function given above, which will restrict the u data frame to values within the ranges of each variable, be done with the apply function? Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/applying-a-function-to-data-frame-columns-tp15619657p15619657.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] intersecting rows of a matrix
useR's, First, I would like to say thanks to John Fox for providing this segment of code to perform intersection for multiple sets: intersection - function(x, y, ...){ if (missing(...)) intersect(x, y) else intersect(x, intersection(y, ...)) } I want to execute this function on the rows of a matrix I have: Ik.mat.test - matrix(c(2,3,6,1,2,6,6,1,2),byrow=T,nrow=3) Ik.mat.test [,1] [,2] [,3] [1,]236 [2,]126 [3,]612 I need to find a way to run the intersection function on the rows of this matrix. The result should be a vector containing 2, 6. This works, but I want to find a way to do this for any size matrix. intersection(Ik.mat.test[1,],Ik.mat.test[2,], Ik.mat.test[3,]) So, if the user supplied a matrix with any number of rows, I would like to be able to run this function. Any ideas? I have tried something like do.call(intersection, list(Ik.mat.test[1,]: Ik.mat.test[3,])) but this doesnt work Thanks in advance. Derek -- View this message in context: http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15601658.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] intersecting rows of a matrix
Thanks Gabor, that works great! Gabor Grothendieck wrote: Try this: do.call(intersection, unname(as.data.frame(t(mat On Wed, Feb 20, 2008 at 6:45 PM, dxc13 [EMAIL PROTECTED] wrote: useR's, First, I would like to say thanks to John Fox for providing this segment of code to perform intersection for multiple sets: intersection - function(x, y, ...){ if (missing(...)) intersect(x, y) else intersect(x, intersection(y, ...)) } I want to execute this function on the rows of a matrix I have: Ik.mat.test - matrix(c(2,3,6,1,2,6,6,1,2),byrow=T,nrow=3) Ik.mat.test [,1] [,2] [,3] [1,]236 [2,]126 [3,]612 I need to find a way to run the intersection function on the rows of this matrix. The result should be a vector containing 2, 6. This works, but I want to find a way to do this for any size matrix. intersection(Ik.mat.test[1,],Ik.mat.test[2,], Ik.mat.test[3,]) So, if the user supplied a matrix with any number of rows, I would like to be able to run this function. Any ideas? I have tried something like do.call(intersection, list(Ik.mat.test[1,]: Ik.mat.test[3,])) but this doesnt work Thanks in advance. Derek -- View this message in context: http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15601658.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. -- View this message in context: http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15602494.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] intersecting rows of a matrix
Thank you, that also works perfectly! I am not sure which method works more efficiently, but they are both instantaneous and give the correct result. Thanks Derek Henrique Dallazuanna wrote: lk.list-split(lk.mat.test,1:nrow(lk.mat.test)) names(lk.list)-NULL do.call(intersection,lk.list) 2008/2/20, dxc13 [EMAIL PROTECTED]: useR's, First, I would like to say thanks to John Fox for providing this segment of code to perform intersection for multiple sets: intersection - function(x, y, ...){ if (missing(...)) intersect(x, y) else intersect(x, intersection(y, ...)) } I want to execute this function on the rows of a matrix I have: Ik.mat.test - matrix(c(2,3,6,1,2,6,6,1,2),byrow=T,nrow=3) Ik.mat.test [,1] [,2] [,3] [1,]236 [2,]126 [3,]612 I need to find a way to run the intersection function on the rows of this matrix. The result should be a vector containing 2, 6. This works, but I want to find a way to do this for any size matrix. intersection(Ik.mat.test[1,],Ik.mat.test[2,], Ik.mat.test[3,]) So, if the user supplied a matrix with any number of rows, I would like to be able to run this function. Any ideas? I have tried something like do.call(intersection, list(Ik.mat.test[1,]: Ik.mat.test[3,])) but this doesnt work Thanks in advance. Derek -- View this message in context: http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15601658.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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ 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. -- View this message in context: http://www.nabble.com/intersecting-rows-of-a-matrix-tp15601658p15603158.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] extending code to handle more variables
useR's, Consider the variables defined below: yvals - c(25,30,35) x1 - c(1,2,3) x2 - c(3,4,5) x3 - c(6,7,8) x - as.data.frame(cbind(x1,x2,x3)) delta - c(2.5, 1.5, 0.5) h - delta/2 vars - 3 xk1 - seq(min(x1)-0.5, max(x1)+0.5, 0.5) xk2 - seq(min(x2)-0.5, max(x2)+0.5, 0.5) xk3 - seq(min(x3)-0.5, max(x3)+0.5, 0.5) xks - list(xk1,xk2,xk3) xk - do.call(expand.grid, xks) I want to perform several calculations with these variables. For only two variables, my code is successful, however, here there are three variables (x1,x2,x3) and there could really be n variables (x1,...xn) if need be. Below is code that works for two variables, I want to convert it to work for any number of variables. Any ideas? yk - numeric(nrow(xk)) for (j in 1:nrow(xk)) { w1 - abs(x[,1] - xk$x1[j]) w2 - abs(x[,2] - xk$x2[j]) Ik1 - which(w1 = h[1]) Ik2 - which(w2 = h[2]) Ik - intersect(Ik1, Ik2) YIk - yvals[Ik] yk[j] - mean(YIk) } The result is the one-dimensional vector yk. Does anyone know how to extend this code to cover any number of variables? Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/extending-code-to-handle-more-variables-tp15604234p15604234.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] re stricting points in a data frame
useR's, Consider some variables and a data frame of points: x1 - c(1,2,3) x2 - c(3,4,5) xk1 - seq(min(x1)-.5, max(x1)+.5,.5) xk2 - seq(min(x2)-.5, max(x2)+.5,.5) expand.grid(xk1=xk1,xk2=xk2) xk1 xk2 1 0.5 2.5 2 1.0 2.5 3 1.5 2.5 4 2.0 2.5 5 2.5 2.5 6 3.0 2.5 7 3.5 2.5 ... 46 2.0 5.5 47 2.5 5.5 48 3.0 5.5 49 3.5 5.5 I want to restrict the data frame to only contain points that are within the x1 and x2 range. So for this example, only points between 1 and 3 should be kept for x1 and values between 3 and 5 should be kept for x2. This condition needs to be satisfied for both points, not just one of them; i.e., if an x1 point satisfies this, but its corresponding x2 point does not, then this point should be removed. Does anyone know a way to do this, in general, for any data frame? Hope this is not confusing. Thanks in advance dxc13 -- View this message in context: http://www.nabble.com/restricting-points-in-a-data-frame-tp15184381p15184381.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 with levelplot()
useR's, I want to create a movie of a sin function (from 0 to pi/2) using levelplot() in the lattice package. I basically want to create 20 or so plots of the sin function starting with an amplitude of 0 and ending at amplitude 1. By using a loop and plotting these in succession, it will have the appearance of a movie or animation. I believe I know how to do this part, but I am having trouble working with the colors. I want to use a range of colors starting with light blue (denoting zero) and progressively getting darker as the amplitude gets closer to 1 (red color). Does anyone know a way to do this? Thanks in advance -- View this message in context: http://www.nabble.com/help-with-levelplot%28%29-tp15033748p15033748.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] color ranges on a 2D plot
useR's I am trying to color the points on a scatter plot (code below) with two colors. Red for values 0.5 -1.0 and blue for 0.0 - .49. Does anyone know a easy way to do this? x - runif(100, 0, 1) y - runif(100, 0, 1) plot(y ~ x, pch=16) Thanks, dxc13 -- View this message in context: http://www.nabble.com/color-ranges-on-a-2D-plot-tp14893457p14893457.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] matching values in a list
useR's, I want to match the real number elements of a list that has 3 matrices as its elements and then average those numbers. I think I am close, but I can't get it to quite work out. For example, a - list(matrix(c(10,NA,NA,12,11, 10,13,NA,14,12),ncol=2),matrix(c(10,12,15,13,11, 13,NA,NA,12,10),ncol=2),matrix(c(10,15,NA,13,NA, 13,12,NA,NA,10),ncol=2)) a [[1]] [,1] [,2] [1,] 10 10 [2,] NA 13 [3,] NA NA [4,] 13 14 [5,] 11 12 [[2]] [,1] [,2] [1,] 10 13 [2,] 12 NA [3,] 15 NA [4,] 13 12 [5,] 11 10 [[3]] [,1] [,2] [1,] 10 13 [2,] 15 12 [3,] NA NA [4,] 13 NA [5,] NA 10 I want to average the non-NA numbers that simultaneously appear in each column of all 3 matrices. For the first column, 10 and 13 are the only numbers that appear in all 3 matrices. Thus, I want to take the average of 10 and 13. Then repeat the same process for the second column. The data here is only a sample, but with the data I work with, there are many more columns in each matrix in the list. Does anyone know a efficient way to do this? Maybe using lapply()? Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/matching-values-in-a-list-tp14767170p14767170.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] matching values in a list
That's a very creative way to get the results! I haven't used the table call before, but that certainly made it much easier. Thanks! Gabor Grothendieck wrote: The first line was missing. It should be: mat - do.call(rbind, a) sapply(apply(mat, 2, table), function(x) mean(as.numeric(names(x))[x == length(a)])) On Jan 11, 2008 7:23 PM, Gabor Grothendieck [EMAIL PROTECTED] wrote: Assume that no component matrix can have repeated values. First we apply table to mat yielding a list of named counts. To each component of that we sapply the indicated function which picks out those that occur length(a) times (so they are in every column), converts the name to numeric and averages them. sapply(apply(mat, 2, table), function(x) mean(as.numeric(names(x))[x == length(a)])) By the way, note that the a you define is slightly different from the a displayed in your post. On Jan 11, 2008 6:09 PM, dxc13 [EMAIL PROTECTED] wrote: useR's, I want to match the real number elements of a list that has 3 matrices as its elements and then average those numbers. I think I am close, but I can't get it to quite work out. For example, a - list(matrix(c(10,NA,NA,12,11, 10,13,NA,14,12),ncol=2),matrix(c(10,12,15,13,11, 13,NA,NA,12,10),ncol=2),matrix(c(10,15,NA,13,NA, 13,12,NA,NA,10),ncol=2)) a [[1]] [,1] [,2] [1,] 10 10 [2,] NA 13 [3,] NA NA [4,] 13 14 [5,] 11 12 [[2]] [,1] [,2] [1,] 10 13 [2,] 12 NA [3,] 15 NA [4,] 13 12 [5,] 11 10 [[3]] [,1] [,2] [1,] 10 13 [2,] 15 12 [3,] NA NA [4,] 13 NA [5,] NA 10 I want to average the non-NA numbers that simultaneously appear in each column of all 3 matrices. For the first column, 10 and 13 are the only numbers that appear in all 3 matrices. Thus, I want to take the average of 10 and 13. Then repeat the same process for the second column. The data here is only a sample, but with the data I work with, there are many more columns in each matrix in the list. Does anyone know a efficient way to do this? Maybe using lapply()? Thanks in advance. dxc13 -- View this message in context: http://www.nabble.com/matching-values-in-a-list-tp14767170p14767170.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. -- View this message in context: http://www.nabble.com/matching-values-in-a-list-tp14767170p14768583.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] changing the values in a list
useR's, Does anyone know of an efficient way to change the values in a list to different numbers stored in an object? For example, consider the following: y - matrix(c(20, 13, 25, 10, 17), ncol = 1) res [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA 1.25 0.25 0.75 NA NA NANANANA 1.25 0.25 [2,] 1.25 0.25 0.75 NA NA NA NA NA NA 1.25 0.25 0.75NA NA [3,] NA NA NA NA NA NA 1.25 0.25 0.75NANANANA NA [4,] NA NA 1.25 0.25 0.75 NA NA NA NANANA 1.25 0.25 0.75 [5,] NA NA NA NA NA 1.25 0.25 0.75 NANANANANA NA [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA NA NA NA NA NA NANANANANA NA [2,] NA NA NA NA NA NA NA NA NANANANANA NA [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0.25 0.25 0.25 0.25 0.25 [4,] NA NA NA NA NA NA NA NA NANANANANA NA [5,] NA NA NA NA NA NA NA NA NANANANANA NA What I want to do is change the values in each column of the list that are not NA to their corresponding Y values in the matrix given. So any real number in the first position of any column in the list should be changed to 20, the second number would be changed to 13, and so on... Does anyone know an easy way to do this? Thanks in advance. Derek -- View this message in context: http://www.nabble.com/changing-the-values-in-a-list-tp14749183p14749183.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] changing the values in a list
I will give it a try, thanks for your input! Peter McMahan wrote: On Jan 10, 2008, at 9:48 PM, Peter McMahan wrote: y - matrix(rep(y,ncol(x)),nrow=nrow(y)) x[is.na(x)] - y[is.na(x)] oops, i meant: y - matrix(rep(y,ncol(res)),nrow=nrow(y)) res[is.na(res)] - y[is.na(res)] __ 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. -- View this message in context: http://www.nabble.com/changing-the-values-in-a-list-tp14749183p14749502.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] using lapply()
useR's, I am trying to find a quick way to change some values in a list that are subject to a condition to be NA. Consider the 3x1 matrix: delta - matrix(c(2.5,2.5,1), nrow = 1) And consider the list named v that has 3 elements v v[[1]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 4.25 3.25 2.25 1.25 0.25 0.75 1.75 2.75 3.75 4.25 3.25 2.25 1.25 0.25 [2,] 1.25 0.25 0.75 1.75 2.75 3.75 4.75 5.75 6.75 1.25 0.25 0.75 1.75 2.75 [3,] 7.25 6.25 5.25 4.25 3.25 2.25 1.25 0.25 0.75 7.25 6.25 5.25 4.25 3.25 [4,] 3.25 2.25 1.25 0.25 0.75 1.75 2.75 3.75 4.75 3.25 2.25 1.25 0.25 0.75 [5,] 6.25 5.25 4.25 3.25 2.25 1.25 0.25 0.75 1.75 6.25 5.25 4.25 3.25 2.25 v[[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 9.25 9.25 9.25 9.25 9.25 9.25 9.25 9.25 9.25 8.25 8.25 8.25 8.25 8.25 [2,] 7.25 7.25 7.25 7.25 7.25 7.25 7.25 7.25 7.25 6.25 6.25 6.25 6.25 6.25 [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0.25 0.25 0.25 0.25 0.25 [4,] 4.25 4.25 4.25 4.25 4.25 4.25 4.25 4.25 4.25 3.25 3.25 3.25 3.25 3.25 [5,] 6.25 6.25 6.25 6.25 6.25 6.25 6.25 6.25 6.25 5.25 5.25 5.25 5.25 5.25 v[[3]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 [2,] 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 [3,] 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 [4,] 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 [5,] 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 The first element of the matrix delta (2.5) corresponds to the first list element v[[1]], and so on... What I want to do is to set any values in this list that are greater than its corresponding delta to be NA. For example, if any value in v[[1]] is greater than 2.5 than set it to NA, and similarly for v[[2]]. If any value in v[[3]] is 1, then set it NA. Can this be done using lapply or some other quick, efficient way? Hope this makes sense. Thanks, Derek -- View this message in context: http://www.nabble.com/using-lapply%28%29-tp14693921p14693921.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] using lapply()
Thank you, this works perfectly! Dimitris Rizopoulos wrote: try the following: delta - c(2.5, 2.5, 1) v - list( matrix(round(rnorm(70, 2), 2), 5, 14), matrix(round(rnorm(70, 2), 2), 5, 14), matrix(round(rnorm(70, 2), 1), 5, 14) ) v mapply(function(x, y){ x[x y] - NA; x }, v, delta, SIMPLIFY = FALSE) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: dxc13 [EMAIL PROTECTED] To: r-help@r-project.org Sent: Tuesday, January 08, 2008 6:11 PM Subject: [R] using lapply() useR's, I am trying to find a quick way to change some values in a list that are subject to a condition to be NA. Consider the 3x1 matrix: delta - matrix(c(2.5,2.5,1), nrow = 1) And consider the list named v that has 3 elements v v[[1]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 4.25 3.25 2.25 1.25 0.25 0.75 1.75 2.75 3.75 4.25 3.25 2.25 1.25 0.25 [2,] 1.25 0.25 0.75 1.75 2.75 3.75 4.75 5.75 6.75 1.25 0.25 0.75 1.75 2.75 [3,] 7.25 6.25 5.25 4.25 3.25 2.25 1.25 0.25 0.75 7.25 6.25 5.25 4.25 3.25 [4,] 3.25 2.25 1.25 0.25 0.75 1.75 2.75 3.75 4.75 3.25 2.25 1.25 0.25 0.75 [5,] 6.25 5.25 4.25 3.25 2.25 1.25 0.25 0.75 1.75 6.25 5.25 4.25 3.25 2.25 v[[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 9.25 9.25 9.25 9.25 9.25 9.25 9.25 9.25 9.25 8.25 8.25 8.25 8.25 8.25 [2,] 7.25 7.25 7.25 7.25 7.25 7.25 7.25 7.25 7.25 6.25 6.25 6.25 6.25 6.25 [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0.25 0.25 0.25 0.25 0.25 [4,] 4.25 4.25 4.25 4.25 4.25 4.25 4.25 4.25 4.25 3.25 3.25 3.25 3.25 3.25 [5,] 6.25 6.25 6.25 6.25 6.25 6.25 6.25 6.25 6.25 5.25 5.25 5.25 5.25 5.25 v[[3]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 [2,] 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 [3,] 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 [4,] 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 3.5 [5,] 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 4.5 The first element of the matrix delta (2.5) corresponds to the first list element v[[1]], and so on... What I want to do is to set any values in this list that are greater than its corresponding delta to be NA. For example, if any value in v[[1]] is greater than 2.5 than set it to NA, and similarly for v[[2]]. If any value in v[[3]] is 1, then set it NA. Can this be done using lapply or some other quick, efficient way? Hope this makes sense. Thanks, Derek -- View this message in context: http://www.nabble.com/using-lapply%28%29-tp14693921p14693921.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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ 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. -- View this message in context: http://www.nabble.com/using-lapply%28%29-tp14693921p14695366.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] using mapply()
useR's, This is a follow up question to one I previously asked. Consider the 3-element list below res [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA 1.25 0.25 0.75 NA NA NANANANA 1.25 0.25 [2,] 1.25 0.25 0.75 NA NA NA NA NA NA 1.25 0.25 0.75NA NA [3,] NA NA NA NA NA NA 1.25 0.25 0.75NANANANA NA [4,] NA NA 1.25 0.25 0.75 NA NA NA NANANA 1.25 0.25 0.75 [5,] NA NA NA NA NA 1.25 0.25 0.75 NANANANANA NA [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA NA NA NA NA NA NANANANANA 1.25 [2,] 1.25 NA NA NA NA NA NA NA NANANANANA NA [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0.25 0.25 0.25 0.25 0.25 [4,] NA NA NA NA NA NA NA NA NANANANANA NA [5,] NA NA NA NA NA 1.25 NA NA NANANANANA NA [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA NA NA NA NA NA NANANANANA 1.25 [2,] 0.25 NA NA NA NA NA NA NA NANANANANA NA [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0.25 0.25 0.25 0.25 0.25 [4,] NA NA NA NA NA NA NA NA NANANANANA NA [5,] NA NA NA NA NA 0.25 NA NA NANANANANA NA What I want to do is find the values that are not NA simultaneously for each value in each element of the list, and then average these numbers and store them in a 14x1 vector. For example, in position (2,1) in each of the 3 elements of the res list, there is a value, 1.25 in [[1]] and [[2]], 0.25 in [[3]]. Since there is a value in this position in all elements of the list I want to average these numbers: (1.25+1.25+0.25)/3 and store this value in a numeric vector. So, if there are not 3 values to average, this means that a given position (i,j) does not have a value in each element of the list and thus I want to store NA in the 14x1 vector. In the end, the 14x1 vector will be mixed with values and NA's or NaN's. Does anyone know a efficient way to do this? Maybe using mapply()? I hope it is clear. Thank you for any input. Derek -- View this message in context: http://www.nabble.com/using-mapply%28%29-tp14697351p14697351.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] using mapply()
Yes, that's it. Thank you again! Dimitris Rizopoulos wrote: do you mean the following?: out - rowMeans(matrix(unlist(res), ncol = length(res))) dim(out) - dim(res[[1]]) out Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting dxc13 [EMAIL PROTECTED]: useR's, This is a follow up question to one I previously asked. Consider the 3-element list below res [[1]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA 1.25 0.25 0.75 NA NA NANANANA 1.25 0.25 [2,] 1.25 0.25 0.75 NA NA NA NA NA NA 1.25 0.25 0.75NA NA [3,] NA NA NA NA NA NA 1.25 0.25 0.75NANANANA NA [4,] NA NA 1.25 0.25 0.75 NA NA NA NANANA 1.25 0.25 0.75 [5,] NA NA NA NA NA 1.25 0.25 0.75 NANANANANA NA [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA NA NA NA NA NA NANANANANA 1.25 [2,] 1.25 NA NA NA NA NA NA NA NANANANANA NA [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0.25 0.25 0.25 0.25 0.25 [4,] NA NA NA NA NA NA NA NA NANANANANA NA [5,] NA NA NA NA NA 1.25 NA NA NANANANA NA NA [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [1,] NA NA NA NA NA NA NA NA NANANANANA 1.25 [2,] 0.25 NA NA NA NA NA NA NA NANANANANA NA [3,] 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 0.25 0.25 0.25 0.25 0.25 [4,] NA NA NA NA NA NA NA NA NANANANANA NA [5,] NA NA NA NA NA 0.25 NA NA NANANANA NA NA What I want to do is find the values that are not NA simultaneously for each value in each element of the list, and then average these numbers and store them in a 14x1 vector. For example, in position (2,1) in each of the 3 elements of the res list, there is a value, 1.25 in [[1]] and [[2]], 0.25 in [[3]]. Since there is a value in this position in all elements of the list I want to average these numbers: (1.25+1.25+0.25)/3 and store this value in a numeric vector. So, if there are not 3 values to average, this means that a given position (i,j) does not have a value in each element of the list and thus I want to store NA in the 14x1 vector. In the end, the 14x1 vector will be mixed with values and NA's or NaN's. Does anyone know a efficient way to do this? Maybe using mapply()? I hope it is clear. Thank you for any input. Derek -- View this message in context: http://www.nabble.com/using-mapply%28%29-tp14697351p14697351.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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ 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. -- View this message in context: http://www.nabble.com/using-mapply%28%29-tp14697351p14698238.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] Avoiding FOR loops
useR's, I would like to know if there is a way to avoid using FOR loops to perform the below calculation. Consider the following data: x [,1] [,2] [,3] [1,]4 111 [2,]192 [3,]733 [4,]364 [5,]685 xk Var1 Var2 Var3 1 -0.25 1.75 0.5 20.75 1.75 0.5 31.75 1.75 0.5 42.75 1.75 0.5 53.75 1.75 0.5 64.75 1.75 0.5 75.75 1.75 0.5 86.75 1.75 0.5 97.75 1.75 0.5 10 -0.25 2.75 0.5 Here, X is a matrix of 3 variables in which each is of size 5 and XK are some values that correspond to each variable. For each variable, I want to do: |Xi - xkj| where i = 1 to 3 and j = 1 to 10 It looks as if a double FOR loop would work, but can the apply function work? Or some other function that is shorter than a FOR loop? Thank you, I hope this makes sense. Derek -- View this message in context: http://www.nabble.com/Avoiding-FOR-loops-tp14656517p14656517.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] expand.grid function
useR's, I have used expand.grid() several times and like the results it gives me. I am now trying something with it that I have not been able to get to work. For any n column matrix I would like to run this function on those n columns and store the results. For example, if my matrix has 1 column then this is just expand.grid(x = column1). If my matrix has two columns, then I want expand.grid(x = column1, y = column2), and so on for any number of columns... In a program I am writing, the user can specify any matrix. Does anyone know of a way for R to calculate this based on what the input matrix is? e.g. if this user gives a 3 column matrix, I want to be able to perform expand.grid() on these 3 columns without having to hard code it b/c I want to have this small function embedded in my code and the results stored as a variable. If this isn't clear, I can try to be more detailed. Thank you for any thoughts. Derek -- View this message in context: http://www.nabble.com/expand.grid-function-tp14484403p14484403.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] expand.grid function
Yes, that works perfectly. Thank you for your help! Derek Charilaos Skiadas-3 wrote: Hi Derek, On Dec 23, 2007, at 10:59 PM, dxc13 wrote: useR's, I have used expand.grid() several times and like the results it gives me. I am now trying something with it that I have not been able to get to work. For any n column matrix I would like to run this function on those n columns and store the results. For example, if my matrix has 1 column then this is just expand.grid (x = column1). If my matrix has two columns, then I want expand.grid(x = column1, y = column2), and so on for any number of columns... Does this do what you want (x is the matrix)? do.call(expand.grid, as.data.frame(x)) In a program I am writing, the user can specify any matrix. Does anyone know of a way for R to calculate this based on what the input matrix is? e.g. if this user gives a 3 column matrix, I want to be able to perform expand.grid() on these 3 columns without having to hard code it b/c I want to have this small function embedded in my code and the results stored as a variable. If this isn't clear, I can try to be more detailed. Thank you for any thoughts. Derek Haris Skiadas Department of Mathematics and Computer Science Hanover College __ 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. -- View this message in context: http://www.nabble.com/expand.grid-function-tp14484403p14484572.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.