Re: [R] dim vs length for vectors
On Friday 21 January 2005 06:35, Gabor Grothendieck wrote: In R, vectors are not arrays: R v - 1:4 R dim(v) NULL R is.array(v) [1] FALSE R a - array(1:4) R dim(a) [1] 4 R is.array(a) [1] TRUE Is this a feature which is useful in some applications? IMHO the difference between vectors and 1-dimensional arrays is really annoying and I had already several bugs in my code, because I mixed these up. Is it possible in future versions of R that R does not differentiate between vectors and 1-dimensional arrays (e.g. by treating all vectors as 1-dimensional arrays)? Arne __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
Arne Henningsen wrote: On Friday 21 January 2005 06:35, Gabor Grothendieck wrote: In R, vectors are not arrays: R v - 1:4 R dim(v) NULL R is.array(v) [1] FALSE R a - array(1:4) R dim(a) [1] 4 R is.array(a) [1] TRUE Is this a feature which is useful in some applications? IMHO the difference between vectors and 1-dimensional arrays is really annoying and I had already several bugs in my code, because I mixed these up. Is it possible in future versions of R that R does not differentiate between vectors and 1-dimensional arrays (e.g. by treating all vectors as 1-dimensional arrays)? No, that would break huge amounts of code! See ?[ and learn how to use its argument drop. Uwe Ligges Arne __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
IMHO the difference between vectors and 1-dimensional arrays is really annoying and I had already several bugs in my code, because I mixed these up. Is it possible in future versions of R that R does not differentiate between vectors and 1-dimensional arrays (e.g. by treating all vectors as 1-dimensional arrays)? No, that would break huge amounts of code! See ?[ and learn how to use its argument drop. What I was proposing doesn't require a lot of programming. Just whenever you call dim(), it does length() if the object is a vector and returns it in the format: R a - 1:12 R dim(a) [1] 12 This means that you can provide a unified introduction to data structures that take only one type of atomic value (and generally call these structures arrays). What I call a one dimensional array only has one dim attribute and hence would use a[4] for extraction (for example), which is consistent with the usage for [ (to my understanding). That way, when you introduce [ and arrays at the same time, you can tell beginners: 1) Run dim() 2) If there's one dim, use [ ]; if there are 2 dims, use [ , ]; if there are 3 dims, you use [ , , ]. This is conceptually easy for a beginner and avoids a bit of frustration. If I need to initiate an empty vector for example I use R a - array() It just looks like the difference between a 1-d array (which doesn't exist as far as I can tell) and a vector is semantic, and I think that R would be more logical if they were treated as the same thing. This doesn't mean changing the way the is.array(), is.vector(), -, etc functions work, but just changing dim(). Yours, Olivia Lau __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
Olivia Lau wrote: IMHO the difference between vectors and 1-dimensional arrays is really annoying and I had already several bugs in my code, because I mixed these up. Is it possible in future versions of R that R does not differentiate between vectors and 1-dimensional arrays (e.g. by treating all vectors as 1-dimensional arrays)? No, that would break huge amounts of code! See ?[ and learn how to use its argument drop. What I was proposing doesn't require a lot of programming. Just whenever you call dim(), it does length() if the object is a vector and returns it in the format: R a - 1:12 R dim(a) [1] 12 Dim reports the dimesnion attribute, so why do you want to make it inconsistant This means that you can provide a unified introduction to data structures that take only one type of atomic value (and generally call these structures arrays). What I call a one dimensional array only has one dim attribute and hence would use a[4] for extraction (for example), which is consistent with the usage for [ (to my understanding). That way, when you introduce [ and arrays at the same time, you can tell beginners: 1) Run dim() 2) If there's one dim, use [ ]; if there are 2 dims, use [ , ]; if there are 3 dims, you use [ , , ]. This is conceptually easy for a beginner and avoids a bit of frustration. Well, you forgot that even a matrix is represented as a vector, but with dimension attributes! And indexing a matrix vector-like has some benefits in some cases. If I need to initiate an empty vector for example I use R a - array() It just looks like the difference between a 1-d array (which doesn't exist as far as I can tell) and a vector is semantic, and I think that R Note the difference: x - 1:5 dim(x) # NULL str(x) # int [1:5] 1 2 3 4 5 x - array(1:5, dim=5) dim(x) # [1] 5 str(x) # int [, 1:5] 1 2 3 4 5 would be more logical if they were treated as the same thing. This doesn't mean changing the way the is.array(), is.vector(), -, etc functions work, but just changing dim(). But then, you won't see the difference beween an 1D array (vector with dim attribute and a vector without dim attribute any more! You might want to read the R Language Definition manual. Uwe Ligges Yours, Olivia Lau __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
Perhaps you could just write dim(array(x)) when you want that effect. That has the advantage of not requiring any change to R and preserving the meaning of dim(x) as retrieving the dim attribute. Date: Fri, 21 Jan 2005 10:00:01 -0500 From: Olivia Lau [EMAIL PROTECTED] To: Uwe Ligges [EMAIL PROTECTED],Arne Henningsen [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Subject: Re: [R] dim vs length for vectors What I was proposing doesn't require a lot of programming. Just whenever you call dim(), it does length() if the object is a vector and returns it in the format: R a - 1:12 R dim(a) [1] 12 This means that you can provide a unified introduction to data structures that take only one type of atomic value (and generally call these structures arrays). What I call a one dimensional array only has one dim attribute and hence would use a[4] for extraction (for example), which is consistent with the usage for [ (to my understanding). That way, when you introduce [ and arrays at the same time, you can tell beginners: 1) Run dim() 2) If there's one dim, use [ ]; if there are 2 dims, use [ , ]; if there are 3 dims, you use [ , , ]. This is conceptually easy for a beginner and avoids a bit of frustration. If I need to initiate an empty vector for example I use R a - array() It just looks like the difference between a 1-d array (which doesn't exist as far as I can tell) and a vector is semantic, and I think that R would be more logical if they were treated as the same thing. This doesn't mean changing the way the is.array(), is.vector(), -, etc functions work, but just changing dim(). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] dim vs length for vectors
I think you can assign a dim attribute to a vector to make a 1-d array. v - 1:10 dim(v) NULL dim(v) - 10 dim(v) [1] 10 Also, u - array(1:10,dim=10) u [1] 1 2 3 4 5 6 7 8 9 10 dim(u) [1] 10 Perhaps the philosophy is that vectors are the basic, underlying type, and arrays (including 1-d arrays) carry additional structure information. So arrays are vectors, if you forget the dim attribute, but vectors are not any particular type of array. Reid Huntsinger -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Olivia Lau Sent: Friday, January 21, 2005 10:00 AM To: Uwe Ligges; Arne Henningsen Cc: r-help@stat.math.ethz.ch Subject: Re: [R] dim vs length for vectors IMHO the difference between vectors and 1-dimensional arrays is really annoying and I had already several bugs in my code, because I mixed these up. Is it possible in future versions of R that R does not differentiate between vectors and 1-dimensional arrays (e.g. by treating all vectors as 1-dimensional arrays)? No, that would break huge amounts of code! See ?[ and learn how to use its argument drop. What I was proposing doesn't require a lot of programming. Just whenever you call dim(), it does length() if the object is a vector and returns it in the format: R a - 1:12 R dim(a) [1] 12 This means that you can provide a unified introduction to data structures that take only one type of atomic value (and generally call these structures arrays). What I call a one dimensional array only has one dim attribute and hence would use a[4] for extraction (for example), which is consistent with the usage for [ (to my understanding). That way, when you introduce [ and arrays at the same time, you can tell beginners: 1) Run dim() 2) If there's one dim, use [ ]; if there are 2 dims, use [ , ]; if there are 3 dims, you use [ , , ]. This is conceptually easy for a beginner and avoids a bit of frustration. If I need to initiate an empty vector for example I use R a - array() It just looks like the difference between a 1-d array (which doesn't exist as far as I can tell) and a vector is semantic, and I think that R would be more logical if they were treated as the same thing. This doesn't mean changing the way the is.array(), is.vector(), -, etc functions work, but just changing dim(). Yours, Olivia Lau __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
Sorry, I meant as.array, not array. Date: Fri, 21 Jan 2005 12:21:39 -0500 (EST) From: Gabor Grothendieck [EMAIL PROTECTED] To: [EMAIL PROTECTED], [EMAIL PROTECTED], [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Subject: Re: [R] dim vs length for vectors Perhaps you could just write dim(array(x)) when you want that effect. That has the advantage of not requiring any change to R and preserving the meaning of dim(x) as retrieving the dim attribute. Date: Fri, 21 Jan 2005 10:00:01 -0500 From: Olivia Lau [EMAIL PROTECTED] To: Uwe Ligges [EMAIL PROTECTED],Arne Henningsen [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Subject: Re: [R] dim vs length for vectors What I was proposing doesn't require a lot of programming. Just whenever you call dim(), it does length() if the object is a vector and returns it in the format: R a - 1:12 R dim(a) [1] 12 This means that you can provide a unified introduction to data structures that take only one type of atomic value (and generally call these structures arrays). What I call a one dimensional array only has one dim attribute and hence would use a[4] for extraction (for example), which is consistent with the usage for [ (to my understanding). That way, when you introduce [ and arrays at the same time, you can tell beginners: 1) Run dim() 2) If there's one dim, use [ ]; if there are 2 dims, use [ , ]; if there are 3 dims, you use [ , , ]. This is conceptually easy for a beginner and avoids a bit of frustration. If I need to initiate an empty vector for example I use R a - array() It just looks like the difference between a 1-d array (which doesn't exist as far as I can tell) and a vector is semantic, and I think that R would be more logical if they were treated as the same thing. This doesn't mean changing the way the is.array(), is.vector(), -, etc functions work, but just changing dim(). __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
On Fri, 21 Jan 2005, Olivia Lau wrote: What I was proposing doesn't require a lot of programming. Just whenever you call dim(), it does length() if the object is a vector and returns it in the format: R a - 1:12 R dim(a) [1] 12 One problem is that there is code using is.null(dim(x)) to distinguish vectors from matrices. This code would break. -thomas __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] dim vs length for vectors
Hi all, I'm not sure if this is a feature or a bug (and I did read the FAQ and the posting guide, but am still not sure). Some of my students have been complaining and I thought I just might ask: Let K be a vector of length k. If one types dim(K), you get NULL rather than [1] k. Is this logical? Here's the way I explain it (and maybe someone can provide a more accurate explanation of what's going on): R has several types of scalar (atomic) values, the most common of which are numeric, integer, logical, and character values. Arrays are data structures which hold only one type of atomic value. Arrays can be one-dimensional (vectors), two-dimensional (matrices), or n-dimensional. (We generally use arrays of n-1 dimensions to populate n-dimensional arrays -- thus, we generally use vectors to populate matrices, and matrices to populate 3-dimensional arrays, but could use any array of dimension n-1 to populate an n-dimensional array.) It logically follows that when one does dim() on a vector, one should *not* get NULL, but should get the length of the vector (which one *could* obtain by doing length(), but I think this is less logical). I think that R should save length() for lists that have objects of different dimension and type. Does this make sense? Or is there a better explanation? Thanks in advance! Yours, Olivia Lau __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
Olivia Lau olau at fas.harvard.edu writes: : : Hi all, : : I'm not sure if this is a feature or a bug (and I did read the : FAQ and the posting guide, but am still not sure). Some of my : students have been complaining and I thought I just might ask: : Let K be a vector of length k. If one types dim(K), you get : NULL rather than [1] k. Is this logical? : : Here's the way I explain it (and maybe someone can provide a : more accurate explanation of what's going on): R has several : types of scalar (atomic) values, the most common of which are : numeric, integer, logical, and character values. Arrays are : data structures which hold only one type of atomic value. : Arrays can be one-dimensional (vectors), two-dimensional : (matrices), or n-dimensional. : : (We generally use arrays of n-1 dimensions to populate : n-dimensional arrays -- thus, we generally use vectors to : populate matrices, and matrices to populate 3-dimensional : arrays, but could use any array of dimension n-1 to populate : an n-dimensional array.) : : It logically follows that when one does dim() on a vector, one : should *not* get NULL, but should get the length of the vector : (which one *could* obtain by doing length(), but I think this is : less logical). I think that R should save length() for lists : that have objects of different dimension and type. : In R, vectors are not arrays: R v - 1:4 R dim(v) NULL R is.array(v) [1] FALSE R a - array(1:4) R dim(a) [1] 4 R is.array(a) [1] TRUE __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
I think the more intuitive way to think of it is that dim works only for matrices (an array being a 1 column matrix). and vectors are not matrices. x - 1:5 class(x) # numeric dim(x) - 5 class(x) # array dim(x) - c(5,1) class(x) # matrix dim(x) - c(1,5) class(x) # matrix On Fri, 21 Jan 2005 05:35:11 + (UTC), Gabor Grothendieck [EMAIL PROTECTED] wrote: Olivia Lau olau at fas.harvard.edu writes: : : Hi all, : : I'm not sure if this is a feature or a bug (and I did read the : FAQ and the posting guide, but am still not sure). Some of my : students have been complaining and I thought I just might ask: : Let K be a vector of length k. If one types dim(K), you get : NULL rather than [1] k. Is this logical? : : Here's the way I explain it (and maybe someone can provide a : more accurate explanation of what's going on): R has several : types of scalar (atomic) values, the most common of which are : numeric, integer, logical, and character values. Arrays are : data structures which hold only one type of atomic value. : Arrays can be one-dimensional (vectors), two-dimensional : (matrices), or n-dimensional. : : (We generally use arrays of n-1 dimensions to populate : n-dimensional arrays -- thus, we generally use vectors to : populate matrices, and matrices to populate 3-dimensional : arrays, but could use any array of dimension n-1 to populate : an n-dimensional array.) : : It logically follows that when one does dim() on a vector, one : should *not* get NULL, but should get the length of the vector : (which one *could* obtain by doing length(), but I think this is : less logical). I think that R should save length() for lists : that have objects of different dimension and type. : In R, vectors are not arrays: R v - 1:4 R dim(v) NULL R is.array(v) [1] FALSE R a - array(1:4) R dim(a) [1] 4 R is.array(a) [1] TRUE __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] dim vs length for vectors
More generally, anything that has a dim attribute is an array including 1d, 2d, 3d structures with dim attributes. Matrices have a dim attribute so matrices are arrays and is.array(m) will be TRUE if m is a matrix. miguel manese jjonphl at gmail.com writes: : : I think the more intuitive way to think of it is that dim works only : for matrices (an array being a 1 column matrix). and vectors are not : matrices. : : x - 1:5 : class(x) # numeric : dim(x) - 5 : class(x) # array : dim(x) - c(5,1) : class(x) # matrix : dim(x) - c(1,5) : class(x) # matrix : : On Fri, 21 Jan 2005 05:35:11 + (UTC), Gabor Grothendieck : ggrothendieck at myway.com wrote: : Olivia Lau olau at fas.harvard.edu writes: : : : : : Hi all, : : : : I'm not sure if this is a feature or a bug (and I did read the : : FAQ and the posting guide, but am still not sure). Some of my : : students have been complaining and I thought I just might ask: : : Let K be a vector of length k. If one types dim(K), you get : : NULL rather than [1] k. Is this logical? : : : : Here's the way I explain it (and maybe someone can provide a : : more accurate explanation of what's going on): R has several : : types of scalar (atomic) values, the most common of which are : : numeric, integer, logical, and character values. Arrays are : : data structures which hold only one type of atomic value. : : Arrays can be one-dimensional (vectors), two-dimensional : : (matrices), or n-dimensional. : : : : (We generally use arrays of n-1 dimensions to populate : : n-dimensional arrays -- thus, we generally use vectors to : : populate matrices, and matrices to populate 3-dimensional : : arrays, but could use any array of dimension n-1 to populate : : an n-dimensional array.) : : : : It logically follows that when one does dim() on a vector, one : : should *not* get NULL, but should get the length of the vector : : (which one *could* obtain by doing length(), but I think this is : : less logical). I think that R should save length() for lists : : that have objects of different dimension and type. : : : : In R, vectors are not arrays: : : R v - 1:4 : R dim(v) : NULL : R is.array(v) : [1] FALSE : : R a - array(1:4) : R dim(a) : [1] 4 : R is.array(a) : [1] TRUE : : __ : R-help at stat.math.ethz.ch mailing list : https://stat.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting- guide.html : : : __ : R-help at stat.math.ethz.ch mailing list : https://stat.ethz.ch/mailman/listinfo/r-help : PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html : : __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html