简体   繁体   中英

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.

I would suggest this tidyverse approach:

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))

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

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM