[R] Re moving unwanted double values in list

Jim Regetz regetz at nceas.ucsb.edu
Wed Apr 29 22:54:28 CEST 2009


Hi Josh,

Comments below.

Josh Roll wrote:
> 
>   What you suggested works perfectly for the question i posed.  Hoever, 
> on further inspection by additional numbers cannot be thrown out and 
> instead need to be added to the list somehow.  I tried specifying the 
> list dimensions but that causes problems and doesnt fix my problem.  The 
> procedure that creates the list below is referencing list accTAZ below
>  
> [1]   6476936.4    624214.8   2652368.4    536223.6  10255766.4  
> 35486589.6   1443578.4
>   [8]  22289652.0   3857673.6   7682241.6   8546907.6  11657962.8    
> 339332.4   3337567.2
>  
> against  list VAC_ACRES:
>  
> TAZ   VAC_ACRES
> 1   100  45043653.6
> 2   101   4471869.6
> 3   102   9579715.2
> 4   103  13336329.6
> 5   104   5841396.0
> 6   105    920858.4
> 7   106  15816200.4
> 8   107  14691481.2
> 9   108  21712046.4
> 10  109  18699436.8
> 
> and returning matching TAZ values resulting in TAZS.  The code is as such:
>  
> for(j in 1:accTAZlength){
>    
>   #Creates list of initially accepted TAZs by TAZ number
>   TAZS[[j]]=TAZ_VAC_FEET[TAZ_VAC_FEET$VAC_ACRES==accTAZ[j],1]
> 
>  
> }
>  
> The issue is when a accTAZ value is referenced and multiple TAZ values 
> are returned for identical VAC_ACRES values which shouldnt occur but 
> does becasue of the existence of duplicate values.  I now need to cross 
> reference the TAZS with an earlier list to make sure there are not TAZ 
> values present that shouldnt be but i cant figure out how to compare a 
> list with one column to one that in some places has 2-3 such as :
>  
> 100           630
> 101           636
> 102           242, 534, 637
> 103           638
> 104           643
> 106           651, 801
> 107           654
> 
> Is there any way i can simply turn the list above from having 7 rows to 
> 10 rows.  Having the rows with multiple values just returned to the 
> bottom of the list?

Lists don't have rows, they have elements. In the case of TAZS, the 
elements are vectors, each containing one or more values. You can 
simplify this into a single vector of (all) values using 'unlist'. See 
if this example helps:

 > foo <- list(102, 100, c(104, 101), 103)
 > foo
[[1]]
[1] 102

[[2]]
[1] 100

[[3]]
[1] 104 101

[[4]]
[1] 103

 > unlist(foo)
[1] 102 100 104 101 103

This doesn't put the values from length>1 vectors at the end, but if you 
really want that, you could do:

unlist(foo[order(sapply(foo, length))])


But taking a step back: if in the first place all you really want is a 
vector of the TAZ values for which the associated VAC_ACRES value is 
found in accTAZ, you don't need to do any of this stuff (including your 
loop). Consider instead:

TAZ_VAC_FEET$TAZ[TAZ_VAC_FEET$VAC_ACRES %in% accTAZ]

Hope that helps,
Jim

> Hope this is all clear.  Thanks
>  
> Cheers,
> JR
>  
>  
>  
>  
>  > Date: Tue, 28 Apr 2009 21:12:27 -0700
>  > From: regetz at nceas.ucsb.edu
>  > To: J_R_36 at hotmail.com
>  > CC: r-help at r-project.org
>  > Subject: Re: Re moving unwanted double values in list
>  >
>  > PDXRugger wrote:
>  > > I have a procedure that goes sorts out some numbers based on specidifed
>  > > criteria and for some reason the list contains double values in 
> some of the
>  > > rows such as:
>  > >
>  > > TAZs <-
>  > >
>  > > [[84]]
>  > > [1] 638
>  > >
>  > > [[85]]
>  > > [1] 643
>  > >
>  > > [[86]]
>  > > [1] 644 732
>  > >
>  > > [[87]]
>  > > [1] 651 801
>  > >
>  > > i would like to check list TAZs for double values and remove any if 
> present.
>  > > I have tried
>  > >
>  > > if (length(TAZDs==2))
>  > > rm(TAZDs[2])
>  > > but no luck. I cant find nor think of another way. Any help would be
>  > > helpful. Thanks in advance
>  > >
>  >
>  >
>  > Try this:
>  >
>  > TAZs[sapply(TAZs, length)!=2]
>  >
>  > Cheers,
>  > Jim




More information about the R-help mailing list