R:如何快速地从一个非常大的表格中选择两列中的常用单词或相同数字?

标签 r dataframe data-science

我有一个非常大的表 (1,000,000 X 20) 需要处理并且需要快速处理。

例如,我的表中有 2 列 X2 和 X3:

enter image description here

    X1  X2                                          X3
c1  1   100020003001, 100020003002, 100020003003    100020003001, 100020003002, 100020003004
c2  2   100020003001, 100020004002, 100020004003    100020003001, 100020004007, 100020004009
c3  3   100050006003, 100050006001, 100050006001    100050006011, 100050006013, 100050006021

现在我想创建 2 个新列,其中包含

1)常用词或相同数字

例如:[1] "100020003001""100020003002"

2) 常用词或相同数字的个数

例如:[1] 2

我试过下面线程的方法,但是,处理时间很慢,因为我是用 f​​or 循环做的:

Count common words in two strings

 library(stringi)
 Reduce(`intersect`,stri_extract_all_regex(vec1,"\\w+"))

感谢您的帮助! 我真的在这里挣扎......

最佳答案

我们可以通过,拆分'X2','X3'列,得到对应list元素的intersectmap2 并使用 lengths 来“计算”list

中的元素数量
library(tidyverse)
df1 %>%
   mutate(common_words = map2(strsplit(X2, ", "),
                              strsplit(X3, ", "),  
                                   intersect), 
          count = lengths(common_words))
# X1                                       X2                                       X3
#1  1 100020003001, 100020003002, 100020003003 100020003001, 100020003002, 100020003004
#2  2 100020003001, 100020004002, 100020004003 100020003001, 100020004007, 100020004009
#3  3 100050006003, 100050006001, 100050006001 100050006011, 100050006013, 100050006021
#                common_words count
#1 100020003001, 100020003002     2
#2               100020003001     1
#3                                0

或者使用base R

df1$common_words <- Map(intersect, strsplit(df1$X2, ", "), strsplit(df1$X3, ", "))
df1$count <- lengths(df1$common_words)

数据

df1 <- structure(list(X1 = 1:3, X2 = c("100020003001, 100020003002, 100020003003", 
"100020003001, 100020004002, 100020004003", "100050006003, 
 100050006001, 100050006001"
 ), X3 = c("100020003001, 100020003002, 100020003004", "100020003001, 
 100020004007, 100020004009", 
 "100050006011, 100050006013, 100050006021")), class = "data.frame", 
  row.names = c("c1", "c2", "c3"))

关于R:如何快速地从一个非常大的表格中选择两列中的常用单词或相同数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52142521/

相关文章:

r - 使随机相关矩阵半定正

python - 重新排序数据框列包含按升序排列的字符串和数字

r - 按照以下方法计算销售额

python - 如何从 "class"内的 html "span"中获取/抓取所有元素?

r - 在 R 中更新 data.table 中的整行

r - 如何将数据框中的行与 dplyr group by 合并并展平其他数据?

machine-learning - 如何在 Pyspark 中获得直线线性回归结果?

scala - 将scala映射值与列表进行比较,并为列表中不存在的键返回默认值

r - 具有离散值的热图/图像

python-3.x - 在Python中将特定的中文标点符号替换为相应的英文标点符号