Hi All, I want to have an array/ matrix that looks this x<- 0 0 1 1 1 3 5 4 4 7 -1 8 9 10 6 I hope this makes sense. So basically if I want x[1,3] it will access 0 and similarly x[4,2], -1. Thanks in advance, Sachin p.s. sorry about the corporate notice. --- Please consider the environment before printing this email --- Allianz - Best General Insurance Company of the Year 2010* Allianz - General Insurance Company of the Year 2009+ * Australian Banking and Finance Insurance Awards + Australia and New Zealand Insurance Industry Awards This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header.
Not sure if this is the best way ... but something similar to my question from yesterday that I could solve as follows.> tD <- read.csv("Book1.csv") > tDX0 X0.1 X1 X1.1 X X.1 1 1 3 5 NA NA NA 2 4 4 NA NA NA NA 3 7 -1 8 9 10 6> x1 <- tD[1,1:3] > > x2 <- tD[2,1:2] > > x3 <- tD[3,1:6] > tData<-list(vector('numeric')) > tData<-list(vector('numeric')) > x1 = unlist(x1) > x2 = unlist(x2) > x3 = unlist(x3) > tData[[1]]=x1 > tData[[2]]=x2 > tData[[3]]=x3 > tData[[1]] X0 X0.1 X1 1 3 5 [[2]] X0 X0.1 4 4 [[3]] X0 X0.1 X1 X1.1 X X.1 7 -1 8 9 10 6>HTH, S -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of sachinthaka.abeywardana at allianz.com.au Sent: 10 November 2010 08:26 To: r-help at r-project.org Subject: [R] arrays of arrays Hi All, I want to have an array/ matrix that looks this x<- 0 0 1 1 1 3 5 4 4 7 -1 8 9 10 6 I hope this makes sense. So basically if I want x[1,3] it will access 0 and similarly x[4,2], -1. Thanks in advance, Sachin p.s. sorry about the corporate notice. --- Please consider the environment before printing this email --- Allianz - Best General Insurance Company of the Year 2010* Allianz - General Insurance Company of the Year 2009+ * Australian Banking and Finance Insurance Awards + Australia and New Zealand Insurance Industry Awards This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header. ______________________________________________ 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.
This type of object has the "matrix" class in R. So just use ?matrix to create it. matrix(1:25, ncol = 5) for example. On 11/09/2010 08:55 PM, sachinthaka.abeywardana at allianz.com.au wrote:> > Hi All, > > I want to have an array/ matrix that looks this > > x<- 0 0 1 1 > 1 3 5 > 4 4 > 7 -1 8 9 10 6 > > I hope this makes sense. So basically if I want x[1,3] it will access 0 and > similarly x[4,2], -1. > > Thanks in advance, > Sachin > p.s. sorry about the corporate notice. > > --- Please consider the environment before printing this email --- > > Allianz - Best General Insurance Company of the Year 2010* > Allianz - General Insurance Company of the Year 2009+ > > * Australian Banking and Finance Insurance Awards > + Australia and New Zealand Insurance Industry Awards > > This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. > Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header. > > ______________________________________________ > 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.
Hi Erik, Thanks for replying. Only problem with that is that each row has 5 elements (or 5 columns). I want varying number of columns as shown in my example. x<- 0 0 1 1 1 3 5 4 7 -1 8 9 10 6 Regards, Sachin p.s. sorry about corporate notice. --- Please consider the environment before printing this email --- Allianz - Best General Insurance Company of the Year 2010* Allianz - General Insurance Company of the Year 2009+ * Australian Banking and Finance Insurance Awards + Australia and New Zealand Insurance Industry Awards This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header.
Hello Sachin, You have a "ragged array" and you can easily store this as a list of vectors... x <- list(c(0,0,1,1), c(1,3,5), 4, c(7, -1, 8, 9, 10, 6)) The only gotcha with this is that you will then need to use double brackets for the first index when retrieving values (single brackets will return a vector wrapped in a list)... e.g. x[[2]][3] gives 5 You can query vector length with double brackets... e.g. length( x[[2]] ) gives 3 Hope this helps, Michael On 10 November 2010 16:00, <sachinthaka.abeywardana at allianz.com.au> wrote:> Hi Erik, > > Thanks for replying. Only problem with that is that each row has 5 elements > (or 5 columns). I want varying number of columns as shown in my example. > > x<- ? ? ? ? ? ? ?0 ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 1 > ? ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? 3 ? ? ? ? ? ? ? 5 > ? ? ? ? ? ? ? ? 4 > ? ? ? ? ? ? ? ? 7 ? ? ? ? ? ? ? -1 ? ? ? ? ? ? ?8 ? ? ? ? ? ? ? 9 ? ? ? ? ? ? ? 10 ? ? ? ? ? ? ?6 > > Regards, > Sachin > p.s. sorry about corporate notice. > > --- Please consider the environment before printing this email --- > > Allianz - Best General Insurance Company of the Year 2010* > Allianz - General Insurance Company of the Year 2009+ > > * Australian Banking and Finance Insurance Awards > + Australia and New Zealand Insurance Industry Awards > > This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. > Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header. > > ______________________________________________ > 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 10/11/2010 12:12 AM, Michael Bedward wrote:> Hello Sachin, > > You have a "ragged array" and you can easily store this as a list of vectors... > > x<- list(c(0,0,1,1), c(1,3,5), 4, c(7, -1, 8, 9, 10, 6)) > > The only gotcha with this is that you will then need to use double > brackets for the first index when retrieving values (single brackets > will return a vector wrapped in a list)... > > e.g. x[[2]][3] gives 5An alternative syntax that is sometimes easier to work with is x[[c(2,3)]] This needs the double brackets; x[c(2,3)] would be a list holding items 2 and 3 from x, i.e. list(c(1,3,5), 4). Duncan Murdoch> > You can query vector length with double brackets... > > e.g. length( x[[2]] ) gives 3 > > Hope this helps, > > Michael > > On 10 November 2010 16:00,<sachinthaka.abeywardana at allianz.com.au> wrote: >> Hi Erik, >> >> Thanks for replying. Only problem with that is that each row has 5 elements >> (or 5 columns). I want varying number of columns as shown in my example. >> >> x<- 0 0 1 1 >> 1 3 5 >> 4 >> 7 -1 8 9 10 6 >> >> Regards, >> Sachin >> p.s. sorry about corporate notice. >> >> --- Please consider the environment before printing this email --- >> >> Allianz - Best General Insurance Company of the Year 2010* >> Allianz - General Insurance Company of the Year 2009+ >> >> * Australian Banking and Finance Insurance Awards >> + Australia and New Zealand Insurance Industry Awards >> >> This email and any attachments has been sent by Allianz Australia Insurance Limited (ABN 15 000 122 850) and is intended solely for the addressee. It is confidential, may contain personal information and may be subject to legal professional privilege. Unauthorised use is strictly prohibited and may be unlawful. If you have received this by mistake, confidentiality and any legal privilege are not waived or lost and we ask that you contact the sender and delete and destroy this and any other copies. In relation to any legal use you may make of the contents of this email, you must ensure that you comply with the Privacy Act (Cth) 1988 and you should note that the contents may be subject to copyright and therefore may not be reproduced, communicated or adapted without the express consent of the owner of the copyright. >> Allianz will not be liable in connection with any data corruption, interruption, delay, computer virus or unauthorised access or amendment to the contents of this email. If this email is a commercial electronic message and you would prefer not to receive further commercial electronic messages from Allianz, please forward a copy of this email to unsubscribe at allianz.com.au with the word unsubscribe in the subject header. >> >> ______________________________________________ >> 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.