RQuestions - 10 months ago 51

R Question

I have a vector of numbers:

`numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,`

453,435,324,34,456,56,567,65,34,435)

How can I have R count the number of times a value

Answer

You can just use `table()`

:

```
> a <- table(numbers)
> a
numbers
4 5 23 34 43 54 56 65 67 324 435 453 456 567 657
2 1 2 2 1 1 2 1 2 1 3 1 1 1 1
```

Then you can subset it:

```
> a[names(a)==435]
435
3
```

Or convert it into a data.frame if you're more comfortable working with that:

```
> as.data.frame(table(numbers))
numbers Freq
1 4 2
2 5 1
3 23 2
4 34 2
...
```

Source (Stackoverflow)