0

I have a tabulated data with counts in each row. I want to categorize and see the sums of each category in a table. I have used the script below:

as.matrix.data.frame(xtabs(Count ~ Factor, 
                           data=aggregate(Count ~ Factor, data=data, FUN=sum)))

I have the error below:

Error in seq_len(p) : argument must be coercible to non-negative integer

I created a random database below whichc gives the same error:

data <- cbind(c("Factor" = factor(sample.int(n = 41, size = 18000, replace = TRUE))),
c("Count" = as.numeric(sample.int(n = 10, size = 18000, replace = TRUE))))

as.matrix.data.frame(xtabs(V2 ~ V1, 
                           data=aggregate(V2 ~ V1, data=data, FUN=sum)))
Error in seq_len(p) : argument must be coercible to non-negative integer

In the data I see dicordance like below:

> summary(data$V2)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.00    5.00    5.49    8.00   10.00 
> summary(desc(data$V2))
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 -10.00   -8.00   -5.00   -5.49   -3.00   -1.00 

And the multiplications below does not help:

data$V1 <- (-1)*data$V1
data$V2 <- (-1)*data$V2

I want to have sum of counts sy category but I have the error.

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

0

The issue is that the "Count" column contains negative values, which cannot be used as the argument for the xtabs function. The "xtabs" function is used to create contingency tables, and the counts must be non-negative integers. To resolve this issue, you need to remove the negative values from the "Count" column before aggregating the data. You can use the following code to remove the negative values:

data[data$Count < 0, "Count"] <- 0
agg_data <- aggregate(Count ~ Factor, data=data, FUN=sum)
xtab_data <- xtabs(Count ~ Factor, data=agg_data)
as.matrix.data.frame(xtab_data)
Khanh Tran
  • 87
  • 1
  • 3
  • 9
  • Thank you for your quick response. However, I imported my original data from Excel and the original data includes non-missing positive integers. The error happened again when I wanted to create artificial data to ask my question (above). As my data is concrete I wanted to handle the problem by multiplication with (-1) which didn't work again. – Erdem Erkoyun Feb 02 '23 at 07:51
  • After I use all your recommeended code I still have the same error: – Error in seq_len(p) : argument must be coercible to non-negative integer – Erdem Erkoyun Feb 02 '23 at 07:57