[R] squashing some numbers

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Mon Mar 22 09:58:10 CET 2004


are you looking for s.th. like this?

# some data
m<-c(3,1,2,4,4,4,4,0,5,4,4,5,6,1,1,1)
m<-as.data.frame(matrix(m,4,4))
print(m)
# sort m -- if necessary
m<-m[order(m[,3]),]
m<-m[order(m[,2]),]
# compute sums
cum<-cumsum(m[,4])
# find rows
ind<-c(T,0!=diff(m[,2] )| 0!=diff(m[,3]))
ind<-c(ind[-1],T)
# combine result
mneu<-cbind(m[ind,-4],V4=diff(c(0,cum[ind])))
print(mneu)

initial matrix:
  V1 V2 V3 V4
1  3  4  5  6
2  1  4  4  1
3  2  4  4  1
4  4  0  5  1

result:
  V1 V2 V3 V4
4  4  0  5  1
3  2  4  4  2
1  3  4  5  6

result without sorting

  V1 V2 V3 V4
1  3  4  5  6
3  2  4  4  2
4  4  0  5  1


Peter Wolf

Fred J. wrote:

>Hello
>I have a data frame with many col.s and rows
>V4 V5 V6 V7
>3  4   5  6
>1  4   4  1
>2  4   4  1
>4  0   5  1
>
>since the data has the middle 2 rows with V5 and V6
>are equal, I need to produce
>V4 V5 V6 V7
>3  4   5  6
>            this line removed and the value V7 = 1 is 
>     
>            added to the next row V7 valuve making it
>2
>2  4   4  2
>4  0   5  1
>
>thanks a lot
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>




More information about the R-help mailing list