r - 错误 "no applicable method for ' 重组'应用于类 "c(' 整数的对象', 'numeric' )""

标签 r dplyr

嗨,我是 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 篇文章,我想根据数据框架格式的文章在用户之间建立关系,例如 ##for article one##

1258  54789
1258  547821
54789 547821

##similarly for article 2##

3258  4521
3258  4528
4528  4521

其他一些信息是

dput(头(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('整数','数字')”)的对象##

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 
}

任何建议将不胜感激。

最佳答案

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

你可以这样做:

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/11380733cSplit

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

关于r - 错误 "no applicable method for ' 重组'应用于类 "c(' 整数的对象', 'numeric' )"",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926877/

相关文章:

r - 将字符向量列表写入 R 中的单个文件

跨 NA 和 NULL 行回滚 xts

r - 用 R 覆盖重叠段

R 相当于 Matlab 的 'persistent'

r - 根据 dplyr 中的列保留组之间的不同行

r - 使用 tidyverse 进行条件过滤

r - dplyr::failwith 不适用于 lme4::lmer 但适用于 lm

R dplyr : summarise complete cases by group for all variables

r - 如何按行获取非零元素的均值,改变条件使用的列

r - 如何根据 R 生成的表创建新的数据框?