错误提示:"no applicable method for 'regroup' applied to an object of class "c('integer', 'numeric')""

3

你好,我刚开始使用 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

同样适用于第二篇文章。
3258  4521
3258  4528
4528  4521

一些其他的信息是:
``` dput(head(w2,)) structure(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") ```
(注:这是一段R语言代码,其中包含了一个数据框的结构。)
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 
}

任何建议都将不胜感激。
1个回答

1
我猜测从dplyr do操作的列表输出分配名称中阅读来看,这在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对象”。 - Naveed Khan Wazir
@user3841811。使用相同的数据集,我没有任何错误。如果你可以使用dput在一个较小的数据集(10-20行)上展示出现错误,我会尝试解决。 - akrun
实际上,上面的数据只是整个数据集的一个子集,其中一些信息是dput(head(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")。 - Naveed Khan Wazir
@user3841811。使用我的第二种方法,我得到了结果。res$combN [1] "1591 3113, 1591 6661, 1591 20909, 1591 28065, 1591 42783, 3113 6661, 3113 20909, 3113 28065, 3113 42783, 6661 20909, 6661 28065, 6661 42783, 20909 28065, 20909 42783, 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 28065 42.... - akrun
user3841811。是的,正如我之前提到的,并在链接的评论中发现的那样,目前存在一些限制(也许我错了)。 - akrun
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接