Reputation: 21
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.
Upvotes: 0
Views: 532
Reputation: 87
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)
Upvotes: 0