【问题标题】:R Creating co-occurrence matrixR创建共现矩阵
【发布时间】:2018-11-24 19:48:24
【问题描述】:

我的问题是关于文本挖掘和文本处理的。 我想根据我的数据构建一个共现矩阵。 我的数据是:

dat <- read.table(text="id_reférence id_paper
        621107   621100
        621100   621101
        621107   621102
        621109   621103
        621105   621104
        621103   621105
        621109   621106
        621106   621107
        621107   621108
        621106   621109", header=T)

expected <- matrix(0,10,10)
### Article 1 has been cited by article 2
expected[2, 1] <- 1

提前致谢:)

【问题讨论】:

    标签: r matrix text text-mining adjacency-matrix


    【解决方案1】:
    # loop through the observations of dat
    for(i in seq_len(nrow(dat))) {
      # convert reference ids to integer and store in a vector
      # example data requires this step, you may already have integers in your actual data
      ref <- as.integer(strsplit(as.character(dat$id_reférence[i]), ",")[[1]])
      # loop through the list of references
      for(j in ref) {
        # mark the citations using (row, column) ~ (i, j) pairs
        expected[dat$id_paper[i], j] <- 1
      }
    }
    
    expected
    #      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
    # [1,]    0    1    0    0    0    0    0    0    0     0
    # [2,]    0    0    0    1    0    0    0    1    0     0
    # [3,]    1    0    0    0    1    0    0    0    0     0
    # [4,]    0    0    0    0    0    0    0    1    0     0
    # [5,]    0    0    0    1    1    0    0    0    1     0
    # [6,]    0    0    1    0    0    0    0    1    0     0
    # [7,]    0    1    0    1    0    0    0    0    0     0
    # [8,]    0    0    0    0    0    1    0    0    1     0
    # [9,]    0    0    0    0    0    0    0    0    0     1
    # [10,]   1    0    0    1    0    0    0    0    1     0
    

    【讨论】:

    • 抱歉,如果没有reproducible 的问题,我无法帮助您。您更新了问题,这些答案都不再有效。新问题似乎也不够笼统。如果您的示例数据集不能代表原始数据集,您将不断遇到错误并浪费时间。
    【解决方案2】:

    这里使用data.table 的另一种方法。瓶颈可能是以下方法不会以sparseMatrix 结束。根据数据集的大小,可能值得检查针对稀疏数据对象的方法。

    library(data.table)
    setDT(dat)
    # split id_reférence column into multiple rows by comma
    # code for this step taken from: #https://stackoverflow.com/questions/13773770/split-comma-separated-strings-in-a-column-into-separate-rows
    dat = dat[, strsplit(as.character(id_reférence), ",", fixed=TRUE),
       by = .(id_paper, id_reférence)][, id_reférence := NULL][
        , setnames(.SD, "V1", "id_reférence")]
    # add value column for casting
    dat[, cite:= 1]
    # cast you data into long format
    dat = dcast(dat, id_paper ~ id_reférence, fill = 0)[, id_paper:= NULL]
    

    【讨论】:

    • 您发布的数据未生成正确的表格数据。你能再检查一下吗,拜托。旁注,您也可以使用dput 发布数据示例。
    • 循环不一定比其他替代方案慢,或者不使用循环的代码不一定更快。不错的data.table 方法!
    • 你是对的,如果你启动你的数据对象循环通常没问题。我将删除答案的相应部分。感谢您仍然认可我的做法。
    • 对于您更新的数据,您可以使用我的答案的代码 - 只需跳过用逗号拆分为多行的步骤,您就在那里。请考虑接受@Ozan147 的回答(如果他们为您提供可行的解决方案,这也将引导您获得您想要的内容)或我的回答。
    • 非常感谢您的回答。我已经在这个项目上工作了几个晚上,我别无选择,只能提出问题以继续前进。
    猜你喜欢
    • 2012-10-28
    • 1970-01-01
    • 2020-03-21
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多