“longer object length is not a multiple of shorter object length”

后端 未结 3 1092
梦谈多话
梦谈多话 2021-01-21 16:05

I have this dataset -

print(df)

  object    group   
1 apple      A    
1 banana     B    
1 pear       A    
1 robot      C

print(df2)

  object    group   
         


        
3条回答
  •  悲&欢浪女
    2021-01-21 16:53

    From my comment: dplyr functions work on the whole column taken as a vector. Try

    df %>%
    rowwise() %>% 
    mutate(reference = length(df2[df2$object == object,]$object))%>%
    ungroup()
    

    As you said, ungroup will be needed, unless you plan on doing further row-wise operations.

提交回复
热议问题