Re: [R] list of lists, is this element empty

2014-12-21 Thread William Dunlap
Your 'x' has length 2, so x[[3]] cannot be calculated ('subscript out of
bounds' is what I get).  You can check for this with length(x)3.

In general, you want to be more precise: 'does not have a value', 'is
NULL', and 'is empty' are not synonymous.  I'm not sure what 'does not have
a value' means to you.  NULL is a value with a certain type, 'NULL', and
length, 0.  seq_len(0) is empty, but not NULL, since it has type 'integer'.
 c(1,NA) contains a 'missing value'.  Can you explain what what you are
trying to check for, giving some context?



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Sat, Dec 20, 2014 at 7:58 AM, Ragia Ibrahim ragi...@hotmail.com wrote:

 Hello,
 Kindly I have a list of lists as follow
 x
 [[1]]
 [1] 7

 [[2]]
 [1] 3 4 5

 as showen x[[3]] does not have a value and it has NULL, how can I check on
 this
 how to test if x[[3]] is empty.

 thanks in advance
 Ragia

 [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
 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.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] list of lists, is this element empty

2014-12-20 Thread Ragia Ibrahim
Hello,
Kindly I have a list of lists as follow
x
[[1]]
[1] 7

[[2]]
[1] 3 4 5

as showen x[[3]] does not have a value and it has NULL, how can I check on this 
how to test if x[[3]] is empty.

thanks in advance
Ragia
  
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] list of lists, is this element empty

2014-12-20 Thread Ben Tupper
Hi,


On Dec 20, 2014, at 10:58 AM, Ragia Ibrahim ragi...@hotmail.com wrote:

 Hello,
 Kindly I have a list of lists as follow
 x
 [[1]]
 [1] 7
 
 [[2]]
 [1] 3 4 5
 
 as showen x[[3]] does not have a value and it has NULL, how can I check on 
 this 
 how to test if x[[3]] is empty.
 

In general you can us is.null()

x - list(7, 3:5, NULL, A)

 is.null(x[[3]])
[1] TRUE

but be aware that trying access an element by index that is greater than the 
length of the list will cause you issues.

 is.null(x[[10]])
Error in x[[10]] : subscript out of bounds

You can make your own function to test for the existence of an element and if 
it is NULL.  Note that the function isn't complete in the sense that it doesn't 
test if you provide an negative index, that x is not a list, etc.  You can add 
all of those tests in.

is_null - function(x, index){
( index[1]  length(x) ) || is.null(x[[index[1]]])
}

 is_null(x, 1)
[1] FALSE
 is_null(x, 3)
[1] TRUE
 is_null(x, 10)
[1] TRUE


There a lot of info on index at 

 ?`[`

Does that answer your question?

Cheers,
Ben

 thanks in advance
 Ragia
 
   [[alternative HTML version deleted]]
 
 __
 R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
 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.



Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] list of lists, is this element empty

2014-12-20 Thread Rui Barradas

Hello,

Your list seems to have only 2 elements. You can check this with

length(x)

Or you can try

lapply(x, is.null)

Hope this helps,

Rui Barradas

Em 20-12-2014 15:58, Ragia Ibrahim escreveu:

Hello,
Kindly I have a list of lists as follow
x
[[1]]
[1] 7

[[2]]
[1] 3 4 5

as showen x[[3]] does not have a value and it has NULL, how can I check on this
how to test if x[[3]] is empty.

thanks in advance
Ragia

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 -- To UNSUBSCRIBE and more, see
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] list of lists, is this element empty

2014-12-20 Thread Boris Steipe
This can be tricky, because depending on what the missing object is, you can 
get either NULL, NA, or an error. Moreover is.na() behaves differently when 
evaluated on its own, or as the condition of an if() statement. Here is a 
function that may make life easier. The goal is NOT to have to pass extra 
arguments. 

- I use try() and return FALSE if the evaluation returns an error. 
  This applies to objects that are not found, incorrect syntax etc.
- List elements that don't exist are NULL and return FALSE.
- If any elements are NA, return FALSE. This handles out-of-bounds
  elements AND out-of-bounds slices on vectors. But it would also
  trip on valid vectors that contain an NA. I can't think of a good
  way to distinguish these two cases right now. The best way for
  this depends on the context.

I think I am handling the most obvious special cases - though I do expect this 
can be improved.


is.valid - function(x, 
 na.ignore = FALSE, 
 null.ignore=FALSE) {

# errors are always FALSE
if (class(try(x, silent=TRUE)) == try-error) return(FALSE)

# NULL is FALSE except if ignored
if (is.null(x)) {
if (!null.ignore) return(FALSE)
return(TRUE)
}

# If all elments are NA, return FALSE except if ignored; 
if (any(is.na(x))) {
if (!na.ignore) return(FALSE)
return(TRUE)
}

# Everything else is TRUE
return(TRUE)
}



# Test cases
is.valid(1) # TRUE: valid numeric constant
is.valid(FALSE) # TRUE: valid boolean constant 
is.valid(nonSuch)   # FALSE: object doesn't exist

x - 1:5; 
is.valid(x) # TRUE: existing variable
is.valid(x[4])  # TRUE: vector element
is.valid(x[8])  # FALSE: out of bounds: NA
is.valid(x[5:6])# FALSE: partially out of bounds: (5, NA)

is.valid(x[8], na.ignore=TRUE)  # TRUE

x[3] - NA
is.valid(x) # FALSE: no element can be NA
is.valid(x, na.ignore=TRUE)  # TRUE


m - matrix(1:9,nrow=3, ncol=3)
is.valid(m[2,2])# TRUE
is.valid(m[2,4])# FALSE: subscript out of bounds
is.valid(m[2,2,2])  # FALSE: incorrect n of dimensions

