[R] list manipulation

Thompson, David (MNR) David.John.Thompson at ontario.ca
Tue May 6 20:56:02 CEST 2008


Thanks Mark,

Your suggestion led me to this:

> !is.null(lapply(ls(pattern='bn'), function(y) cat(y, dim(get(y)),
"\t", names(get(y)), "\n")))
bn1993 2885 11   oplt rplt rsiz tree bd ht oaz odst raz rdst spr 
bn1994 3158 7    oplt tree bd ht spr stat dam 
bn1995 734 7     oplt tree bd ht spr stat dam 
bn1996 293 7     oplt tree bd ht spr stat dam 
bn1997 264 7     oplt tree bd ht spr stat dam 
bn1998 768 7     oplt tree bd ht spr stat dam 
bn1999 654 7     oplt tree bd ht dbh stat dam 
bn2003 1407 9    oplt tree bd94 ht94 ht99 ht02 ht03 stat dam 
[1] TRUE

Which is far cleaner and more compact (which was probably what I was
_actually_ looking for) than what I had started with.

Thanx, DaveT.
>-----Original Message-----
>From: markleeds at verizon.net [mailto:markleeds at verizon.net] 
>Sent: May 6, 2008 02:47 PM
>To: Thompson, David (MNR)
>Subject: RE: [R] list manipulation
>
>  Hi: i would guess that you are sending objects into the lapply that 
>dim and names don't work on so you get NULL in those cases.
>you can probably figure  out what's happening by doing 
>ls(pattern='bn') 
>and seeing what gets returned ? There could be some other
>reason but that's my best guess.
>
>if you  don't care why and you just want to get rid of them you can do
>
>initialoutput<-lapply( that stuff)
>finaloutput<-initialoutput[!sapply(initialoutput,is.null]
>print(finaloutput)
>
>i'm no R expert by any stretch but, if you want to get better 
>at R , the 
>best way is to live on this list if possible and watch the 
>questions and 
>solutions.
>for me, it beats any book out there. good luck and let me know if 
>anything above is unclear or doesn't work.
>
>
>
>
>
>On Tue, May 6, 2008 at  2:35 PM, Thompson, David (MNR) wrote:
>
>> Hello,
>>
>> I have a set of one-liners (many thanks to previous responses from 
>> this
>> list) that I use to look at newly imported data sets with functions 
>> like
>> dim(), names(), str(), etc. within lapply(). Generally, 
>these commands
>> work for me but, I am apparently still missing some aspect of list
>> manipulation. I don't understand why I get a set of NULL 
>list elements
>> at the end of each output as demonstrated below. How can I generate 
>> this
>> (and similar) result(s) without all the trailing NULLs?
>>
>>> lapply(ls(pattern='bn'), function(x) cat(x, dim(get(x)), "\t",
>> names(get(x)), "\n"))
>> bn1993 2885 11   oplt rplt rsiz tree bd ht oaz odst raz rdst spr 
>> bn1994 3158 7    oplt tree bd ht spr stat dam bn1995 734 7     oplt 
>> tree bd ht spr stat dam bn1996 293 7     oplt tree bd ht spr 
>stat dam 
>> bn1997 264 7     oplt tree bd ht spr stat dam bn1998 768 7     oplt 
>> tree bd ht spr stat dam bn1999 654 7     oplt tree bd ht dbh 
>stat dam 
>> bn2003 1407 9    oplt tree bd94 ht94 ht99 ht02 ht03 stat dam [[1]]
>> NULL
>>
>> [[2]]
>> NULL
>>
>> [[3]]
>> NULL
>>
>> [[4]]
>> NULL
>>
>> [[5]]
>> NULL
>>
>> [[6]]
>> NULL
>>
>> [[7]]
>> NULL
>>
>> [[8]]
>> NULL
>>
>> Thanx, DaveT.
>> *************************************
>> Silviculture Data Analyst
>> Ontario Forest Research Institute
>> Ontario Ministry of Natural Resources
>> david.john.thompson at ontario.ca
>> http://ofri.mnr.gov.on.ca
>>
>> ______________________________________________
>> 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.
>



More information about the R-help mailing list