0

I have a data frame as follows: x1 ,x2, x3 are column's name

x1 x2 x3
A  c  d
A  e  f
B  z  f
B  z  f
B  d  e

I want to count the number of column x2 and x3 according to x1 Such as following table:

   x1 x2 x3
    A  2  2
    B  2  2

I have tried as follows: library(plyr) df3<-count(df, "x1","x2",x3) But it give me an error Error in Summary.factor And Also I have tried this Approach

library(dplyr)
    df %>% select(x1,x2,x3)
     group_by(df$x1) %>%
      mutate(unique_types = n_distinct(df$x2,df$x3))

But it does not work.

user
  • 592
  • 6
  • 26
  • 1
    `df %>% group_by(x1) %>% summarise_at(vars(x2:x3), funs(n_distinct)) ` – Ronak Shah Dec 04 '18 at 05:39
  • Thanks it works very well. so we should use summarise_at function including the n_distinct to omit the duplicated values. – user Dec 04 '18 at 05:42
  • 1
    I am not sure how many other columns you have in your dataframe. `summarise_at` is used to specify for which columns we want to get distinct values. If you have only 3 columns `df %>% group_by(x1) %>% summarise_all(n_distinct)` would also work. `n_distinct` basically returns number of unique values and here as we are grouping it by `x1` it returns number of unique values for each `x1` for `x2` and `x3` columns. – Ronak Shah Dec 04 '18 at 05:46
  • Thanks for clarification very helpful. – user Dec 04 '18 at 19:48

0 Answers0