l - list(first=7, letters, NULL)
is.valid(l[[first]])  # TRUE: existing list elements
is.valid(l[[4]])# FALSE: list element does not exist
is.valid(l[[2]][27])# FALSE: out of bounds on existing element  
is.valid(l$first)   # TRUE: 
is.valid(l$second)  # FALSE: non-existent element: NULL
is.valid(l$second, null.ignore=TRUE)  # TRUE


Cheers,
B.



On Dec 20, 2014, at 11:20 AM, Rui Barradas ruipbarra...@sapo.pt wrote:

 Hello,
 
 Your list seems to have only 2 elements. You can check this with
 
 length(x)
 
 Or you can try
 
 lapply(x, is.null)
 
 Hope this helps,
 
 Rui Barradas
 
 Em 20-12-2014 15:58, Ragia Ibrahim escreveu:
 Hello,
 Kindly I have a list of lists as follow
 x
 [[1]]
 [1] 7
 
 [[2]]
 [1] 3 4 5
 
 as showen x[[3]] does not have a value and it has NULL, how can I check on 
 this
 how to test if x[[3]] is empty.
 
 thanks in advance
 Ragia
  
  [[alternative HTML version deleted]]
 
 __
 R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
 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 -- To UNSUBSCRIBE and more, see
 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 -- To UNSUBSCRIBE and more, see
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] list of lists, is this element empty

2014-12-20 Thread Bert Gunter
Boris et. al:

Indeed, corner cases are a bear, which is why it is incumbent on any
OP to precisely define what they mean by, say, missing,
null,empty, etc.

Here is an evil example to illustrate the sorts of nastiness that can occur:

 z - list(a=NULL, b=list(), c=NA)

 with(z,{
+ c(identical(a,b),
+ identical(a,c),
+ identical(b,c)
+ )
+ })
[1] FALSE FALSE FALSE

## OK, none of these three are the same in the sense of identical().
But ...

 outer(z,z,identical)
Error in outer(z, z, identical) :
  dims [product 9] do not match the length of object [1]

## outer gets completely flummoxed, as it should!

 expand.grid(z,z)
  Var1 Var2
1 NULL NULL
2 NULL NULL
3   NA NULL
4 NULL NULL
5 NULL NULL
6   NA NULL
7 NULL   NA
8 NULL   NA
9   NA   NA

## and expand.grid gets confused, as it probably should.

:-)

Cheers,
Bert

Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom.
Clifford Stoll




On Sat, Dec 20, 2014 at 10:57 AM, Boris Steipe boris.ste...@utoronto.ca wrote:
 This can be tricky, because depending on what the missing object is, you can 
 get either NULL, NA, or an error. Moreover is.na() behaves differently when 
 evaluated on its own, or as the condition of an if() statement. Here is a 
 function that may make life easier. The goal is NOT to have to pass extra 
 arguments.

 - I use try() and return FALSE if the evaluation returns an error.
   This applies to objects that are not found, incorrect syntax etc.
 - List elements that don't exist are NULL and return FALSE.
 - If any elements are NA, return FALSE. This handles out-of-bounds
   elements AND out-of-bounds slices on vectors. But it would also
   trip on valid vectors that contain an NA. I can't think of a good
   way to distinguish these two cases right now. The best way for
   this depends on the context.

 I think I am handling the most obvious special cases - though I do expect 
 this can be improved.


 is.valid - function(x,
  na.ignore = FALSE,
  null.ignore=FALSE) {

 # errors are always FALSE
 if (class(try(x, silent=TRUE)) == try-error) return(FALSE)

 # NULL is FALSE except if ignored
 if (is.null(x)) {
 if (!null.ignore) return(FALSE)
 return(TRUE)
 }

 # If all elments are NA, return FALSE except if ignored;
 if (any(is.na(x))) {
 if (!na.ignore) return(FALSE)
 return(TRUE)
 }

 # Everything else is TRUE
 return(TRUE)
 }



 # Test cases
 is.valid(1) # TRUE: valid numeric constant
 is.valid(FALSE) # TRUE: valid boolean constant
 is.valid(nonSuch)   # FALSE: object doesn't exist

 x - 1:5;
 is.valid(x) # TRUE: existing variable
 is.valid(x[4])  # TRUE: vector element
 is.valid(x[8])  # FALSE: out of bounds: NA
 is.valid(x[5:6])# FALSE: partially out of bounds: (5, NA)

 is.valid(x[8], na.ignore=TRUE)  # TRUE

 x[3] - NA
 is.valid(x) # FALSE: no element can be NA
 is.valid(x, na.ignore=TRUE)  # TRUE


 m - matrix(1:9,nrow=3, ncol=3)
 is.valid(m[2,2])# TRUE
 is.valid(m[2,4])# FALSE: subscript out of bounds
 is.valid(m[2,2,2])  # FALSE: incorrect n of dimensions

 l - list(first=7, letters, NULL)
 is.valid(l[[first]])  # TRUE: existing list elements
 is.valid(l[[4]])# FALSE: list element does not exist
 is.valid(l[[2]][27])# FALSE: out of bounds on existing element
 is.valid(l$first)   # TRUE:
 is.valid(l$second)  # FALSE: non-existent element: NULL
 is.valid(l$second, null.ignore=TRUE)  # TRUE


 Cheers,
 B.



 On Dec 20, 2014, at 11:20 AM, Rui Barradas ruipbarra...@sapo.pt wrote:

 Hello,

 Your list seems to have only 2 elements. You can check this with

 length(x)

 Or you can try

 lapply(x, is.null)

 Hope this helps,

 Rui Barradas

 Em 20-12-2014 15:58, Ragia Ibrahim escreveu:
 Hello,
 Kindly I have a list of lists as follow
 x
 [[1]]
 [1] 7

 [[2]]
 [1] 3 4 5

 as showen x[[3]] does not have a value and it has NULL, how can I check on 
 this
 how to test if x[[3]] is empty.

 thanks in advance
 Ragia

  [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
 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 -- To UNSUBSCRIBE and more, see
 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 -- To UNSUBSCRIBE and more, see
 

Re: [R] list of lists, is this element empty

2014-12-20 Thread Duncan Murdoch
This may be out of context, but on the face of it, this claim is wrong:

On 20/12/2014, 1:57 PM, Boris Steipe wrote:
Moreover is.na() behaves differently when evaluated on its own, or as
the condition of an if() statement.

The conditions in an if() statement are not evaluated in special
conditions at all.  The only way you'll get a different value is if the
argument to is.na() does tricky stuff like looking at the evaluation stack.

Duncan Murdoch

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] list of lists, is this element empty

