Skip to content
Prev 361697 / 398506 Next

summing up a column.

Jeff;
Thanks a lot. I do appreciate for this. I will try your codes.
Regards,
Oslo
On Monday, June 13, 2016 11:47 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
You did half of what Petr asked... but your email still looks unreadable
because you did not send it as text only. Look at [1] to see what we see, 
and why we want you to send plain text. What you see is not what we see.

This is an outer join... an inherently inefficient operation according to 
relational database theory. Most solutions to this type of problem are 
likely to be slow, but minimizing unnecessary use of memory can help and 
to do that you can overwrite existing values instead of successively 
concatenating longer vectors of results as you go.

To understand the solution below, you should execute individual 
expressions interactively as you step through the code to see what the 
intermediate values look like. In particular, expressions on the right 
side of assignments can be interactively entered at the console without 
changing the variables in the environment so do it as much as you need to 
in order to see what is happening.

# alignment not required, but HTML run-together lines not wanted
A <- structure( list( posA = c( 1L, 2L, 5L, 4L, 9L)
? ? ? ? ? ? ? ? ? ? , posB = c( 9L, 7L, 12L, 7L, 13L)
? ? ? ? ? ? ? ? ? ? )
? ? ? ? ? ? ? , .Names = c("posA", "posB")
? ? ? ? ? ? ? , class = "data.frame"
? ? ? ? ? ? ? , row.names = c(NA, -5L)
? ? ? ? ? ? ? )
B <- structure( list( pos = c( 4L, 2L, 7L, 1L, 13L, 12L, 9L)
? ? ? ? ? ? ? ? ? ? , a = c(0.4, 0.1, 0.5, 0.4, 0.1, 0.2, 0.3)
? ? ? ? ? ? ? ? ? ? , b = c(7L, 5L, 8L, 1L, 6L, 11L, 12L)
? ? ? ? ? ? ? ? ? ? , c = c(0.8, 0.4, 0.32, 0.1, 0.13, 0.01, 0.23))
? ? ? ? ? ? ? , .Names = c("pos", "a", "b", "c")
? ? ? ? ? ? ? , class = "data.frame"
? ? ? ? ? ? ? , row.names = c(NA, -7L)
? ? ? ? ? ? ? )
# sort B
sB <- B[ order( B$pos ), ]
# performance: set aside memory to remember results
A$count07 <- NA
A$mina <- NA
for ( i in seq.int( nrow( A ) ) ) {
? # logical indexing vector
? idx <- A[ i, "posA" ] <= sB$pos & sB$pos <= A[ i, "posB" ]
? # only extract desired vector once
? a <- sB[ idx, "a" ]
? # sum adds logical values as if TRUE=1
? A[ i, "count07" ] <- sum( cumsum( a ) < 0.7 )
? A[ i, "mina" ] <-min( a )
}
print( A )

#=== sample interactive session for study after A and B are defined
#=== execute lines one at a time and study them!
order( B$pos )
B[ order( B$pos ), ]
sB <- B[ order( B$pos ), ]
A$count07 <- NA
A$mina <- NA
i <- 1
A[ i, "posA" ] 
A[ i, "posB" ]
sB$pos
A[ i, "posA" ] <= sB$pos
sB$pos <= A[ i, "posB" ] 
A[ i, "posA" ] <= sB$pos & sB$pos <= A[ i, "posB" ]
idx <- A[ i, "posA" ] <= sB$pos & sB$pos <= A[ i, "posB" ]
sB[ idx, "a" ]
a <- sB[ idx, "a" ]
cumsum( a )
cumsum( a ) < 0.7
sum( cumsum( a ) < 0.7 )
A[ i, "count07" ] <- sum( cumsum( a ) < 0.7 )
min( a )

------
[1] https://stat.ethz.ch/pipermail/r-help/2016-June/439404.html
On Mon, 13 Jun 2016, oslo via R-help wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller? ? ? ? ? ? ? ? ? ? ? ? The? ? .....? ? ? .....? Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>? ? ? ? Basics: ##.#.? ? ? ##.#.? Live Go...
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Live:? OO#.. Dead: OO#..? Playing
Research Engineer (Solar/Batteries? ? ? ? ? ? O.O#.? ? ? #.O#.? with
/Software/Embedded Controllers)? ? ? ? ? ? ? .OO#.? ? ? .OO#.? rocks...1k
---------------------------------------------------------------------------