M1245689
M1245689

Reputation: 63

Why do I get an error message when I try to obtain a count by 2 variables in R?

I am trying to do a frequency count by 2 variables. This is my data, in dataframe "api":

      Name          Grade
1     John Smith    C     
2     John Smith    B       
3     John Smith    C      
4     Jane Doe      A     
5     Jane Doe      C     
6     Lisa Brown    B  

I want this:

      Name          Grade   Freq
1     John Smith    C       2    
2     John Smith    B       1  
3     John Smith    C       2 
4     Jane Doe      A       1 
5     Jane Doe      C       1   
6     Lisa Brown    B       1   

This is my code:

api_count<-count(api, c("Name", "Grade")

And I get this error message:

Error: Problem with `mutate()` input `..1`.
x Input `..1` can't be recycled to size 28328.
i Input `..1` is `c("Name", "Grade")`.
i Input `..1` must be size 28328 or 1, not 2.

Upvotes: 0

Views: 3575

Answers (4)

akrun
akrun

Reputation: 887183

We can use add_count

library(dplyr)
df %>% 
  add_count(Name, Grade)
#        Name Grade n
#1 John Smith     C 2
#2 John Smith     B 1
#3 John Smith     C 2
#4   Jane Doe     A 1
#5   Jane Doe     C 1
#6 Lisa Brown     B 1

data

df <- structure(list(Name = c("John Smith", "John Smith", "John Smith", 
"Jane Doe", "Jane Doe", "Lisa Brown"), Grade = c("C", "B", "C", 
"A", "C", "B")), class = "data.frame", row.names = c(NA, -6L))

Upvotes: 0

r2evans
r2evans

Reputation: 160447

I think @Duck's is the most direct approach (and preferred; and with this data, half the computation time), but here's an alternative in case it makes more sense: count and then join back with the original data:

df %>%
  count(Name, Grade) %>%
  left_join(df, ., by = c("Name", "Grade"))
#         Name Grade n
# 1 John Smith     C 2
# 2 John Smith     B 1
# 3 John Smith     C 2
# 4   Jane Doe     A 1
# 5   Jane Doe     C 1
# 6 Lisa Brown     B 1

Upvotes: 0

Ubiminor
Ubiminor

Reputation: 124

I think your code is mostly correct, only some minor syntax issues:

api <- data.frame(Name = c(rep("John Smith",3), rep("Jane Doe", 2), "Lisa Brown"), Grade = c("C", "B", "C", "A", "C","B")))
api
   Name Grade
1 John Smith     C
2 John Smith     B
3 John Smith     C
4   Jane Doe     A
5   Jane Doe     C
6 Lisa Brown     B

count(api, c("Name", "Grade"))
        Name Grade freq
1   Jane Doe     A    1
2   Jane Doe     C    1
3 John Smith     B    1
4 John Smith     C    2
5 Lisa Brown     B    1

Upvotes: 1

Duck
Duck

Reputation: 39595

I would suggest this tidyverseapproach:

library(tidyverse)
#Code
df %>% group_by(Name,Grade) %>% mutate(N=n())

Output:

# A tibble: 6 x 3
# Groups:   Name, Grade [5]
  Name       Grade     N
  <chr>      <chr> <int>
1 John Smith C         2
2 John Smith B         1
3 John Smith C         2
4 Jane Doe   A         1
5 Jane Doe   C         1
6 Lisa Brown B         1

Some data used:

#Data
df <- structure(list(Name = c("John Smith", "John Smith", "John Smith", 
"Jane Doe", "Jane Doe", "Lisa Brown"), Grade = c("C", "B", "C", 
"A", "C", "B")), class = "data.frame", row.names = c(NA, -6L))

Upvotes: 1

Related Questions