## R - count occurrences in long vectors

r count number of occurrences in column
r count frequency
r count observations by group
count the number of times a value appears in a column r
conditional count in r
count observations in r
count number of observations in r
count function in r package

I have a dataframe that is 6249 rows long, filled with character-type data and will likely get a lot bigger.

I want to count the number of occurrences of each string. Normally I'd use `table(df)` or

```count(df)
```

but they both seem to stop after 250 rows.

Is there a different function or a way to force count() or table() to continue for 6000+ results?

A simple way to do this with any sized data frame is to add a `count` field to the data frame and then summarize the `string` field by `count` with the `doBy` package - like so:

```require(doBy)
df\$count <- 1
result <- summaryBy(count ~ string, data = df, FUN = sum, keep.names = TRUE)
```

R Is Not So Hard! A Tutorial, Part 15: Counting Elements in a Data Set, Let's count the 3s in the vector b. count3 <- length(which(b == 3)) count3   rowCounts: Counts the number of occurrences of a specific value In matrixStats: Functions that Apply to Rows and Columns of Matrices (and to Vectors) Description. The row- and column-wise functions take either a matrix or a vector as input. If a vector, then argument dim. must be specified and fulfill prod(dim.) == length(x).

As @Gregor noticed it seems like you interpreted the `table` output wrongly whereas it is actually doing the right counting. Anyway here goes a solution using `Reduce`, you should replace `df` where indicated by your dataframe and `string` column name by the column name of your actual dataframe in which you are counting.

```# let's create some dataframe with three strings randomly distributed of length 1000
df <- data.frame(string = unlist(lapply(round(runif(1000, 1, 3)), function(i) c('hi', 'ok', 'my cat')[i])))
my.count <- function(word, df) {
# now let's count how many 'b' we found
Reduce(function(acc, r) {
# replace 'string' by the name of the column of your dataframe over which you want to count
if(r\$string == word)
acc + 1
else
acc
}, apply(df, 1, as.list), init = 0)
}

# count how many 'my cat' strings are in the df dataframe at column 'string', replace with yours
my.count('my cat', df)
# now let's try to find the frequency of all of them
uniq <- unique(df\$string)
freq <- unlist(lapply(uniq, my.count, df))
names(freq) <- uniq
freq
# output
# ok my cat     hi
# 490    261    249
# we can check indeed that the sum is 1000
sum(freq)
#  1000
```

The advantages of using count() to get N-way , How do you find the length of a vector in R? e. then count the number of activities after the last work activity. The result for the three example tours then would be: 1.number of activities before first primary: 1 (home) number of activities between first and last primary: 0 number of activities after last primary: 2 (leisure & home) number of primary activities: 1 (work)

Well, this won't be popular, but in the end I achieved the desired result with a for loop and and taking the number of rows in a subset.

```y <- as.numeric(vector())
x <- as.numeric(vector())
for (i in test\$token){
x <- as.numeric(nrow(df[(df\$token == i),]))

y <- c(y, x)

}
```

Y then becomes a vector with the number of occurences of each string.

colCounts: Counts the number of occurrences of a specific value in , in the “plyr” package to get the same frequencies in a list format. > count (bevs, c ("name", "drink")) name drink freq 1 Bill cocoa 2 2 Bill coffee 2 3 Llib tea 2 4 Llib water 2 It’s all pretty easy. Just tell count which data frame you’re using, then which columns you want to tally by, and it does the counting very quickly and efficiently.

count: Count the number of occurences. in plyr: Tools for Splitting , The row- and column-wise functions take either a matrix or a vector as input. If a vector, then argument dim. must be specified and fulfill  variables to count unique values of wt_var optional variable to weight by - if this is non-NULL, count will sum up the value of this variable for each combination of id variables.

count function, View source: R/count.r Compared to table + as.data.frame , count also preserves the type of the identifier variables, instead of converting them to  To figure out what data can be factored when working in R, let’s take a look at the dataset mtcars. This built-in dataset describes fuel consumption and ten different design points from 32 cars from the 1970s. It contains, in total, 11 variables, but all of them are numeric. Although you can work with the …

Counting and aggregating in R, Count the number of occurences. Equivalent to as.data.frame(table(x)) , but does not include combinations with zero counts. Keywords: manip  Previous message: Jean Eid: "[R] System memory" In reply to Spencer Graves: "Re: [R] counting the occurrences of vectors" Next in thread: Gabor Grothendieck: "Re: [R] counting the occurrences of vectors" Reply: Gabor Grothendieck: "Re: [R] counting the occurrences of vectors" Reply: Gabor Grothendieck: "Re: [R] counting the occurrences of vectors"

• are you just trying to count the rows in the data frame? if so use `nrow(df)`
• `table` doesn't stop counting - the default print behavior just truncates it off. Try `tt = table(runif(6000))`, `length(tt)`, `head(tt)`, `tail(tt)`...
• Do you want a count column in `df`? If so, try `aggregate(. ~ string, df, function(x) length(unique(x)))`. Otherwise, as @Gregor says, `table` should work.