0

Im trying to find the number of occurrences of each row value throughout the data frame.

count(df,"name of column")

This is what the data looks like

right now I get just the number of rows altogether rather than the occurrence of each value.

Thanks!

  • 1
    Are you using `dplyr`? There is no `count()` function in base R. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Images of data are not helpful because we can't copy/paste the values to test. Are you putting the name in actual quotes? If so, try without the quotes, just use the column name. If the column name has unusual character, use the `\`` for quotes, not `"` – MrFlick Apr 25 '21 at 21:19
  • Remove the quotes around "name of column". – neilfws Apr 25 '21 at 22:11

2 Answers2

0

In data.table, we can use the built in .N counter function to get the number of times each unique value occurs in a given column:

library(data.table)
df <- setDT(df)
# no quotes in the column name
counts <- df[,.(.N), by = name_of_column]

As others have noted, it is easier if you included a reproducible example with sample input and output.

Gabe Solomon
  • 365
  • 3
  • 12
-1

The problem is the quotes around the column name. Demonstration using the iris dataset:

library(dplyr)

With quotes (wrong):

count(iris, "Species")

  "Species"   n
1   Species 150

Without quotes (correct):

count(iris, Species)

     Species  n
1     setosa 50
2 versicolor 50
3  virginica 50
neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Thank you ! I realized its because I need plyr – rachealjohn Apr 26 '21 at 01:56
  • OK: so yes, the `plyr` version of `count` uses quoted variables. But I would recommend using `dplyr` which is an improved, and more recent alternative to `plyr`. And never load `plyr` and `dplyr` at the same time, that will cause issues. – neilfws Apr 26 '21 at 02:31