[R] Readjusting frequencies

arun smartpink111 at yahoo.com
Wed Nov 13 17:00:37 CET 2013


Hi Katherine,

Check if this works:
fraud_data = data.frame(no_of_frauds = c(1, 2, 4, 6, 7, 9, 10), frequency = c(3, 1, 7, 11, 13, 1, 4))
fraud_data1 <- rbind(fraud_data,c(11,10))

fun1 <- function(dat) {
 sum1 <- 0
 for(i in 1:nrow(dat)){
 sum1 <- sum1 + dat[i, "frequency"]
 dat[i,"frequency"] <- sum1
 if(sum1> 5){
 sum1 <- 0
 }
 }
indx <- which(dat$frequency >5)
if(max(indx)==nrow(dat))
{res <- dat[dat$frequency >5,]
}
else{
dat[max(indx),"frequency"] <- sum(dat[c(max(indx),nrow(dat)),"frequency"])
res <- dat[dat$frequency > 5,]
}
res
}
fun1(fraud_data)
#  no_of_frauds frequency
#3            4        11
#4            6        11
#5            7        18
 fun1(fraud_data1)
#  no_of_frauds frequency
#3            4        11
#4            6        11
#5            7        13
#8           11        15


A.K.

On Tuesday, November 12, 2013 5:12 AM, Katherine Gobin <katherine_gobin at yahoo.com> wrote:

Dear Mr Arun,

Hi!
 Sorry to bother you. Yesterday I had raised one query to the forum but 
haven't received any response. If the time permits, will it be possible 
for you to at least have a look at it?


I have
following data.frame as

fraud_data
= data.frame(no_of_frauds = c(1, 2, 4, 6, 7, 9, 10), frequency = c(3, 1, 7, 11,
13, 1, 4))

>
fraud_data

no_of_frauds frequency
1
           1         3
2
           2         1
3
           4         7
4
           6        11
5
           7        13
6
           9         1
7
          10         4

I need
to regroup the data in such a way that if the frequency is less than 5, the
corresponding class data gets merged to next class (or at times with previous class too) i.e. the frequencies get
added till the added frequencies exceed 5. 

Thus, in above data.frame
since frequencies pertaining to no_of_frauds 1 and 2 are 3 and 1 respectively,
these get added to class 4 and the frequency of this class now becomes 3+1+7 =
11. Likewise, frequency of classes 9 and 10 are 1 and 4 and when these are
added still it is 5 i.e. doesn't exceed 5. Thus, these should get added to the
previous class i.e. 7.

Thus I
need to have

no_of_frauds
      frequency

      4                
  11            #  ( 3 + 1 + 7)

      6                
  11           

      7                
  18            #  (13 + 1 + 4)


If possible, pl go through it. Sorry to bother you.

Regards

Katherine




More information about the R-help mailing list