Search code examples
rloopsfor-loopunique

For loop that only counts unique values


My data frame consists of these columns: A_NUMBER, B_NUMBER, DURATION. I would like to count how many times A_NUMBER calls to a different B_NUMBER (to see how big their network is).

I first created a new column with all values set equal to 0.

df$CFU <- rep (0,nrow(df))

Next, I tried the following for loop:

for (j in 1:nrow(df)){ for (i in 1:nrow(unique(df$B_NUMBER))){ 
   if(df$A_NUMBER[i] == df$A_NUMBER[j]) {df$CFU[j] <- sum(df$CFU[j],1)  }}}

Then I get the following error:

'error in 1:nrow(unique(df$B_NUMBER)): argument of length 0.

How should I solve this?


Solution

  • The way I understood your question is that you are looking for is a list of unique B_NUMBERs for each A_NUMBER.

    A_NUMBER = round(runif(100,0,10))
    B_NUMBER = round(runif(100,0,10))
    df = cbind(A_NUMBER, B_NUMBER)
    aggregate(B_NUMBER ~ A_NUMBER, data=df, unique)
    
       A_NUMBER                   B_NUMBER
    1         0                      10, 8
    2         1           9, 3, 1, 7, 8, 0
    3         2       7, 0, 6, 1, 9, 2, 10
    4         3           7, 3, 6, 8, 4, 5
    5         4 7, 9, 3, 10, 4, 8, 1, 2, 5
    6         5                 6, 5, 2, 8
    7         6          4, 8, 9, 6, 10, 3
    8         7     7, 3, 6, 0, 4, 1, 9, 8
    9         8              7, 9, 8, 5, 2
    10        9        8, 6, 2, 9, 0, 4, 1
    11       10                          7
    

    and then you can call the length of the vectors as

    aggregate(B_NUMBER ~ A_NUMBER, data=df, function(x) length(unique(x))
    
       A_NUMBER B_NUMBER
    1         0        2
    2         1        6
    3         2        7
    4         3        6
    5         4        9
    6         5        4
    7         6        6
    8         7        8  
    9         8        5
    10        9        7
    11       10        1
    

    and check whether it was correct by

    subset(df,A_NUMBER == 8)
    
           A_NUMBER B_NUMBER
     [1,]        8        7
     [2,]        8        9
     [3,]        8        7
     [4,]        8        8
     [5,]        8        5
     [6,]        8        7
     [7,]        8        2
     [8,]        8        2
     [9,]        8        8
    

    Looks good, only 7s, 9s, 8s, 5s and 2s!