Re: [R] arrays of arrays

2010-11-10 Thread Claudia Beleites

Hi Sachin,

I guess there are several different possibilities that are more or less handy 
depending on your data:


- lists were mentioned already, and I think they are the most natural 
representation of ragged arrays. Also very flexible, e.g. you can introduce more 
dimensions. But they can get terribly slow and very memory consuming if you have 
many rows.


- If you have many rows and they have almost the same number of elements, you 
may be better off using a normal matrix and setting the unused elements to NA.


- There are also sparse matrices in package Matrix. I've never used them, but I 
guess they may be what you are after.


This here:
new(dgCMatrix
, i = c(0L, 1L, 2L, 3L, 0L, 1L, 2L, 0L, 1L, 0L, 1L, 2L, 3L, 4L, 5L)
, p = c(0L, 4L, 7L, 9L, 15L)
, Dim = c(6L, 4L)
, Dimnames = list(NULL, NULL)
, x = c(0, 0, 1, 1, 1, 3, 5, 4, 4, 7, -1, 8, 9, 10, 6)
, factors = list()
)

Is the transposed of your example:
6 x 4 sparse Matrix of class dgCMatrix

[1,] 0 1 4  7
[2,] 0 3 4 -1
[3,] 1 5 .  8
[4,] 1 . .  9
[5,] . . . 10
[6,] . . .  6

The numeric versions do not store the zeros, and will return 0 for for the 
elements marked with '.' in the print.


You won't get any benefit from this representation in terms of memory (over a 
normal matrix) unless the total number of elements is smaller than

nrow * max (elements per row) / 2 - nrow - some more overhead

The Matrix () function will give you a hint: check whether it produces a dense 
or a sparse matrix.


- if you are terribly tight with memory you'll program your own representation 
that just stores a vector of your values and start indices for each row.

You index then with rowstart [i] + j

Here's a comparison:

# list
 l - structure(list(V1 = c(0, 0, 1, 1), V2 = c(1, 3, 5), V3 = c(4,
4), V4 = c(7, -1, 8, 9, 10, 6)), .Names = c(V1, V2, V3,
V4))
 str (l)
List of 4
 $ V1: num [1:4] 0 0 1 1
 $ V2: num [1:3] 1 3 5
 $ V3: num [1:2] 4 4
 $ V4: num [1:6] 7 -1 8 9 10 6

 object.size (l)
736 bytes

# sparse matrix
 s - new(dgCMatrix
, i = c(0L, 1L, 2L, 3L, 0L, 1L, 2L, 0L, 1L, 0L, 1L, 2L, 3L, 4L, 5L)
, p = c(0L, 4L, 7L, 9L, 15L)
, Dim = c(6L, 4L)
, Dimnames = list(NULL, NULL)
, x = c(0, 0, 1, 1, 1, 3, 5, 4, 4, 7, -1, 8, 9, 10, 6)
, factors = list()
)
 s
6 x 4 sparse Matrix of class dgCMatrix

[1,] 0 1 4  7
[2,] 0 3 4 -1
[3,] 1 5 .  8
[4,] 1 . .  9
[5,] . . . 10
[6,] . . .  6
 object.size (s)
1640 bytes
# there's a lot of overhead for the sparse matrix

# matrix
 m - structure(c(0, 1, 4, 7, 0, 3, 4, -1, 1, 5, NA, 8, 1, NA, NA,
9, NA, NA, NA, 10, NA, NA, NA, 6), .Dim = c(4L, 6L))
 m
 [,1] [,2] [,3] [,4] [,5] [,6]
[1,]0011   NA   NA
[2,]135   NA   NA   NA
[3,]44   NA   NA   NA   NA
[4,]7   -189   106
 object.size (m)
392 bytes


# own representation
 o - structure(c(0, 0, 1, 1, 1, 3, 5, 4, 4, 7, -1, 8, 9, 10, 6), rowstart = 
c(0, 4, 7, 9)) # index of end of row before saves subtracting 1 all the time


 o
 [1]  0  0  1  1  1  3  5  4  4  7 -1  8  9 10  6
attr(,rowstart)
[1] 0 4 7 9
 object.size (o)
