2017-08-12 32 views
1

我有一个推文数据框,我想添加一个不存在的“retweetCount”列。我使用了以下内容:将retweetCount列添加到R中的数据框中

tweets$retweetCount <- tweets %>% 
group_by(text) %>% 
summarize(count = n()) 

这引发以下错误:

Error in `$<-.data.frame`(`*tmp*`, retweetCount, value = list(text = c("- 
#WetterOnline Pro by @WetterOnline #Wetter #Berlin, : 
replacement has 48780 rows, data has 137659 

看来,我无法计算在没有转推的情况下“retweetCount”的值。

head(data): 
id_str <chr>, from_user <chr>, text <chr>, created_at <chr>, time <chr>, 
geo_coordinates <chr>,user_lang <chr>, in_reply_to_user_id_str <chr>, 
in_reply_to_screen_name <chr>,from_user_id_str <chr>, 
in_reply_to_status_id_str <chr>, source <chr>,profile_image_url <chr>, 
user_followers_count <int>, user_friends_count <int>,user_location <chr>, 
status_url <chr>, entities_str <chr>, date <date> 

dput(data) 
.Names = c("id_str", "from_user", "text", 
"created_at", "time", "geo_coordinates", "user_lang", 
"in_reply_to_user_id_str", 
"in_reply_to_screen_name", "from_user_id_str", "in_reply_to_status_id_str", 
"source", "profile_image_url", "user_followers_count", "user_friends_count", 
"user_location", "status_url", "entities_str", "date", "ehe", 
"eggheads"), row.names = c(NA, -137659L), class = c("tbl_df", 
"tbl", "data.frame")) 
+0

难道您发布的数据或它的一个样本,以便W¯¯可以帮助你吗? –

回答

0

如果我理解正确(没有看到数据),这应该可以解决您的问题。

aux <- tweets %>% 
    group_by(text) %>% 
    summarize(retweetCount = n()) 

tweets <- inner_join(tweets, aux, by = "text") 
相关问题