使用str_extract_all从文本中提取模式作为标签

使用str_extract_all从文本中提取模式作为标签,r,twitter,vector,character,stringr,R,Twitter,Vector,Character,Stringr,我对stringr:str\u extract\u all包的功能有问题 我想在我的案例中提取一个模式,即字符向量中的hashtags。我的数据是: 'data.frame': 2858732 obs. of 15 variables: $ created_at : Factor w/ 995761 levels "Fri Sep 12 00:00:00 +0000 2014",..: 164928 164929 164931 164931 164932 164937

我对stringr:str\u extract\u all包的功能有问题 我想在我的案例中提取一个模式,即字符向量中的hashtags。我的数据是:

'data.frame':   2858732 obs. of  15 variables:
 $ created_at           : Factor w/ 995761 levels "Fri Sep 12 00:00:00 +0000 2014",..: 164928 164929 164931 164931 164932 164937 164938 164938 164940 164940 ...
 $ favorite_count       : int  0 0 0 0 0 0 0 0 0 0 ...
 $ favorited            : Factor w/ 1 level "false": 1 1 1 1 1 1 1 1 1 1 ...
 $ id                   : num  5.09e+17 5.09e+17 5.09e+17 5.09e+17 5.09e+17 ...
 $ id_str               : num  5.09e+17 5.09e+17 5.09e+17 5.09e+17 5.09e+17 ...
 $ in_reply_to_status_id: Factor w/ 747393 levels "11111569142",..: 747393 747393 7983 747393 747393 7893 747393 7994 747393 747393 ...
 $ in_reply_to_user_id  : Factor w/ 594092 levels "1000001311","1000004054",..: 594092 594092 283452 594092 594092 39362 594092 87635 594092 594092 ...
 $ lang                 : Factor w/ 60 levels "am","ar","bg",..: 13 13 13 13 13 13 13 13 13 13 ...
 $ retweet_count        : int  0 0 0 0 0 0 0 0 0 0 ...
 $ retweeted            : Factor w/ 1 level "false": 1 1 1 1 1 1 1 1 1 1 ...
 $ text                 : chr  "RT directe indirectecat Una nit dencartellada o perqu guanyarem http//tco/Sp09q6MVvq" "RT manelmarquez Grande TeresaForcadesF PConstituentLos poderosos no slo estn en Espaa tambin en Catalunya 9N2014 11S2014 htt" "AnastasyHope mas vale suspiro sino todo seria culpa mia aparco y quito las llaves del contacto" "RT VyvyanBasterd No s qu collons em passa avui per m'acabo de llevar amb una trempera sobrenatural vaig a aprofitar per a despe"| __truncated__ ...
 $ user_screen_name     : Factor w/ 3022692 levels "000000000096_",..: 929035 441496 1110467 741648 256996 569276 152104 2716367 2755620 2657050 ...
 $ created              : POSIXct, format: "2014-09-08 07:59:40" "2014-09-08 07:59:41" ...
 $ created.day          : POSIXct, format: "2014-09-08" "2014-09-08" ...
 $ created.hours        : POSIXct, format: "2014-09-08 08:00:00" "2014-09-08 08:00:00" ...
还有我的剧本:

doc0_hashtags = str_extract_all(doc0_hash$text, "#\\w+")
该功能工作正常,但效果不佳。输出全部为character0。我怎样才能纠正它? 我尝试使用另一种方法使用此函数提取哈希标记:

extract.hashes = function(vec){

  hash.pattern = "#[[:alpha:]]+"
  have.hash = grep(x = vec, pattern = hash.pattern)

  hash.matches = gregexpr(pattern = hash.pattern,
                          text = vec[have.hash])
  extracted.hash = regmatches(x = vec[have.hash], m = hash.matches)

  df = data.frame(table(tolower(unlist(extracted.hash))))
  colnames(df) = c("tag","freq")
  df = df[order(df$freq,decreasing = TRUE),]
  return(df)
}
但当我使用它时,我有一个错误作为输出,如下所示:

dat = head(extract.hashes(as.matrix(doc0_hash$text)),50)


Error in `colnames<-`(`*tmp*`, value = c("tag", "freq")) :
  'names' attribute [2] must be the same length as the vector [1]
dput(head(doc0$text))

c("RT directe indirectecat Una nit dencartellada o perqu guanyarem http//tco/Sp09q6MVvq",
"RT manelmarquez Grande TeresaForcadesF PConstituentLos poderosos no slo estn en Espaa tambin en Catalunya 9N2014 11S2014 htt",
"AnastasyHope mas vale suspiro sino todo seria culpa mia aparco y quito las llaves del contacto",
"RT VyvyanBasterd No s qu collons em passa avui per m'acabo de llevar amb una trempera sobrenatural vaig a aprofitar per a despertar",
"RT FIL0S0FIA El amor no se manifiesta en el deseo de acostarse con alguien sino en el deseo de dormir junto a alguien",
"MMjencarlos no es   sino bulgaro Pero las dos idiomas se parecen"
)

非常感谢

在数据的相关部分使用dput和head制作一个可复制的示例。另外,您可能对qdapRegex包中的函数rm_hash,extract=TRUE感兴趣。PS我认为你的错误来自于一些tweet有不止一个散列标签。您需要找出如何以不同的方式存储。但是没有数据也不能确定。谢谢你的回复。我现在尝试使用这个软件包,但它在我的文本中找不到hashtags,我认为这是不可能的,因为我的数据集太大。我在您提供的文本中没有看到任何hashtags,即abc。这就是为什么得到character0和rm_哈希不起作用的原因。没有什么可提取的。同意泰勒的说法。投票关闭为不可再现的用户错误。