How do I use the dim function to evaluate another dataframe using mutate in r?

43 views Asked by At

I am trying to add a column to a dataframe that has the dimension of a portion of another dataframe using the following code.

library(dplyr)

dfa <- data.frame(x=c(1,2,3,4,5), sym=c("a","a","b","b","b"))
dfa <- dfa %>% group_by(sym)

dfb <- data.frame( sym = c("a", "b") )
dfb %>% mutate( len = dim( dfa[ dfa[["sym"]]==sym , ] )[1] )

This code give the unintended output and warning message:

  sym len
1   a   2
2   b   2
Warning message:
Problem while computing `len = dim(dfa[dfa[["sym"]] == sym, ])[1]`.
ℹ longer object length is not a multiple of shorter object length 

The output I want is (I must use mutate)

  sym len
1   a   2
2   b   3

Any suggestions?

1

There are 1 answers

0
gaut On

You can use

dfb %>% group_by(sym) %>% mutate(len=sum(sym == dfa$sym))
  sym     len
  <chr> <int>
1 a         2
2 b         3

The key is to group dfb by sym as well.