2014-12-20 Thread Boris Steipe
Thanks. This is what I was referring to:

x - rep(NA, 3)
is.na(x)
[1] TRUE TRUE TRUE

if (is.na(x)) {print(True)}
[1] True
Warning message:
In if (is.na(x)) { :
  the condition has length  1 and only the first element will be used

You are of course right - the warning is generated by if(), not by is.na() and 
the reason for the warning is that is.na() returns a vector if applied to a 
vector. I should have been more clear.

Cheers,
B.



On Dec 20, 2014, at 3:29 PM, Duncan Murdoch murdoch.dun...@gmail.com wrote:

 This may be out of context, but on the face of it, this claim is wrong:
 
 On 20/12/2014, 1:57 PM, Boris Steipe wrote:
 Moreover is.na() behaves differently when evaluated on its own, or as
 the condition of an if() statement.
 
 The conditions in an if() statement are not evaluated in special
 conditions at all.  The only way you'll get a different value is if the
 argument to is.na() does tricky stuff like looking at the evaluation stack.
 
 Duncan Murdoch

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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] List of Lists by for Loop

2014-07-15 Thread PIKAL Petr
Hi

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Munjal Patel
 Sent: Monday, July 14, 2014 8:45 PM
 To: r-help@r-project.org
 Subject: [R] List of Lists by for Loop

 Dear Experts,
 I have a one more doubt about making list of lists.
 Here is the simple code i have made.
 I am doing the following for only one digit=20

You can try something like that (untested)

numbers-c(20, 30, 40, 50)
master-vector(length(numbers), mode=list)
k - 0
for(j in numbers)
{
k-k+1
a=vector(j,mode=list)
b=vector(j,mode=list)

 for (i in 1:j){
   #Do Calculation
   a[[i]]=data.frame()
   b[[i]]=data.frame()
}
master[[k]] - list(a, b)
}

