R mapply返回列表中的函数调用不是数据帧
我有一个自定义函数。当我手动运行该函数时,它返回一个数据帧:R mapply返回列表中的函数调用不是数据帧,r,R,我有一个自定义函数。当我手动运行该函数时,它返回一个数据帧: > 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 r
> 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
但是,我希望多次运行此操作,因此我在数据帧的每一行上使用mappy
。以下是数据,数据框仅为1行(用于测试):
或:
下面是函数。它需要twitter授权,所以我不确定如何轻松地重新运行它。函数本身是否会使mapply
返回列表而不是数据帧
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))
}
库(rtweet)
图书馆(tidytext)
图书馆(tidyverse)
图书馆(BBmisc)
图书馆(重塑)
创建令牌(
app=“可翻转情绪分析”,
消费者密钥=c_k,
消费者秘密=消费者秘密,
access_token您的create_token_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))
太好了,谢谢。我不知道关于mapply的事
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))
}
mapply(create_sentiment_df, datt1$twtr_handle, datt1$party, datt1$result, SIMPLIFY = FALSE)
do.call(rbind, mapply(create_sentiment_df, datt1$twtr_handle,
datt1$party, datt1$result, SIMPLIFY = FALSE))