Re: [R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-25 Thread Joe Byers
I want to thank everyone for their comments and suggestions.  P. Burns' 
S Poetry I think will be a lot of help and I thought it was really 
poetry:).  So will the other references that were provided by I believe 
Mr Kane.  The other replies provided me some great insights.

If I understand R uses the $ and [[]] like java uses the . (dot) 
notation for accessing components of classes and objects.  If the java 
class or object being accessed by the . notation is specifically a list, 
container, vector, or array type you use the element operator which is 
the parenthesis in java but R uses [].

This helps be visualize what I am trying to do if I am correct with my 
interpretation.

Thank all of you so much.
Joe


Thomas Lumley wrote:
 On Tue, 24 Oct 2006, Joe W. Byers wrote:
 following code produces a 5 element list of 2X5 random numbers that I
 then convert to a 2X5X5 matrix.
 cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
 rnds-NULL;
 for (i in 1:5){
  t1-rnorm(5,cov)
  t2-rnorm(5,cov)
  t3-rbind(t1,t2)
  rnds[i]-list(t3)
 }

 rnds.matrix-array(unlist(rnds),dim=c(2,5,5));

 To access the matrix rnds.matrix I use rnds.matrix[x,y,z].  This I
 understand.

 To access the list I user [[z]][x,y].  This I do not understand.  I
 found by chance this reference notation in an old mailing list that
 helped me.

 
 Yes, this can be confusing.  One reason that it is confusing is that the 
 rules appear to be different (though they aren't) for vectors and lists.
 
 The single bracket [ extracts a subvector, and the double bracket [[ 
 extracts an element.  That is, with
   a-list(b=1,c=2,d=3)
 you can extracts the first element of a,
 a[[1]]
 [1] 1
 or a sublist with the first two elements
 a[1:2]
 $b
 [1] 1
 
 $c
 [1] 2
 or a sublist with just the first element
 a[1]
 $b
 [1] 1
 
 The same is true for numeric or character vectors, but there an element 
 and a subvector of length one are the same, so the distinction between [[ 
 and [ is harder to understand.
 b-1:10
 b[1:2]
 [1] 1 2
 b[1]
 [1] 1
 b[[1]]
 [1] 1
 
   -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
 and provide commented, minimal, self-contained, reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.


[R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-24 Thread Joe W. Byers
Hi all,

I would greatly appreciate some help understanding how R references 
arrays, matrices, lists, and objects using [[]] and [].  I have read the 
R guides and several tutorials but I am not the fastest kid on the block 
so I am still having difficulty understanding this.  For examples the 
following code produces a 5 element list of 2X5 random numbers that I 
then convert to a 2X5X5 matrix.
cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
rnds-NULL;
for (i in 1:5){
t1-rnorm(5,cov)
t2-rnorm(5,cov)
t3-rbind(t1,t2)
rnds[i]-list(t3)
}

rnds.matrix-array(unlist(rnds),dim=c(2,5,5));

To access the matrix rnds.matrix I use rnds.matrix[x,y,z].  This I 
understand.

To access the list I user [[z]][x,y].  This I do not understand.  I 
found by chance this reference notation in an old mailing list that 
helped me.

I could use some help in knowing when to use [[]] referencing and when 
to use [] referencing.  If there is a really good book, webpage, or link 
with explanation and examples I would appreciate you forwarding the the 
citation.

Thank you
Joe

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-24 Thread David Barron
Have you tried help([), which gives a good explanation.


On 24/10/06, Joe W. Byers [EMAIL PROTECTED] wrote:
 Hi all,

 I would greatly appreciate some help understanding how R references
 arrays, matrices, lists, and objects using [[]] and [].  I have read the
 R guides and several tutorials but I am not the fastest kid on the block
 so I am still having difficulty understanding this.  For examples the
 following code produces a 5 element list of 2X5 random numbers that I
 then convert to a 2X5X5 matrix.
 cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
 rnds-NULL;
 for (i in 1:5){
 t1-rnorm(5,cov)
 t2-rnorm(5,cov)
 t3-rbind(t1,t2)
 rnds[i]-list(t3)
 }

 rnds.matrix-array(unlist(rnds),dim=c(2,5,5));

 To access the matrix rnds.matrix I use rnds.matrix[x,y,z].  This I
 understand.

 To access the list I user [[z]][x,y].  This I do not understand.  I
 found by chance this reference notation in an old mailing list that
 helped me.

 I could use some help in knowing when to use [[]] referencing and when
 to use [] referencing.  If there is a really good book, webpage, or link
 with explanation and examples I would appreciate you forwarding the the
 citation.

 Thank you
 Joe

 __
 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
 and provide commented, minimal, self-contained, reproducible code.



-- 
=
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-24 Thread Joe Byers

David,

Yes, I did.  I just still do not get it.  That is why I ask here.  
Hoping someone knew a step by step guide that I could look at.  My trial 
and error approach takes me hours some days.  I currently move most 
things in and out of data.frames where I can name the columns and 
reference with the $ and a subsetting function, but that is not always 
efficient.  If I could understand the [ referencing better, my code 
would be more efficient and I think faster.  Part of my problem is my 
SAS background where everything is a flat table and coding is really 
sloppy.  A data step with a bunch of if-then-else to perform the 
calculation where as in matrix format like in R you do things more 
compactly.  Not always easy to read but efficient and fast.


I appreciate you help.

Thank you
Joe




David Barron wrote:

Have you tried help([), which gives a good explanation.


On 24/10/06, Joe W. Byers [EMAIL PROTECTED] wrote:

Hi all,

I would greatly appreciate some help understanding how R references
arrays, matrices, lists, and objects using [[]] and [].  I have read the
R guides and several tutorials but I am not the fastest kid on the block
so I am still having difficulty understanding this.  For examples the
following code produces a 5 element list of 2X5 random numbers that I
then convert to a 2X5X5 matrix.
cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
rnds-NULL;
for (i in 1:5){
t1-rnorm(5,cov)
t2-rnorm(5,cov)
t3-rbind(t1,t2)
rnds[i]-list(t3)
}

rnds.matrix-array(unlist(rnds),dim=c(2,5,5));

To access the matrix rnds.matrix I use rnds.matrix[x,y,z].  This I
understand.

To access the list I user [[z]][x,y].  This I do not understand.  I
found by chance this reference notation in an old mailing list that
helped me.

I could use some help in knowing when to use [[]] referencing and when
to use [] referencing.  If there is a really good book, webpage, or link
with explanation and examples I would appreciate you forwarding the the
citation.

Thank you
Joe

__
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

and provide commented, minimal, self-contained, reproducible code.




__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-24 Thread David Barron
Well, as I understand it, with a list [] doesn't return the actual
contents of that element of the list, whereas [[]] does.  Compare, for
example rnds[1] + 1 with rnds[[1]] + 1.  So, with a list you have to
use the double bracket notation to get to the actual contents of that
element of the list, as opposed to a pointer to the element.  With a
vector or a matrix, [[]] doesn't seem to have much use, though you can
use it with a single number, in which case you will get the same
result as [].

There is a separate method for data frames, in which [[]] will give
you a column.  So, I think that data[[1]] is the same as data[,1].

Anyway, these rules have always worked for me, though I'm sure others
can give a more sophisticated answer!

On 24/10/06, Joe Byers [EMAIL PROTECTED] wrote:
 David,

 Yes, I did.  I just still do not get it.  That is why I ask here.
 Hoping someone knew a step by step guide that I could look at.  My trial
 and error approach takes me hours some days.  I currently move most
 things in and out of data.frames where I can name the columns and
 reference with the $ and a subsetting function, but that is not always
 efficient.  If I could understand the [ referencing better, my code
 would be more efficient and I think faster.  Part of my problem is my
 SAS background where everything is a flat table and coding is really
 sloppy.  A data step with a bunch of if-then-else to perform the
 calculation where as in matrix format like in R you do things more
 compactly.  Not always easy to read but efficient and fast.

 I appreciate you help.

 Thank you
 Joe




 David Barron wrote:
  Have you tried help([), which gives a good explanation.
 
 
  On 24/10/06, Joe W. Byers [EMAIL PROTECTED] wrote:
  Hi all,
 
  I would greatly appreciate some help understanding how R references
  arrays, matrices, lists, and objects using [[]] and [].  I have read the
  R guides and several tutorials but I am not the fastest kid on the block
  so I am still having difficulty understanding this.  For examples the
  following code produces a 5 element list of 2X5 random numbers that I
  then convert to a 2X5X5 matrix.
  cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
  rnds-NULL;
  for (i in 1:5){
  t1-rnorm(5,cov)
  t2-rnorm(5,cov)
  t3-rbind(t1,t2)
  rnds[i]-list(t3)
  }
 
  rnds.matrix-array(unlist(rnds),dim=c(2,5,5));
 
  To access the matrix rnds.matrix I use rnds.matrix[x,y,z].  This I
  understand.
 
  To access the list I user [[z]][x,y].  This I do not understand.  I
  found by chance this reference notation in an old mailing list that
  helped me.
 
  I could use some help in knowing when to use [[]] referencing and when
  to use [] referencing.  If there is a really good book, webpage, or link
  with explanation and examples I would appreciate you forwarding the the
  citation.
 
  Thank you
  Joe
 
  __
  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
  and provide commented, minimal, self-contained, reproducible code.
 
 
 





-- 
=
David Barron
Said Business School
University of Oxford
Park End Street
Oxford OX1 1HP

__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-24 Thread Joe Byers

Thank you so very much. I can try this on my own examples.

Much appreciated.

Joe


David Barron wrote:

Well, as I understand it, with a list [] doesn't return the actual
contents of that element of the list, whereas [[]] does.  Compare, for
example rnds[1] + 1 with rnds[[1]] + 1.  So, with a list you have to
use the double bracket notation to get to the actual contents of that
element of the list, as opposed to a pointer to the element.  With a
vector or a matrix, [[]] doesn't seem to have much use, though you can
use it with a single number, in which case you will get the same
result as [].

There is a separate method for data frames, in which [[]] will give
you a column.  So, I think that data[[1]] is the same as data[,1].

Anyway, these rules have always worked for me, though I'm sure others
can give a more sophisticated answer!

On 24/10/06, Joe Byers [EMAIL PROTECTED] wrote:

David,

Yes, I did.  I just still do not get it.  That is why I ask here.
Hoping someone knew a step by step guide that I could look at.  My trial
and error approach takes me hours some days.  I currently move most
things in and out of data.frames where I can name the columns and
reference with the $ and a subsetting function, but that is not always
efficient.  If I could understand the [ referencing better, my code
would be more efficient and I think faster.  Part of my problem is my
SAS background where everything is a flat table and coding is really
sloppy.  A data step with a bunch of if-then-else to perform the
calculation where as in matrix format like in R you do things more
compactly.  Not always easy to read but efficient and fast.

I appreciate you help.

Thank you
Joe




David Barron wrote:
 Have you tried help([), which gives a good explanation.


 On 24/10/06, Joe W. Byers [EMAIL PROTECTED] wrote:
 Hi all,

 I would greatly appreciate some help understanding how R references
 arrays, matrices, lists, and objects using [[]] and [].  I have 
read the
 R guides and several tutorials but I am not the fastest kid on the 
block

 so I am still having difficulty understanding this.  For examples the
 following code produces a 5 element list of 2X5 random numbers that I
 then convert to a 2X5X5 matrix.
 cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
 rnds-NULL;
 for (i in 1:5){
 t1-rnorm(5,cov)
 t2-rnorm(5,cov)
 t3-rbind(t1,t2)
 rnds[i]-list(t3)
 }

 rnds.matrix-array(unlist(rnds),dim=c(2,5,5));

 To access the matrix rnds.matrix I use rnds.matrix[x,y,z].  This I
 understand.

 To access the list I user [[z]][x,y].  This I do not understand.  I
 found by chance this reference notation in an old mailing list that
 helped me.

 I could use some help in knowing when to use [[]] referencing and 
when
 to use [] referencing.  If there is a really good book, webpage, 
or link
 with explanation and examples I would appreciate you forwarding 
the the

 citation.

 Thank you
 Joe

 __
 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
 and provide commented, minimal, self-contained, reproducible code.









__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-24 Thread John Kane

--- Joe Byers [EMAIL PROTECTED] wrote:

 David,
 
 Yes, I did.  I just still do not get it.  That is
 why I ask here.  
 Hoping someone knew a step by step guide that I
 could look at.  My trial 
 and error approach takes me hours some days.  I
 currently move most 
 things in and out of data.frames where I can name
 the columns and 
 reference with the $ and a subsetting function, but
 that is not always 
 efficient.  If I could understand the [ referencing
 better, my code 
 would be more efficient and I think faster.  Part of
 my problem is my 
 SAS background where everything is a flat table and
 coding is really 
 sloppy.  A data step with a bunch of if-then-else to
 perform the 
 calculation where as in matrix format like in R you
 do things more 
 compactly.  Not always easy to read but efficient
 and fast.
 
 I appreciate you help.
 
 Thank you
 Joe
 
I'm with you Joe.  However you might want to have a
look at chapter 2 of An Introduction to S and The
Hmisc and Design Libraries
Carlos Alzola  Harrell  availabe in the contributed
material on the CRAN site.
 
 David Barron wrote:
  Have you tried help([), which gives a good
 explanation.
 
 
  On 24/10/06, Joe W. Byers [EMAIL PROTECTED]
 wrote:
  Hi all,
 
  I would greatly appreciate some help
 understanding how R references
  arrays, matrices, lists, and objects using [[]]
 and [].  I have read the
  R guides and several tutorials but I am not the
 fastest kid on the block
  so I am still having difficulty understanding
 this.  For examples the
  following code produces a 5 element list of 2X5
 random numbers that I
  then convert to a 2X5X5 matrix.
  cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
  rnds-NULL;
  for (i in 1:5){
  t1-rnorm(5,cov)
  t2-rnorm(5,cov)
  t3-rbind(t1,t2)
  rnds[i]-list(t3)
  }
 
  rnds.matrix-array(unlist(rnds),dim=c(2,5,5));
 
  To access the matrix rnds.matrix I use
 rnds.matrix[x,y,z].  This I
  understand.
 
  To access the list I user [[z]][x,y].  This I do
 not understand.  I
  found by chance this reference notation in an old
 mailing list that
  helped me.
 
  I could use some help in knowing when to use [[]]
 referencing and when
  to use [] referencing.  If there is a really good
 book, webpage, or link
  with explanation and examples I would appreciate
 you forwarding the the
  citation.
 
  Thank you
  Joe
 
  __
  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
  and provide commented, minimal, self-contained,
 reproducible code.
 
 
 
  __
 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
 and provide commented, minimal, self-contained,
 reproducible code.


__
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
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Help with understanding [[]] [] array, list, matrix referencing

2006-10-24 Thread Thomas Lumley
On Tue, 24 Oct 2006, Joe W. Byers wrote:
 following code produces a 5 element list of 2X5 random numbers that I
 then convert to a 2X5X5 matrix.
 cov-matrix(c(.4,-.1,-.1,.3),nrow=2,ncol=2)
 rnds-NULL;
 for (i in 1:5){
   t1-rnorm(5,cov)
   t2-rnorm(5,cov)
   t3-rbind(t1,t2)
   rnds[i]-list(t3)
 }

 rnds.matrix-array(unlist(rnds),dim=c(2,5,5));

 To access the matrix rnds.matrix I use rnds.matrix[x,y,z].  This I
 understand.

 To access the list I user [[z]][x,y].  This I do not understand.  I
 found by chance this reference notation in an old mailing list that
 helped me.


Yes, this can be confusing.  One reason that it is confusing is that the 
rules appear to be different (though they aren't) for vectors and lists.

The single bracket [ extracts a subvector, and the double bracket [[ 
extracts an element.  That is, with
  a-list(b=1,c=2,d=3)
you can extracts the first element of a,
 a[[1]]
[1] 1
or a sublist with the first two elements
 a[1:2]
$b
[1] 1

$c
[1] 2
or a sublist with just the first element
 a[1]
$b
[1] 1

The same is true for numeric or character vectors, but there an element 
and a subvector of length one are the same, so the distinction between [[ 
and [ is harder to understand.
 b-1:10
 b[1:2]
[1] 1 2
 b[1]
[1] 1
 b[[1]]
[1] 1

-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
and provide commented, minimal, self-contained, reproducible code.