Regards
Petr



 Now i have to repeat the whole process for Digits=c(30,40,50,60)
 In short I want the Master list containing following
 List 1(#20)
  List a
  List b

 List 2(#30)
  List a
  List b

 so on
 Can you please guide me in this ?
 Thank you very much.
 Sincerely

 --
 Munjal Patel

   [[alternative HTML version deleted]]

 __
 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.


Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny 
pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně 
jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze 
svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email 
jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či 
zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a 
to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; 
Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce 
s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným 
dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost 
žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně 
pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně 
osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi 
či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are 
intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. 
Delete the contents of this e-mail with all attachments and its copies from 
your system.
If you are not the intended recipient of this e-mail, you are not authorized to 
use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by 
modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a 
contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately 
accept such offer; The sender of this e-mail (offer) excludes any acceptance of 
the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an 
express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into 
any contracts on behalf of the company except for cases in which he/she is 
expressly authorized to do so in writing, and such authorization or power of 
attorney is submitted to the recipient or the person represented by the 
recipient, or the existence of such authorization is known to the recipient of 
the person represented by the recipient.
__
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] List of Lists in For Loop

2014-07-15 Thread Adams, Jean
Munjal,

Something like this should work:

Digits = c(20, 30, 40, 50, 60)
result = vector(length(Digits), mode=list)
names(result) = Digits
for(j in seq(Digits)) {
a = vector(Digits[j], mode=list)
 b = vector(Digits[j], mode=list)
for(i in 1:Digits[j]) {
 #Do Calculation
a[[i]] = data.frame()
 b[[i]] = data.frame()
}
 result[[j]] = list(a, b)
}

Jean


On Mon, Jul 14, 2014 at 1:35 PM, Munjal Patel munjalpate...@gmail.com
wrote:

 Dear Experts,
 I have a one more doubt about making list of lists.
 Here is the simple code i have made.
 I am doing the following for only one digit=20
 a=vector(20,mode=list)
 b=vector(20,mode=list)
 for (i in 1:20){
   #Do Calculation
   a[[i]]=data.frame()
   b[[i]]=data.frame()
 }
 Now i have to repeat the whole process for Digits=c(30,40,50,60)
 In short I want the Master list containing following
 List 1(#20)
  List a
  List b

 List 2(#30)
  List a
  List b

 so on
 Can you please guide me in this ?
 Thank you very much.
 Sincerely

 --
 Munjal

 [[alternative HTML version deleted]]

 __
 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.


[[alternative HTML version deleted]]

__
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] List of Lists by for Loop

2014-07-14 Thread Munjal Patel
Dear Experts,
I have a one more doubt about making list of lists.
Here is the simple code i have made.
I am doing the following for only one digit=20
a=vector(20,mode=list)
b=vector(20,mode=list)
for (i in 1:20){
  #Do Calculation
  a[[i]]=data.frame()
  b[[i]]=data.frame()
}

Now i have to repeat the whole process for Digits=c(30,40,50,60)
In short I want the Master list containing following
List 1(#20)
 List a
 List b

List 2(#30)
 List a
 List b

so on
Can you please guide me in this ?
Thank you very much.
Sincerely

-- 
Munjal Patel

[[alternative HTML version deleted]]

__
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] List of Lists in For Loop

2014-07-14 Thread Munjal Patel
Dear Experts,
I have a one more doubt about making list of lists.
Here is the simple code i have made.
I am doing the following for only one digit=20
a=vector(20,mode=list)
b=vector(20,mode=list)
for (i in 1:20){
  #Do Calculation
  a[[i]]=data.frame()
  b[[i]]=data.frame()
}
Now i have to repeat the whole process for Digits=c(30,40,50,60)
In short I want the Master list containing following
List 1(#20)
 List a
 List b

List 2(#30)
 List a
 List b

so on
Can you please guide me in this ?
Thank you very much.
Sincerely

-- 
Munjal

[[alternative HTML version deleted]]

__
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] List of lists

2013-08-01 Thread mohan . radhakrishnan
Hi,

The solution that works now is

closeAllConnections()

But even if I replace the 'list' with the 'array' I get a similar error.

Exception is  Error in UseMethod(\close\): no applicable method for
'close' applied to an object of class \c('double', 'numeric')\\n

#Write each CPU's utilization data into a
#separate file
filelist.array - function(n){
  cpufile - list()
  cpufiledescriptors - array(n,dim=c(0,n))
  length(cpufile) - n
  for (i in 1:n) {
cpufile[[i]] - paste(output, i, .txt, sep = )
cpufiledescriptors[i]-file( cpufile[[i]], a )
  }
listoffiles - list(cpufile=cpufile,
cpufiledescriptors=cpufiledescriptors)
return (listoffiles)
}

Thanks,
Mohan





   Re: [R] List of lists


   Jim Lemon
  to:   
 mohan.radhakrishnan
   31-07-2013 05:19 
 PM 







On 07/31/2013 04:18 PM, mohan.radhakrish...@polarisft.com wrote:
 Hi Jim,

  close(filedescriptors$cpufiledescriptors[[1]])
  close(filedescriptors$cpufiledescriptors[[2]])
  close(filedescriptors$cpufiledescriptors[[3]])

I might be doing something wrong. Error is

 Error in UseMethod(close) :
no applicable method for 'close' applied to an object of class c
 ('integer', 'numeric')

Hi Mohan,
I just ran the code within your function filelist.array and then was
able to close the connections I had created. I entered your function in R:

filelist.array- function(n){
 cpufile- list()
 cpufiledescriptors- list()
 length(cpufile)- n
 for (i in 1:n) {
   cpufile[[i]]- paste(output, i, .txt, sep = )
  cpufiledescriptors[[i]]-file( cpufile[[i]],
a )
 }
   listoffiles- list(cpufile=cpufile,
  cpufiledescriptors=cpufiledescriptors)
  return (listoffiles)
}

and then:

filedescriptors-filelist.array(3)
close(filedescriptors$cpufiledescriptors[[1]])
close(filedescriptors$cpufiledescriptors[[2]])
close(filedescriptors$cpufiledescriptors[[3]])

and it worked fine. Perhaps a typo in your code somewhere?

Jim




This e-Mail may contain proprietary and confidential information and is sent 
for the intended recipient(s) only.  If by an addressing or transmission error 
this mail has been misdirected to you, you are requested to delete this mail 
immediately. You are also hereby notified that any use, any form of 
reproduction, dissemination, copying, disclosure, modification, distribution 
and/or publication of this e-mail message, contents or its attachment other 
than by its intended recipient/s is strictly prohibited.

Visit us at http://www.polarisFT.com

__
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] List of lists

2013-07-31 Thread mohan . radhakrishnan
Hi Jim,

close(filedescriptors$cpufiledescriptors[[1]])
close(filedescriptors$cpufiledescriptors[[2]])
close(filedescriptors$cpufiledescriptors[[3]])

  I might be doing something wrong. Error is

   Error in UseMethod(close) :
  no applicable method for 'close' applied to an object of class c
('integer', 'numeric')


Thanks,
Mohan





   Re: [R] List of lists


   Jim Lemon
  to:   
 mohan.radhakrishnan, R-help@r-project.org  
   31-07-2013 03:05 
 AM 







On 07/30/2013 10:05 PM, mohan.radhakrish...@polarisft.com wrote:

 Hi,
 I am creating a list of 2 lists, one containing filenames
 and the other file descriptors.  When I retrieve them I am  unable to
close
 the file descriptor.

 I am getting this error when I try to call close(filedescriptors
 [[2]][[1]]).

 Error in UseMethod(close) :
no applicable method for 'close' applied to an object of class c
 ('integer', 'numeric')

 print(filedescriptors[[2]][[1]]) seems to be printing individual
elements.

 Thanks,
 Mohan

 filelist.array- function(n){
cpufile- list()
cpufiledescriptors- list()
length(cpufile)- n
for (i in 1:n) {
  cpufile[[i]]- paste(output, i, .txt, sep = )
cpufiledescriptors[[i]]-file( cpufile[[i]], a )
}
  listoffiles- list(cpufile=cpufile,
 cpufiledescriptors=cpufiledescriptors)
return (listoffiles)
 }



 #Test function

 test.filelist.array- function() {
filedescriptors- filelist.array(3)
  print(filedescriptors[[2]][[1]])
  print(filedescriptors[[2]][[2]])
  print(filedescriptors[[2]][[3]])

 }


Hi Mohan,
When you have opened connections as above, you need to pass the
connection, not just one element, to close:

close(listoffiles$cpufiledescriptors[[1]])

Jim





This e-Mail may contain proprietary and confidential information and is sent 
for the intended recipient(s) only.  If by an addressing or transmission error 
this mail has been misdirected to you, you are requested to delete this mail 
immediately. You are also hereby notified that any use, any form of 
reproduction, dissemination, copying, disclosure, modification, distribution 
and/or publication of this e-mail message, contents or its attachment other 
than by its intended recipient/s is strictly prohibited.

Visit us at http://www.polarisFT.com

__
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] List of lists

2013-07-30 Thread mohan . radhakrishnan

Hi,
   I am creating a list of 2 lists, one containing filenames
and the other file descriptors.  When I retrieve them I am  unable to close
the file descriptor.

I am getting this error when I try to call close(filedescriptors
[[2]][[1]]).

Error in UseMethod(close) :
  no applicable method for 'close' applied to an object of class c
('integer', 'numeric')

print(filedescriptors[[2]][[1]]) seems to be printing individual elements.

Thanks,
Mohan

filelist.array - function(n){
  cpufile - list()
  cpufiledescriptors - list()
  length(cpufile) - n
  for (i in 1:n) {
cpufile[[i]] - paste(output, i, .txt, sep = )
cpufiledescriptors[[i]]-file( cpufile[[i]], a )
  }
listoffiles - list(cpufile=cpufile,
cpufiledescriptors=cpufiledescriptors)
return (listoffiles)
}



#Test function

test.filelist.array - function() {
filedescriptors - filelist.array(3)
print(filedescriptors[[2]][[1]])
print(filedescriptors[[2]][[2]])
print(filedescriptors[[2]][[3]])

}



This e-Mail may contain proprietary and confidential information and is sent 
for the intended recipient(s) only.  If by an addressing or transmission error 
this mail has been misdirected to you, you are requested to delete this mail 
immediately. You are also hereby notified that any use, any form of 
reproduction, dissemination, copying, disclosure, modification, distribution 
and/or publication of this e-mail message, contents or its attachment other 
than by its intended recipient/s is strictly prohibited.

Visit us at http://www.polarisFT.com

__
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] List of lists

2013-07-30 Thread Jim Lemon

On 07/30/2013 10:05 PM, mohan.radhakrish...@polarisft.com wrote:


Hi,
I am creating a list of 2 lists, one containing filenames
and the other file descriptors.  When I retrieve them I am  unable to close
the file descriptor.

I am getting this error when I try to call close(filedescriptors
[[2]][[1]]).

Error in UseMethod(close) :
   no applicable method for 'close' applied to an object of class c
('integer', 'numeric')

print(filedescriptors[[2]][[1]]) seems to be printing individual elements.

Thanks,
Mohan

filelist.array- function(n){
   cpufile- list()
   cpufiledescriptors- list()
   length(cpufile)- n
   for (i in 1:n) {
 cpufile[[i]]- paste(output, i, .txt, sep = )
cpufiledescriptors[[i]]-file( cpufile[[i]], a )
   }
 listoffiles- list(cpufile=cpufile,
cpufiledescriptors=cpufiledescriptors)
return (listoffiles)
}



#Test function

test.filelist.array- function() {
filedescriptors- filelist.array(3)
 print(filedescriptors[[2]][[1]])
 print(filedescriptors[[2]][[2]])
 print(filedescriptors[[2]][[3]])

}



Hi Mohan,
When you have opened connections as above, you need to pass the 
connection, not just one element, to close:


close(listoffiles$cpufiledescriptors[[1]])

Jim

__
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] list of lists to matrix

