0

I tried to refer to Counting the number of elements with the values of x in a vector

Initially I used count(). It was working before but now its not.

Why is this giving an error?

countact <- as.data.frame(table(Data$number))
countact$Probability <- countact$freq/sum(countact$freq)

Error in $<-.data.frame(*tmp*, "Probability", value = numeric(0)) : replacement has 0 rows, data has 4

Community
  • 1
  • 1

1 Answers1

1

Looks to me like a simple letter-case problem: you need to use Freq rather than freq:

numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,453,435,324,34,456,56,567,65,34,435);
countact <- as.data.frame(table(numbers));
countact$Probability <- countact$freq/sum(countact$freq);
## Error in `$<-.data.frame`(`*tmp*`, "Probability", value = numeric(0)) :
##   replacement has 0 rows, data has 15
countact$Probability <- countact$Freq/sum(countact$Freq);
countact;
##    numbers Freq Probability
## 1        4    2  0.09090909
## 2        5    1  0.04545455
## 3       23    2  0.09090909
## 4       34    2  0.09090909
## 5       43    1  0.04545455
## 6       54    1  0.04545455
## 7       56    2  0.09090909
## 8       65    1  0.04545455
## 9       67    2  0.09090909
## 10     324    1  0.04545455
## 11     435    3  0.13636364
## 12     453    1  0.04545455
## 13     456    1  0.04545455
## 14     567    1  0.04545455
## 15     657    1  0.04545455
bgoldst
  • 34,190
  • 6
  • 38
  • 64