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.
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. > > DerekHaris Skiadas Department of Mathematics and Computer Science Hanover College
Hi Derek, I love this function as well. If 'mat' is your matrix,> (mat <- matrix(1:15,ncol=3))[,1] [,2] [,3] [1,] 1 6 11 [2,] 2 7 12 [3,] 3 8 13 [4,] 4 9 14 [5,] 5 10 15 how about> expandedDF <- do.call(`expand.grid`,as.data.frame(mat)) > head(expandedDF)V1 V2 V3 1 1 6 11 2 2 6 11 3 3 6 11 4 4 6 11 5 5 6 11 6 1 7 11 [...] (the function name can be in backticks, single or double quotes, or passed with no quotes). Hope this helps, ST ----- Original Message ---- From: dxc13 <dxc13 at health.state.ny.us> To: r-help at r-project.org Sent: Sunday, December 23, 2007 7:59:49 PM Subject: [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 at 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. ____________________________________________________________________________________ Looking for last minute shopping deals?
Possibly Parallel Threads
- Split data.frames depeding values of a column
- number of pairwise present data in matrix with missings
- accessing a data frame with row names
- Function to read a string as the variables as opposed to taking the string name as the variable
- R Shiny Help - Trouble passing user input columns to emmeans after ANOVA analysis