2013-01-08 Thread John Kane
Please supply some sample data. 
 The easiest way to supply data  is to use the dput() function.  Example with 
your file named testfile: 
dput(testfile) 

Then copy the output and paste into your email.  For large data sets, you can 
just supply a representative sample.  Usually, 
dput(head(testfile, 100)) will be sufficient.   

Simpe example:
 aalist - list(aa = c(3.0, 2.9, 2.7), bb = c(0.86, 0.76, 0.66), 
cc= c(0.07, 0.04, 0.04), cc = c(a, b, c))
  
   dput(aalist) 

The attached text file was useful but actual or example data is much better.

John Kane
Kingston ON Canada


 -Original Message-
 From: eliza_bo...@hotmail.com
 Sent: Mon, 7 Jan 2013 16:13:03 +
 To: r-help@r-project.org
 Subject: [R] list of lists to matrix
 
 
 dear R family,
 [a text file has been attached for better understanding]
 i have a list of 16 and each of of that is further subdivided into
 variable number of lists. So, i have a kind of list into lists
 phenomenon.
 [[1]]$'1'
 1   2  3   4   5  6
 7  8   9
 
 [[1]]$'2'
 1   2  3   4   5  6
 7  8   9
 i want to convert both these sublists into one column and then cbind it
 in the following way
 col1 col2
 1   1
 2   2
 3   3..
 9 9
 
 i want to the same operations on all the 16 lists.
 thanks in advance,
 
 elisa
 __
 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.


FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!

__
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] list of lists to matrix

2013-01-07 Thread eliza botto

dear R family,
[a text file has been attached for better understanding]
i have a list of 16 and each of of that is further subdivided into variable 
number of lists. So, i have a kind of list into lists phenomenon.
[[1]]$'1'
1   2  3   4   5  6
7  8   9

