Skip to content

little manipulation on data frame

3 messages · N Dey, Spencer Graves, Thomas Lumley

#
Dear all,

I have data like 3 coulmns and many rows. Each entry
is less than 10.

Example
	x	y	z
1	5	3	2	
2	3	7	8
3	8	9	5
4	5	4	6
--------------------------
---------------------------

I have to sum entries of each coulmn (seperately) till
it be 10. This i have to start for each row. And I
want to assign no. of rows needed including that row
too(it to be 10 or 10+, the moment it exceeds 10, i
need to stop and count the no. of rows)in additional
coulmns say N1 (corresponding to coulmn x), N2 (y) and
N3 (z).


I want my new table like

	x	y	z	N1	N2	N3 
1	5	3	2	3	2	2
2	3	7	8	2	2	2	
3	8	9	5	2	2	2
4	5	4	6	depends upon next row


If anybody knows it, please help me.

Thanking you.

Best regards,
N. Dey.
#
I don't completely understand what you want, but might the following help?

 > cumsum(1:11)
  [1]  1  3  6 10 15 21 28 36 45 55 66
 > which(cumsum(1:11)>9)
[1]  4  5  6  7  8  9 10 11
 > which(cumsum(1:11)>9)[1]
[1] 4

hth.  spencer graves
N Dey wrote:
#
On Fri, 6 Jun 2003, N Dey wrote:

            
It depends a bit on how many is `many'.

You can get cumulative sums with cumsum, and the first entry in each
column is then

    min(which(cumsum(x) > 10))

The i+1th entry is
    min(which( cumsum(x)-cumsum(x)[-(1:i)] > 10))

If the number of rows is not very large I would do

   sumx<-cumsum(x)
   N1<-min(which(cumsum(x) > 10))
   N1<-c(N1, sapply(1:(length(x)-1), function(i)
		min(which(sumx[-(1:i)]-sumx[i]>10))))

or the equivalent for() loop.

If the number of rows is very large it would be more efficient to rely on
the fact that no more than 10 rows are needed (assuming that zeros aren't
possible)

   n<-length(x)
   sumx<-cumsum(x)

   sumlags<-matrix(nrow=n,ncol=10)
   for(i in 0:9)
	sumlags[,i+1]<-sumx[ c((i+1):n, rep(n,i))]

   N1<-rowSums(sumlags < c(0,sumx[1:(n-1)])+10)+1


	-thomas