[R] need any advises for code optimization.

Rich FitzJohn rich.fitzjohn at gmail.com
Mon Apr 4 12:58:09 CEST 2005


Hi again,

The arguments to %in% are in the wrong order in your version.  Because
of that, the statement
  row.names(to.drop) %in% row.names(whole)
will be TRUE for the first nrow(to.drop) elements, and FALSE for the remainder.

To fix it, just switch the order around, or use the simpler version:
  whole[!row.names(whole) %in% row.names(to.drop),]

The fact that your row names are different to the row indices in whole
will be what is causing the error when trying my variant.

Cheers,
Rich

On Apr 4, 2005 10:21 PM, Wladimir Eremeev <wl at eimb.ru> wrote:
> Dear Rich,
> 
> Thank you for reply. I think, optimization, you offered will satisfy
> my needs.
> I don't completely understand the following.
> 
> RF> ## And wrap the original in a function for comparison:
> RF>   ## This does not subset the way you want:
> RF>   ##  whole[-which(row.names(to.drop) %in% row.names(whole)),]
> RF>   whole[-as.integer(row.names(to.drop)),]
> 
> Why doesn't my subset work properly?
> 
> My data frame 'whole' was created from 3 another data frames by rbind,
> if it makes sense...
> 
> Moreover, your variant gives the error:
> 
> > as.integer(row.names(to.drop)[120:220])
>   [1]   2761   3616   3629   5808   7204   7627   8192  10851  20275 273611   4492 256691   8797
>  [14]  11756  46673 246981 250401 335591    773    774    786    993    995   1454   2715   6990
>  [27]   7951   7962   8185   8662   9406 442100 478100 528100 208710 211710 215910  19846  28660
>  [40]  28661  28691  28806  28878 450611 497411  81672  91572 119232 166191 166281 203981 204201
>  [53] 255171 255212 255301 300651 331212 371761 397651 405241 415331   8779 195510 197910 203210
>  [66] 205410 205510 211810 220610  19615  27165  28581  28640  28641  28642  28662  28714  48692
>  [79] 449611 449911 497211  81702 195451 202491 202551 253931 255071 259102 266971 303341 331831
>  [92] 353912 371931 374612 394461 397641 412671   9227 464100   1558   2161
> > whole[-as.integer(row.names(to.drop)[120:220]),]
> Error in "[.data.frame"(whole, -as.integer(row.names(to.drop)[120:220]),  :
>         subscript out of bounds
> 
> Row names don't coincide with row order numbers in my case.
> 
> --
> Best regards
> Wladimir Eremeev                                     mailto:wl at eimb.ru
> 
> 


-- 
--
Rich FitzJohn
rich.fitzjohn <at> gmail.com   |    http://homepages.paradise.net.nz/richa183
                      You are in a maze of twisty little functions, all alike




More information about the R-help mailing list