[[1]]$'2'
1   2  3   4   5  6
7  8   9
i want to convert both these sublists into one column and then cbind it in the 
following way
col1 col2
1   1
2   2
3   3..
9 9

i want to the same operations on all the 16 lists. 
thanks in advance,

elisa dear R family,

i have a list of 16 and each of of that is further subdivided into variable 
number of lists. So, i have a kind of list of lists phenomenon.

[[1]]$'1'

1   2  3
   
4   5  6

7  8   9


[[1]]$'2'

1   2  3
   
4   5  6

7  8   9

i want to convert both these sublists into one column and then cbind it in the 
following way

col1 col2

1   1

2   2

3   3
.
.

9 9


i want to the same operations on all the 16 lists. 

thanks in advance,

elisa__
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] list of lists to matrix

2013-01-07 Thread R. Michael Weylandt
Something like

do.call(cbind, lists)

?

MW

On Mon, Jan 7, 2013 at 4:13 PM, eliza botto eliza_bo...@hotmail.com wrote:

 dear R family,
 [a text file has been attached for better understanding]
 i have a list of 16 and each of of that is further subdivided into variable 
 number of lists. So, i have a kind of list into lists phenomenon.
 [[1]]$'1'
 1   2  3   4   5  6
 7  8   9

 [[1]]$'2'
 1   2  3   4   5  6
 7  8   9
 i want to convert both these sublists into one column and then cbind it in 
 the following way
 col1 col2
 1   1
 2   2
 3   3..
 9 9

 i want to the same operations on all the 16 lists.
 thanks in advance,

 elisa
 __
 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] list of lists to matrix

2013-01-07 Thread eliza botto

Thanks arun and weylandt,it perfectly worked out..
elisa

 From: michael.weyla...@gmail.com
 Date: Mon, 7 Jan 2013 16:37:53 +
 Subject: Re: [R] list of lists to matrix
 To: eliza_bo...@hotmail.com
 CC: r-help@r-project.org
 
 Something like
 
 do.call(cbind, lists)
 
 ?
 
 MW
 
 On Mon, Jan 7, 2013 at 4:13 PM, eliza botto eliza_bo...@hotmail.com wrote:
 
  dear R family,
  [a text file has been attached for better understanding]
  i have a list of 16 and each of of that is further subdivided into variable 
  number of lists. So, i have a kind of list into lists phenomenon.
  [[1]]$'1'
  1   2  3   4   5  6
  7  8   9
 
  [[1]]$'2'
  1   2  3   4   5  6
  7  8   9
  i want to convert both these sublists into one column and then cbind it in 
  the following way
  col1 col2
  1   1
  2   2
  3   3..
  9 9
 
  i want to the same operations on all the 16 lists.
  thanks in advance,
 
  elisa
  __
  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.
 
  
[[alternative HTML version deleted]]

__
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] List of lists to data frame?

2011-11-17 Thread Bert Gunter
I don't know if this is faster, but ...

out - do.call(rbind,
   lapply(s, function(x)data.frame(x$category,x$name,as.vector(x$series

## You can then name the columns of out via names()

Note: No fancy additional packages are required.

-- Bert

On Wed, Nov 16, 2011 at 6:39 PM, Kevin Burton rkevinbur...@charter.net wrote:
 Say I have the following data:

 s - list()
 s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10,
 start=c(2000,1)), category=top)
 s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10,
 start=c(2000,2)), category=next)

 If I use unlist since this is a list of lists I don't end up with a data
 frame. And the number of rows in the data frame should equal the number of
 time series entries. In the sample above it would be 110. I would expect
 that the name and category strings would be recycled for each row. My brute
 force code attempts to build the data frame by appending to the master data
 frame but like I said it is *very* slow.

 Kevin

 -Original Message-
 From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com]
 Sent: Wednesday, November 16, 2011 5:26 PM
 To: rkevinbur...@charter.net
 Cc: r-help@r-project.org
 Subject: Re: [R] List of lists to data frame?

 unlist(..., recursive = F)

 Michael

 On Wed, Nov 16, 2011 at 6:20 PM,  rkevinbur...@charter.net wrote:

 I would like to make the following faster:

        df - NULL
        for(i in 1:length(s))
        {
                df - rbind(df, cbind(names(s[i]), time(s[[i]]$series),
 as.vector(s[[i]]$series), s[[i]]$category))
        }
        names(df) - c(name, time, value, category)
        return(df)

 The s object is a list of lists. It is constructed like:

 s[[object]] - list(. . . . . .)

 where object would be the name associated with this list
 s[[i]]$series is a 'ts' object and s[[i]]$category is a name.

 Constructing this list is reasonably fast but to do some more
 processing on the data it would be easier if it were converted to a data
 frame.
 Right now the above code is unacceptably slow at converting this list
 of lists to a data frame. Any suggestions on how to optimize this are
 welcome.

 Thank you.

 Kevin

        [[alternative HTML version deleted]]

 __
 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.




-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] List of lists to data frame?

2011-11-16 Thread rkevinburton

I would like to make the following faster:

df - NULL
for(i in 1:length(s))
{
df - rbind(df, cbind(names(s[i]), time(s[[i]]$series), 
as.vector(s[[i]]$series), s[[i]]$category))
}
names(df) - c(name, time, value, category)
return(df)

The s object is a list of lists. It is constructed like:

s[[object]] - list(. . . . . .)

where object would be the name associated with this list s[[i]]$series 
is a 'ts' object and s[[i]]$category is a name.

Constructing this list is reasonably fast but to do some more processing 
on the data it would be easier if it were converted to a data frame. 
Right now the above code is unacceptably slow at converting this list of 
lists to a data frame. Any suggestions on how to optimize this are 
welcome.

