【问题标题】:Use a library function in the apply function在应用函数中使用库函数
【发布时间】:2019-08-20 12:16:08
【问题描述】:

我有一个数据框,其中包含一个名为“msgText”的列,其中包含文本。为此,我想创建另一个名为“wordcount”的列,用于计算“msgText”每行包含多少个单词。

该列如下所示:

head(all_transcripts$msgText)
[1]  "Hi, my name is Chris and I am a programmer"                                                                                                                                                                                                   
[2]  "I have worked with R for 12 years"                                                                                                                                                                                                                                                                                                                                                           
[3]  "Being a programmer I have many questions" 
[4]  "The fellow programmers at Stackoverflow help me to get the answer"                                                                                                                                                                                  
[5]  "This help has saved my life many times."                                                                                                                                                                                                                                                                                                                                                                                                        
[6]  "Thanks Stackoverflow!"      

而我想要的结果是:

head(all_transcripts$wordcount)
    [1]  10                                                                                                                                                                                                   
    [2]  8                                                                                                                                                                                                                                                                                                                                                           
    [3]  7 
    [4]  11                                                                                                                                                                                  
    [5]  8                                                                                                                                                                                                                                                                                                                                                                                                        
    [6]  2  

为此,我使用 ngram 库和 wordcount 函数。

我试过了:

all_transcripts$wordcount <- apply(all_transcripts, 2, 
                                   wordcount(all_transcripts$msgText))

但是,这样做我得到了以下错误:

Error in match.fun(FUN) :    'wordcount(all_transcripts$msgText)' is
not a function, character or symbol

如何正确使用 apply 函数,而不必在我的数据集上使用 for 循环?

【问题讨论】:

  • 我认为您正在遍历该列,然后再次提取该列。也许你只需要all_transcripts$wordcount &lt;- wordcount(all_transcripts$msgText)
  • 不行,然后获取整列所有单词的字数
  • 您能发布一小部分数据吗?这样我们就更容易提供帮助了。
  • Emil,您需要提供一个重现性更高的示例。请以明确格式提供示例数据,例如dput(head(all_transcripts)),以及您的预期/预期输出(因为根据您的描述,您的意思并不清楚)。
  • apply 的第三个参数中需要一个函数。现在,您得到的不是函数,而是wordcount(all_transcripts$msgText) 返回的任何内容...可能是一个向量

标签: r dataframe apply word-count


【解决方案1】:

考虑向量化的lengthsstrsplit 用于使用base R 进行字数统计:

all_transcripts$word_count <- lengths(strsplit(all_transcripts$text, split=" "))

all_transcripts

#                                                                text word_count
# 1                        Hi, my name is Chris and I am a programmer         10
# 2                                 I have worked with R for 12 years          8
# 3                          Being a programmer I have many questions          7
# 4 The fellow programmers at Stackoverflow help me to get the answer         11
# 5                           This help has saved my life many times.          8
# 6                                             Thanks Stackoverflow!          2

数据

all_transcripts <- data.frame(text=c("Hi, my name is Chris and I am a programmer",
                                     "I have worked with R for 12 years",
                                     "Being a programmer I have many questions",
                                     "The fellow programmers at Stackoverflow help me to get the answer",
                                     "This help has saved my life many times.",
                                     "Thanks Stackoverflow!"),
                              stringsAsFactors=FALSE)

【讨论】:

    【解决方案2】:

    我们可以遍历“msgText”的元素并应用wordcount函数

    library(ngram)
    library(tidyverse)
    all_transcripts %>%
         mutate(wordcount = map_int(msgText, wordcount))
    #                                                             msgText wordcount
    #1                        Hi, my name is Chris and I am a programmer        10
    #2                                 I have worked with R for 12 years         8
    #3                          Being a programmer I have many questions         7
    #4 The fellow programmers at Stackoverflow help me to get the answer        11
    #5                           This help has saved my life many times.         8
    #6                                             Thanks Stackoverflow!         2
    

    base R

    all_transcripts$wordcount <- sapply(all_transcripts$msgText, wordcount)
    

    OP 代码中的问题是它循环通过列(apply 中的MARGIN = 2),其中向量(alltranscripts$wordcount)没有dim 属性

    数据

    all_transcripts <- structure(list(msgText = c("Hi, my name is Chris and I am a programmer", 
    "I have worked with R for 12 years", "Being a programmer I have many questions", 
    "The fellow programmers at Stackoverflow help me to get the answer", 
    "This help has saved my life many times.", "Thanks Stackoverflow!"
    )), class = "data.frame", row.names = c(NA, -6L))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-27
      • 2015-09-05
      • 1970-01-01
      • 2016-01-22
      • 2017-06-17
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多