count occurences in a specific way in R

r count occurrences in data frame
r count number of occurrences in column
count the number of times a value appears in a column r
count number of observations in r
r count frequency
countif in r
count factors in r
conditional count in r

I have a vector of double numbers between 1 and 10.

a<-c(3.5, 1.2, 1, 5, 8, 6.9, 5.3, 1.2)

I have to have a dataframe of 10 classes(1 to 10), with the count of each classe, if the nimber does not occure, it must be presented with 1.

so the expected result is this one:

> df
    a b
1   1 3
2   2 1
3   3 1
4   4 1
5   5 2
6   6 1
7   7 1
8   8 1
9   9 1
10 10 1

One way using base R, convert a to integer and then factor with levels 1-10, count their occurrences using table and use pmax to replace 0 counts with 1.

stack(pmax(table(factor(as.integer(a), levels = 1:10)), 1))[2:1]

#   ind values
#1    1      3
#2    2      1
#3    3      1
#4    4      1
#5    5      2
#6    6      1
#7    7      1
#8    8      1
#9    9      1
#10  10      1

rowCounts: Counts the number of occurrences of a specific value in , Description Usage Arguments Value Author(s) See Also Examples. View source: R/rowCounts.R. Description. The row- and column-wise functions take either a� My preferred solution uses rle, which will return a value (the label, x in your example) and a length, which represents how many times that value appeared in sequence.. By combining rle with sort, you have an extremely fast way to count the number of times any value appeared.

Here is another base R solution

df <- data.frame(a=1:10,b=1)
df$b[as.numeric(names(z))] <- (z <- table(floor(a)))

such that

> df
    a b
1   1 3
2   2 1
3   3 1
4   4 1
5   5 2
6   6 1
7   7 1
8   8 1
9   9 1
10 10 1

count: Count the number of occurences. in plyr: Tools for Splitting , count: Count the number of occurences. View source: R/count.r Compared to table + as.data.frame , count also preserves the type of the identifier variables,� Counting and aggregating in R 24 September 2012 r A short post about counting and aggregating in R, because I learned a couple of things while improving the work I did earlier in the year about analyzing reference desk statistics.

The base solution is probably better (I like it better), but here is a dplyr way.

library(dplyr)
library(tidyr)

tibble(a = a) %>%
  count(a = factor(as.integer(a), levels = 1:10)) %>%
  complete(a, fill = list(n = 1))
# A tibble: 10 x 2
   a         n
   <fct> <dbl>
 1 1         3
 2 2         1
 3 3         1
 4 4         1
 5 5         2
 6 6         1
 7 7         1
 8 8         1
 9 9         1
10 10        1

count function, Count the number of occurences. Equivalent to as.data.frame(table(x)) , but does not include combinations with zero counts. Keywords: manip� 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 […]

Count Occurrences of a Char in a String, StringUtils class provides us with the countMatches() method, which can be used for counting chars or even sub-strings in given String. First, we� tally() is a convenient wrapper for summarise that will either call n() or sum(n) depending on whether you're tallying for the first time, or re-tallying. count() is similar but calls group_by() before and ungroup() after. If the data is already grouped, count() adds an additional group that is removed afterwards. add_tally() adds a column n to a table based on the number of items within each

Count occurrences of pattern in strings - MATLAB count, A = count( str , pattern ) returns the number of occurrences of pattern in str . Input text, specified as a string array, a character vector, or a cell array of character� Count filtered cells containing specific text; How to count cells with specific text in Excel. Microsoft Excel has a special function to conditionally count cells, the COUNTIF function. All you have to do is to supply the target text string in the criteria argument. Here's a generic Excel formula to count number of cells containing specific text:

Count/tally observations by group — tally • dplyr, Source: R/count-tally.R If the data is already grouped, count() adds an additional group that is removed afterwards. If specified, will perform a " weighted" tally by summing the (non-missing) values of A tbl, grouped the same way as x . Count Number of Occurrences of words from a list in text entries (R) Hot Network Questions In Germany, are there any cities or special regions in cities which are dangerous for non-white people to go to?

Comments
  • Thanks Ronak, thats realy awsome