Thank you.

Kevin

[[alternative HTML version deleted]]

__
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] List of lists to data frame?

2011-11-16 Thread R. Michael Weylandt
unlist(..., recursive = F)

Michael

On Wed, Nov 16, 2011 at 6:20 PM,  rkevinbur...@charter.net wrote:

 I would like to make the following faster:

        df - NULL
        for(i in 1:length(s))
        {
                df - rbind(df, cbind(names(s[i]), time(s[[i]]$series),
 as.vector(s[[i]]$series), s[[i]]$category))
        }
        names(df) - c(name, time, value, category)
        return(df)

 The s object is a list of lists. It is constructed like:

 s[[object]] - list(. . . . . .)

 where object would be the name associated with this list s[[i]]$series
 is a 'ts' object and s[[i]]$category is a name.

 Constructing this list is reasonably fast but to do some more processing
 on the data it would be easier if it were converted to a data frame.
 Right now the above code is unacceptably slow at converting this list of
 lists to a data frame. Any suggestions on how to optimize this are
 welcome.

 Thank you.

 Kevin

        [[alternative HTML version deleted]]

 __
 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] List of lists to data frame?

2011-11-16 Thread Kevin Burton
Say I have the following data:

 s - list()
 s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10,
start=c(2000,1)), category=top)
 s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10,
start=c(2000,2)), category=next)

If I use unlist since this is a list of lists I don't end up with a data
frame. And the number of rows in the data frame should equal the number of
time series entries. In the sample above it would be 110. I would expect
that the name and category strings would be recycled for each row. My brute
force code attempts to build the data frame by appending to the master data
frame but like I said it is *very* slow.

Kevin

-Original Message-
From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] 
Sent: Wednesday, November 16, 2011 5:26 PM
To: rkevinbur...@charter.net
Cc: r-help@r-project.org
Subject: Re: [R] List of lists to data frame?

unlist(..., recursive = F)

Michael

On Wed, Nov 16, 2011 at 6:20 PM,  rkevinbur...@charter.net wrote:

 I would like to make the following faster:

        df - NULL
        for(i in 1:length(s))
        {
                df - rbind(df, cbind(names(s[i]), time(s[[i]]$series), 
 as.vector(s[[i]]$series), s[[i]]$category))
        }
        names(df) - c(name, time, value, category)
        return(df)

 The s object is a list of lists. It is constructed like:

 s[[object]] - list(. . . . . .)

 where object would be the name associated with this list 
 s[[i]]$series is a 'ts' object and s[[i]]$category is a name.

 Constructing this list is reasonably fast but to do some more 
 processing on the data it would be easier if it were converted to a data
frame.
 Right now the above code is unacceptably slow at converting this list 
 of lists to a data frame. Any suggestions on how to optimize this are 
 welcome.

 Thank you.

 Kevin

        [[alternative HTML version deleted]]

 __
 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] List of lists to data frame?

2011-11-16 Thread Sarah Goslee
On Wed, Nov 16, 2011 at 9:39 PM, Kevin Burton rkevinbur...@charter.net wrote:
 Say I have the following data:

 s - list()
 s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10,
 start=c(2000,1)), category=top)
 s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10,
 start=c(2000,2)), category=next)

 If I use unlist since this is a list of lists I don't end up with a data
 frame. And the number of rows in the data frame should equal the number of
 time series entries. In the sample above it would be 110. I would expect
 that the name and category strings would be recycled for each row. My brute
 force code attempts to build the data frame by appending to the master data
 frame but like I said it is *very* slow.

Appending is very slow, and should be avoided. Instead, create a data frame
of the correct size before starting the loop, and add each new bit into the
appropriate place.

There may well be a more efficient solution (I don't quite understand
what your objective is), but simply getting rid of the rbind() within a
loop will help.


 Kevin

 -Original Message-
 From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com]
 Sent: Wednesday, November 16, 2011 5:26 PM
 To: rkevinbur...@charter.net
 Cc: r-help@r-project.org
 Subject: Re: [R] List of lists to data frame?

 unlist(..., recursive = F)

 Michael

 On Wed, Nov 16, 2011 at 6:20 PM,  rkevinbur...@charter.net wrote:

 I would like to make the following faster:

        df - NULL
        for(i in 1:length(s))
        {
                df - rbind(df, cbind(names(s[i]), time(s[[i]]$series),
 as.vector(s[[i]]$series), s[[i]]$category))
        }
        names(df) - c(name, time, value, category)
        return(df)

 The s object is a list of lists. It is constructed like:

 s[[object]] - list(. . . . . .)

 where object would be the name associated with this list
 s[[i]]$series is a 'ts' object and s[[i]]$category is a name.

 Constructing this list is reasonably fast but to do some more
 processing on the data it would be easier if it were converted to a data
 frame.
 Right now the above code is unacceptably slow at converting this list
 of lists to a data frame. Any suggestions on how to optimize this are
 welcome.

 Thank you.

 Kevin

-- 
Sarah Goslee
http://www.functionaldiversity.org

__
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] List of lists to data frame?

2011-11-16 Thread christiaan pauw
On the face of it this looks like a job for ldply() in the plyr package
which specialises in taking things apart and putting them back together.
 ldply()  applies a function for each element of a list and then combine
results into a data frame



