[R] how to do this trimming/selecting in canonical R?

Berend Hasselman bhh at xs4all.nl
Sun Sep 15 07:49:38 CEST 2013


On 15-09-2013, at 03:46, gildororonar at mail-on.us wrote:

> Quoting "Berend Hasselman" <bhh at xs4all.nl>:
> 
>> Your trim-point in B is not unique (at least for the data you provided).
> 
> Indeed. It's quit a surprise to me. I couldn't figure out why my evaluation expression results in multiple trimp-points
> 
>> In a general way, I am looking for x and y, where:
>> A[x, condition] > max(B[1:y, condition] && A[x, force] > B[y+1, counterforce]
> 
> but I'll show you the code I wrote, which is a better explanation of my indention:
> 
> for( x in seq_len(nrow(A)) ) {
>    y = sum(B[,"condition"] < A[x, "condition"])
>    if (A[x, "force"] > B[y+1, "counterforce"]) break
> }
> cat("x=",x,"y=",y,"res=",res,"\n")
> 
> Result is:
> 
> # x= 5 y= 9 res= TRUE
> 

This I really don't understand.
And it can go wrong. With the same data doing this

for( x in seq_len(nrow(A)) ) {
        y <- sum(A[x,"condition"] > B[,"condition"])
        res <- A[x,"force"] > B[y+1,"counterforce"]
        cat("x=",x,"y=",y,"res=",res,"\n")
}

will give you

# x= 1 y= 0 res= FALSE 
# x= 2 y= 4 res= FALSE 
# x= 3 y= 5 res= FALSE 
# x= 4 y= 6 res= FALSE 
# x= 5 y= 9 res= TRUE 
# x= 6 y= 9 res= TRUE 
# x= 7 y= 9 res= TRUE 
# x= 8 y= 9 res= TRUE 
# x= 9 y= 10 res= TRUE 
# x= 10 y= 15 res= NA 

When y==15 then y+1 references a non-existing row in B and when res==NA the if will throw an error message.

Berend


> --
> To use the same test data as I have:
> 
>> A <- read.table(text = "force condition
> 0.03515542         1
> 0.13267882        13
> 0.26155689        24
> 0.37453142        38
> 0.39360520        45
> 0.43924737        48
> 0.47669800        50
> 0.57044795        51
> 0.81177499        61
> 0.98860450        94", header=T)
> 
>> B <- read.table(text = "counterforce condition
> 0.965769548         2
> 0.965266255         5
> 0.846941244         7
> 0.818013029        11
> 0.813139978        22
> 0.730599939        34
> 0.715985436        39
> 0.658073895        40
> 0.421264948        42
> 0.373774505        52
> 0.242191461        62
> 0.090584590        63
> 0.070020635        68
> 0.067366062        83
> 0.001585313        84", header=T)
> 
> 
> -------------------------------------------------
> 
> VFEmail.net - http://www.vfemail.net
> $14.95 ONETIME Lifetime accounts with Privacy Features!
> 15GB disk! No bandwidth quotas!
> Commercial and Bulk Mail Options!



More information about the R-help mailing list