Hi R Gurus! When creating a matrix, is it better to use the structure function or the matrix function, please? Thanks, Edna Bell
structure vs. matrix
7 messages · Edna Bell, Mark Difford, Duncan Murdoch +2 more
Hi Edna,
When creating a matrix, is it better to use the structure function or the matrix function...?
I hope you have a huge (empty) jar in the kitchen, and that your pantry is empty. R isn't too difficult, except if you're trying to do stats (and don't know what you are doing --- though I am not suggesting that you don't know how to do stats;). ## see ?structure ?matrix A structure is not a matrix. So, if you want to make a matrix, use matrix(). Easy. HTH, Mark.
Edna Bell wrote:
Hi R Gurus! When creating a matrix, is it better to use the structure function or the matrix function, please? Thanks, Edna Bell
______________________________________________ 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.
View this message in context: http://www.nabble.com/structure-vs.-matrix-tf4745521.html#a13572508 Sent from the R help mailing list archive at Nabble.com.
On 04/11/2007 1:28 AM, Edna Bell wrote:
Hi R Gurus! When creating a matrix, is it better to use the structure function or the matrix function, please?
I'd use the matrix() function. You can do it with structure(), and your code might be a little faster, but it will be harder to read, will break if someone ever changes the low-level implementation of matrices, e.g. by replacing it with the Matrix class. Duncan Murdoch
On 04/11/2007 7:43 AM, Mark Difford wrote:
Hi Edna,
When creating a matrix, is it better to use the structure function or the matrix function...?
I hope you have a huge (empty) jar in the kitchen, and that your pantry is empty. R isn't too difficult, except if you're trying to do stats (and don't know what you are doing --- though I am not suggesting that you don't know how to do stats;). ## see ?structure ?matrix A structure is not a matrix. So, if you want to make a matrix, use matrix(). Easy.
But a matrix is a structure, so you could use structure() to create one. I wouldn't normally do that, but there might be circumstances where it was appropriate. Duncan Murdoch
HTH, Mark. Edna Bell wrote:
Hi R Gurus! When creating a matrix, is it better to use the structure function or the matrix function, please? Thanks, Edna Bell
______________________________________________ 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.
On Sun, 4 Nov 2007, Duncan Murdoch wrote:
On 04/11/2007 1:28 AM, Edna Bell wrote:
Hi R Gurus! When creating a matrix, is it better to use the structure function or the matrix function, please?
I'd use the matrix() function. You can do it with structure(), and your code might be a little faster, but it will be harder to read, will break if someone ever changes the low-level implementation of matrices, e.g. by replacing it with the Matrix class.
For the record A <- <some data> dim(A) <- c(nr, rc) is faster than either matrix() or structure(), and seems at least as easy to read. matrix() has the advantage that it will replicate the data to the necessary length, but both will make copies that my version does not if you have the right length of data. But this is only worth worrying about if you have large matrices.
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
I was reading "S Programming" and a set of the code used structure to set up a matrix. I just wondered which was better and why.
On 11/4/07, Mark Difford <mark_difford at yahoo.co.uk> wrote:
Hi Edna,
When creating a matrix, is it better to use the structure function or the matrix function...?
I hope you have a huge (empty) jar in the kitchen, and that your pantry is empty. R isn't too difficult, except if you're trying to do stats (and don't know what you are doing --- though I am not suggesting that you don't know how to do stats;). ## see ?structure ?matrix A structure is not a matrix. So, if you want to make a matrix, use matrix(). Easy. HTH, Mark. Edna Bell wrote:
Hi R Gurus! When creating a matrix, is it better to use the structure function or the matrix function, please? Thanks, Edna Bell
______________________________________________ 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.
-- View this message in context: http://www.nabble.com/structure-vs.-matrix-tf4745521.html#a13572508 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.
In general, you want to use as high level a construct as possible since then the details implementing the construct can be modified and improved without your code changing. This is really just an aspect of modularity. Thus matrix would be preferred. Actually since R allows access to the lower level details this type of benefit is not really guaranteed but as a matter of principle I would use other constructs than structure whenever possible.
On Nov 4, 2007 8:32 PM, Edna Bell <edna.bell01 at gmail.com> wrote:
I was reading "S Programming" and a set of the code used structure to set up a matrix. I just wondered which was better and why. On 11/4/07, Mark Difford <mark_difford at yahoo.co.uk> wrote:
Hi Edna,
When creating a matrix, is it better to use the structure function or the matrix function...?
I hope you have a huge (empty) jar in the kitchen, and that your pantry is empty. R isn't too difficult, except if you're trying to do stats (and don't know what you are doing --- though I am not suggesting that you don't know how to do stats;). ## see ?structure ?matrix A structure is not a matrix. So, if you want to make a matrix, use matrix(). Easy. HTH, Mark. Edna Bell wrote:
Hi R Gurus! When creating a matrix, is it better to use the structure function or the matrix function, please? Thanks, Edna Bell
______________________________________________ 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.
-- View this message in context: http://www.nabble.com/structure-vs.-matrix-tf4745521.html#a13572508 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.
______________________________________________ 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.