On 17 November 2011 04:53, Sarah Goslee sarah.gos...@gmail.com wrote:

 On Wed, Nov 16, 2011 at 9:39 PM, Kevin Burton rkevinbur...@charter.net
 wrote:
  Say I have the following data:
 
  s - list()
  s[[A]] - list(name=first, series=ts(rnorm(50), frequency=10,
  start=c(2000,1)), category=top)
  s[[B]] - list(name=second, series=ts(rnorm(60), frequency=10,
  start=c(2000,2)), category=next)
 
  If I use unlist since this is a list of lists I don't end up with a data
  frame. And the number of rows in the data frame should equal the number
 of
  time series entries. In the sample above it would be 110. I would expect
  that the name and category strings would be recycled for each row. My
 brute
  force code attempts to build the data frame by appending to the master
 data
  frame but like I said it is *very* slow.

 Appending is very slow, and should be avoided. Instead, create a data frame
 of the correct size before starting the loop, and add each new bit into the
 appropriate place.

 There may well be a more efficient solution (I don't quite understand
 what your objective is), but simply getting rid of the rbind() within a
 loop will help.




[[alternative HTML version deleted]]

__
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] List of lists ?

2010-08-09 Thread Carlos Petti
Dear list,

I have to use a list of lists containing vectors.

For instance :

[[1]]
[[1]][[1]]
[1] 1 2 3

[[1]][[2]]
[1] 3 2 1

I want to attribute vectors to the main list

without use of an intermediate list,

but it does not work :

x - list()
x[[1]][[1]] - c(1, 2, 3)
x[[1]][[2]] - c(3, 2, 1)

Thanks in advance,
Carlos

__
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] List of lists ?

2010-08-09 Thread David Winsemius


On Aug 9, 2010, at 12:57 PM, Carlos Petti wrote:


Dear list,

I have to use a list of lists containing vectors.

For instance :

[[1]]
[[1]][[1]]
[1] 1 2 3

[[1]][[2]]
[1] 3 2 1

I want to attribute vectors to the main list

without use of an intermediate list,

but it does not work :

More specifically it produces an error that has information in it.
 x[[1]][[1]] - c(1, 2, 3)
Error in `*tmp*`[[1]] : subscript out of bounds



x - list()
x[[1]][[1]] - c(1, 2, 3)
x[[1]][[2]] - c(3, 2, 1)


So thinking perhaps we just needed another level of subscripting  
available I tried:


 x - list(list())
 x[[1]][[1]] - c(1, 2, 3)
 x[[1]][[2]] - c(3, 2, 1)
 x
[[1]]
[[1]][[1]]
[1] 1 2 3

[[1]][[2]]
[1] 3 2 1

Success. Moral: Read the error messages for meaning or at least clues.  
(Further testing showed that almost anything inside the original  
list() call, even NULL,  would have created enough structure for the  
interpreter to work with.






David Winsemius, MD
West Hartford, CT

__
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] List of lists ?

2010-08-09 Thread jim holtman
Is this what you want:

 x - list()
 x[[1]] - list(1:3)
 x[[2]] - list(3:1)
 x
[[1]]
[[1]][[1]]
[1] 1 2 3


[[2]]
[[2]][[1]]
[1] 3 2 1



On Mon, Aug 9, 2010 at 12:57 PM, Carlos Petti carlos.pe...@gmail.com wrote:
 Dear list,

 I have to use a list of lists containing vectors.

 For instance :

 [[1]]
 [[1]][[1]]
 [1] 1 2 3

 [[1]][[2]]
 [1] 3 2 1

 I want to attribute vectors to the main list

 without use of an intermediate list,

 but it does not work :

 x - list()
 x[[1]][[1]] - c(1, 2, 3)
 x[[1]][[2]] - c(3, 2, 1)

 Thanks in advance,
 Carlos

 __
 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.




-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

__
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] List of Lists

2009-01-14 Thread David Winsemius

See if one of %in% or match gets your further.

 1:10 %in% c(1,3,5,9)
 [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
 match(c(1,3,5,9), 1:10)
[1] 1 3 5 9
 match(c(1,3,5,9), 10:1)
[1] 10  8  6  2

date03 as offered was not a list, but a vector.

date04 - date02[which(date02$date %in% date3), ]  # might work,  
nothing to test it on


If you only want the column of matching dates and not all the rows  
that have matching dates then this might work:

date04 - date02[which(date02$date %in% date3), date ]

From you incorrect use of the term list (in the context of R,  
anyway), I am guessing that you don't really want lists but rather  
subsets of data.frames. Vector and list are not interchangeable terms  
in these parts.



--
David Winsemius

On Jan 14, 2009, at 2:53 PM, glenn wrote:


Dear All;

Is it possible to create a list of lists (I am sure it is) along these
lines;

I have a dataframe data02 that holds a lot of information, and the  
first

column is “date”

I have a list of dates in;

data03-c(date1,.,daten)

And would like to create a list;

data04 - subset(data02, date == data03[1,])

Ie. data04 holds the data from data02 that matches a date in data03

How do I create a list data04 that instead rolls through all the  
elements of

data03 and each element of data04 is a list

Regards

Glenn

[[alternative HTML version deleted]]

__
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] List of Lists

2009-01-14 Thread hadley wickham
On Wed, Jan 14, 2009 at 1:53 PM, glenn g1enn.robe...@btinternet.com wrote:
 Dear All;

 Is it possible to create a list of lists (I am sure it is) along these
 lines;

 I have a dataframe data02 that holds a lot of information, and the first
 column is ³date²

 I have a list of dates in;

 data03-c(date1,.,daten)

 And would like to create a list;

 data04 - subset(data02, date == data03[1,])

 Ie. data04 holds the data from data02 that matches a date in data03

 How do I create a list data04 that instead rolls through all the elements of
 data03 and each element of data04 is a list

Have a look at ?split.

Hadley

-- 
http://had.co.nz/

__
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.