0

Having a specific column like this number_of_columns_with_text:

df <- data.frame(id = c(1,2,3,4,5,6), number_of_columns_with_text = c(3,2,1,3,1,1))

Is there any command which could give the sum of the numbers exists in this column (how many times a number exists).

Example output

data.frame(number = c(1,2,3), volume = c(3,1,2))
Erik Brole
  • 315
  • 9

3 Answers3

2

What you might be looking for is table(...)

> table(df$number_of_columns_with_text)
1 2 3 
3 1 2 
sedsiv
  • 531
  • 1
  • 3
  • 15
1

In dplyr, you can first group_by the variable you want to tabulate and then use n() to count the frequencies of the distinct values:

library(dplyr)
df %>%
  group_by(number_of_columns_with_text)%>%
  summarise(volume = n())
# A tibble: 3 × 2
  number_of_columns_with_text volume
                        <dbl>  <int>
1                           1      3
2                           2      1
3                           3      2
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
0

Using dplyr

library(tidyverse)

df %>% 
group_by(number_of_columns_with_text) %>% 
count()
pluke
  • 3,832
  • 5
  • 45
  • 68