代码之家  ›  专栏  ›  技术社区  ›  Adam_G

mapply返回列表中的函数调用不是数据帧

r
  •  1
  • Adam_G  · 技术社区  · 6 年前

    我有一个自定义函数。当我手动运行函数时,它返回一个数据帧:

    > create_sentiment_df('taggreason', 'republican', 'lost')
    Joining, by = "word"
    Joining, by = "word"
          sentiment prop.sentiment twitter.name      party election.result
    1         anger     0.04721931   taggreason republican            lost
    2  anticipation     0.14375656   taggreason republican            lost
    3       disgust     0.01259182   taggreason republican            lost
    4          fear     0.06190976   taggreason republican            lost
    5           joy     0.09024134   taggreason republican            lost
    6      negative     0.10073452   taggreason republican            lost
    7      positive     0.26862539   taggreason republican            lost
    8       sadness     0.03777545   taggreason republican            lost
    9      surprise     0.03882476   taggreason republican            lost
    10        trust     0.19832109   taggreason republican            lost
    

    但是,我想运行这个多次,所以我使用 mapply 在数据帧的每一行上。以下是数据,数据帧只有一行(用于测试):

    > datt1
    # A tibble: 1 x 4
      twtr_handle party      result district_flipped
      <chr>       <chr>      <chr>  <chr>           
    1 taggreason  republican lost   flipped    
    

    然后函数调用:

    rslt <- mapply(create_sentiment_df, datt1$twtr_handle, datt1$party, datt1$result)
    

    返回:

    > rslt
                    taggreason  
    sentiment       Character,10
    prop.sentiment  Numeric,10  
    twitter.name    Character,10
    party           Character,10
    election.result Character,10
    

    enter image description here

    下面是函数。它需要twitter的授权,所以我不知道如何才能轻松地重新运行它。函数本身是否有什么 映射层

    library(rtweet)
    library(tidytext)
    library(tidyverse)
    library(BBmisc)
    library(reshape)
    
    create_token(
      app = "Flippable Sentiment Analysis",
      consumer_key = c_k,
      consumer_secret = c_s,
      access_token <- a_t,
      access_secret <- a_s)
    
    create_sentiment_df <- function(twitter.name, party, election.result) {
      va_stop_words <- stop_words %>% select(-lexicon) %>% 
        bind_rows(data.frame(word = c("https", "t.co", "rt", "amp")))
      nrc_lex <- get_sentiments("nrc") # many sentiments
    
      dat <- get_timeline(twitter.name, n=3200)
      dat$created_at <- as.Date(dat$created_at)
      dat_2017 <- subset(dat, created_at > as.Date('2017-01-01') & created_at < as.Date('2017-11-06'))
    
      dat_words <- dat_2017 %>% 
        select(status_id, text) %>% 
        unnest_tokens(word,text)
    
      dat_words_interesting <- dat_words %>% anti_join(va_stop_words)
      dat_sentiment <- dat_words_interesting %>% left_join(nrc_lex)
      dat_sentiment_count <- dat_sentiment %>% 
        filter(!is.na(sentiment)) %>% 
        group_by(sentiment) %>% 
        summarise(prop.sentiment=n())
      dat_sentiment_count <- na.omit(dat_sentiment_count)
    
      dat_sentiment_count <- cbind(dat_sentiment_count[1], 
                                prop.table(data.matrix(dat_sentiment_count[-1]), margin=2))
    
      # dat_sentiment_count$twitter.name <- NA
      dat_sentiment_count$twitter.name <- twitter.name
      dat_sentiment_count$party <- party
      dat_sentiment_count$election.result <- election.result
    
      return(as.data.frame(dat_sentiment_count))
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Mankind_2000    6 年前

    你的 create_sentiment_df 函数返回data.frame和 mapply

    如果需要data.frames列表,可以执行以下操作:

    mapply(create_sentiment_df, datt1$twtr_handle, datt1$party, datt1$result, SIMPLIFY = FALSE)
    

    如果所有data.frame输出都需要单个data.frame,请使用:

    do.call(rbind, mapply(create_sentiment_df, datt1$twtr_handle,
                          datt1$party, datt1$result, SIMPLIFY = FALSE))