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

后端 未结 3 1087
梦谈多话
梦谈多话 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:46

    We can do this in data.table

    library(data.table)
    reference <- setDT(df2)[df, .N, on = .(object), by = .EACHI]$N
    df$reference <- reference
    df
    #   object group reference
    #1:  apple     A         3
    #2: banana     B         1
    #3:   pear     A         0
    #4:  robot     C         3
    

提交回复
热议问题