-1

I can't find the

Error: matrix/data.frame criterion should be logical.

in my code.

#count prod_no downward
consoContact2$Count <- as.numeric("")
for (xNo in 1:nrow(consoContact2)){
  consoContact2$Count[xNo] <- count_if(consoContact2[xNo,1],consoContact2[1:xNo,1])
}

Data sample:

    Prod_No         Order_No    Reff_ID                       Count
1   03MB1902-118    920522XXXX  03MB1902-118'A**0920522XXXX NA
2   03MB1902-118    33333XXXX   03MB1902-118'C**033333XXXX  NA
3   07MB1902-105    922977XXXX  07MB1902-105'A**0922977XXXX NA
4   07MB1902-105    32231XXXX   07MB1902-105'C**032231XXXX  NA
5   07MB1902-105    32268XXXX   07MB1902-105'C**032268XXXX  NA

These must be the output:

    Prod_No         Order_No    Reff_ID                       Count
1   03MB1902-118    920522XXXX  03MB1902-118'A**0920522XXXX 1
2   03MB1902-118    33333XXXX   03MB1902-118'C**033333XXXX  2
3   07MB1902-105    922977XXXX  07MB1902-105'A**0922977XXXX 1
4   07MB1902-105    32231XXXX   07MB1902-105'C**032231XXXX  2
5   07MB1902-105    32268XXXX   07MB1902-105'C**032268XXXX  3
markus
  • 25,843
  • 5
  • 39
  • 58
Gorbion
  • 13
  • 4

2 Answers2

0

This question is simpler solved using the several solutions, some of which are stated here. As such this is a duplicate question. However, these do not use the data.table package, as such i'll provide an example here using this package. For more information check their wiki page here. Additionally it seems the very last column of your data is logical, which should be changed to numeric

library(data.table)
#Change from data.frame to data.table
setDT(data_sample)
#set count to be numeric
data_sample[, Count := as.integer(Count)]
#Change count to be the format needed.
data_sample[, Count := seq(1,.N), by = Prod_No]
Oliver
  • 8,169
  • 3
  • 15
  • 37
0

This minor change should help you get the required output

consoContact2$Count <- as.numeric("")
for (xNo in 1:nrow(consoContact2)){
  consoContact2$Count[xNo] <- length(which(consoContact2$Prod_No[1:xNo] == consoContact2[xNo,1]))
}

Hope that helps.

Rage
  • 323
  • 1
  • 13