352 bytes

 o [attr (o, rowstart) [2] + 3 ]
[1] 5

Claudia

--
Claudia Beleites
Dipartimento dei Materiali e delle Risorse Naturali
Università degli Studi di Trieste
Via Alfonso Valerio 6/a
I-34127 Trieste

phone: +39 0 40 5 58-37 68
email: cbelei...@units.it

__
R-help@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.


Re: [R] arrays of arrays

2010-11-10 Thread Duncan Murdoch

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 5


An 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.abeyward...@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 unsubscr...@allianz.com.au with 
the word unsubscribe in the subject header.

__
R-help@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@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@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] arrays of arrays

2010-11-09 Thread sachinthaka . abeywardana

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 unsubscr...@allianz.com.au with 
the word unsubscribe in the subject header.

__
R-help@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.


Re: [R] arrays of arrays

2010-11-09 Thread Santosh Srinivas
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)
 tD
  X0 X0.1 X1 X1.1  X X.1
1  13  5   NA NA  NA
2  44 NA   NA NA  NA
3  7   -1  89 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 
   135 

[[2]]
  X0 X0.1 
   44 

[[3]]
  X0 X0.1   X1 X1.1X  X.1 
   7   -189   106 



HTH,
S

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of sachinthaka.abeyward...@allianz.com.au
Sent: 10 November 2010 08:26
To: r-help@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
unsubscr...@allianz.com.au with the word unsubscribe in the subject header.

__
R-help@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@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.


Re: [R] arrays of arrays

2010-11-09 Thread Erik Iverson

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.abeyward...@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 unsubscr...@allianz.com.au with 
the word unsubscribe in the subject header.

__
R-help@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@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.


Re: [R] arrays of arrays

2010-11-09 Thread sachinthaka . abeywardana
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 unsubscr...@allianz.com.au with 
the word unsubscribe in the subject header.

__
R-help@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.


Re: [R] arrays of arrays

2010-11-09 Thread Michael Bedward
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.abeyward...@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 
 unsubscr...@allianz.com.au with the word unsubscribe in the subject header.

 __
 R-help@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@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.


Re: [R] arrays of arrays

2010-11-09 Thread sachinthaka . abeywardana
Hi Michael,

Thanks for that. Its a starting point I guess. But what if I didn't know
the length of the outer vector is? (i.e. all dimensions are variable). Or
for that matter I don't actually know what the initial dimensions are going
to be. All of it is created within a for loop.

I was hoping for something like a C++ vector, where you can specify
vectorvectorint myarray. I'm sure lists is definitely the way to go,
but not sure how to implement it.

Cheers,
Sachin

--- 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 unsubscr...@allianz.com.au with 
the word unsubscribe in the subject header.

__
R-help@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.


Re: [R] arrays of arrays

2010-11-09 Thread Michael Bedward
Hi Sachin,

That's OK - you don't need to know the dimensions up front and you can
add new vectors, or elements to an existing vector, as required.

# empty list to start with
X - list()

# we get a vector
v1 - c(1, 2, 3, 4, 5)

# add it to the ragged array
X - c(X, list(v1))

# get another couple of vectors and add them as well
v2 - c(9, 8, 7, 6)
v3 - c(2, 4, 6, 8)
X - c(X, list(v2, v3))

# add some more elements to the first vector in X - note double
brackets to access
# the vector directly
X[[1]] - c(X[[1]], 4, 3, 2, 1)

Michael

On 10 November 2010 16:46,  sachinthaka.abeyward...@allianz.com.au wrote:
 Hi Michael,

 Thanks for that. Its a starting point I guess. But what if I didn't know
 the length of the outer vector is? (i.e. all dimensions are variable). Or
 for that matter I don't actually know what the initial dimensions are going
 to be. All of it is created within a for loop.

 I was hoping for something like a C++ vector, where you can specify
 vectorvectorint myarray. I'm sure lists is definitely the way to go,
 but not sure how to implement it.

 Cheers,
 Sachin

 --- 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 
 unsubscr...@allianz.com.au with the word unsubscribe in the subject header.



__
R-help@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.