【问题标题】:error "no applicable method for 'regroup' applied to an object of class "c('integer', 'numeric')""错误“没有适用于 'regroup' 的方法应用于类“c('integer','numeric')”的对象“”
【发布时间】:2014-09-15 14:31:21
【问题描述】:

您好,我是 r 的新手,我有一个问题,即从名为 w2 的数据帧中找到用户网络(uID)和文章网络(faID)

faID      uID
 1        1256
 1        54789
 1        547821
 2        3258
 2        4521
 2        4528
 3        98745
 3        1256
 3        3258
 3        2145

这只是一个例子,我有超过 2000 篇文章,我想根据数据框格式的文章在用户之间建立关系,例如 ##第一篇##

1258  54789
1258  547821
54789 547821

##第 2 条类似##

3258  4521
3258  4528
4528  4521

其他一些信息是

输入(头部(w2,)) 结构(列表(faID=c(1L,1L,1L,1L,1L,1L),uID=c(20909L,6661L,1591L,28065L,42783L,3113L)), .Names=c("faID","uID "),row.names=c(7L,9L,10L,12L,14L,16L),class=data.frame")

dim(w2) 
[1] 364323 2

我正在使用一位志愿者建议的代码

错误出现在 >"Error in UseMethod("regroup") :

没有适用于“c('integer', 'numeric')”类对象的“regroup”方法)##

library(dplyr)
edges<-tbl_df(w2) %>% 
group_by(w2$faID) %>% 
do({    
tmp <-combn(sort(.$user),m =2)
data.frame(a=tmp[1,],b=tmp[2,],stringsAsFactors=FALSE )
 })%>%
 ungroup 
}

我们将不胜感激任何建议。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    从阅读Assigning names to the list output of dplyr do operation 来看,我猜这在dplyr 中还没有实现

    你可以这样做:

    library(gsubfn)
    library(dplyr)
    w2%>% 
    group_by(faID) %>%
    fn$do2(~combn(.$uID, m=2)) #`do2` from the link
    
    #    $`1`
    #      [,1]   [,2]   [,3]
    #[1,]  1256   1256  54789
    #[2,] 54789 547821 547821
    
    #   $`2`
    #      [,1] [,2] [,3]
    # [1,] 3258 3258 4521
    #[2,] 4521 4528 4528
    
    #  $`3`
    #     [,1]  [,2]  [,3] [,4] [,5] [,6]
    # [1,] 98745 98745 98745 1256 1256 3258
    # [2,]  1256  3258  2145 3258 2145 2145
    

    数据

    w2 <- structure(list(faID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L
    ), uID = c(1256L, 54789L, 547821L, 3258L, 4521L, 4528L, 98745L, 
    1256L, 3258L, 2145L)), .Names = c("faID", "uID"), class = "data.frame", row.names = c(NA, 
    -10L))
    

    更新

    可以这样做:

    res <- w2 %>% 
    group_by(faID) %>% 
    do({data.frame(
         combN=paste(apply(combn(sort(.$uID), m=2),2,paste,collapse=" "),
        collapse=", "), stringsAsFactors=F)})
    
    res
    #   faID                                                               combN
    # 1    1                               1256 54789, 1256 547821, 54789 547821
    # 2    2                                     3258 4521, 3258 4528, 4521 4528
    # 3    3 1256 2145, 1256 3258, 1256 98745, 2145 3258, 2145 98745, 3258 98745
    
    library(data.table)
    

    https://gist.github.com/mrdwab/11380733使用cSplit

    cSplit(cSplit(res, "combN", ", ", "long"),"combN", " ")
    #     faID combN_1 combN_2
    #  1:    1    1256   54789
    #  2:    1    1256  547821
    #  3:    1   54789  547821
    #  4:    2    3258    4521
    #  5:    2    3258    4528
    #  6:    2    4521    4528
    #  7:    3    1256    2145
    #  8:    3    1256    3258
    #  9:    3    1256   98745
    # 10:    3    2145    3258
    # 11:    3    2145   98745
    # 12:    3    3258   98745
    

    【讨论】:

    • 我已经将代码复制粘贴到链接中,然后应用上面的代码,它给了我这个错误“eval(expr,envir,enclos) 中的错误:找不到对象 fn
    • @user3841811。如果我不加载library(gsubfn),我会得到你提到的错误。
    • @user3841811。使用相同的数据集,我没有任何错误。如果您可以在显示错误的较小数据集(10-20 行)上使用dput 显示数据,我会尝试。
    • 其实上面的数据只是整个数据集的一个子集,有些信息是dput(head(w2,))结构(list(faID = c(1L, 1L, 1L, 1L, 1L, 1L), uID = c(20909L, 6661L, 1591L, 28065L, 42783L, 3113L)), .Names = c("faID", "uID"), row.names = c(7L, 9L, 10L, 12L , 14L, 16L), class= "data.frame")
    • @user3841811。使用我的第二种方法。我得到了结果。 RES $ COMBN [1]“1591 3113,1591 6661,1591 20909,1591 28065,1591 42783,3113 6661,3113 6661,3113 20909,3113 28065,3113 42783,6661,6661,661,28909,6661 28065,6661 42783,6661 42783,20909 28065,20909 28065,20909 28065,28065 42783"。使用 gsubfn,我也得到了结果。 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12 ] [1,] 20909 20909 20909 20909 20909 6661 6661 6661 6661 1591 1591 1591 [2,] 6661 1591 28065 42783 3113 1591 28065 42783 3113 8.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2021-08-17
    • 2022-10-05
    • 1970-01-01
    相关资源
    最近更新 更多