R语言rename函数报错:unused argument (word = value)的原因与解决
问题描述
数据集
dput(model$top_terms) structure(c("occupations", "professionals", "isco", "ict", "education", "demand", "sector", "shortages", "shortage", "number", "labour", "www", "health", "workers", "accessed", "supply", "market", "skills", "thousand", "graduates", "skills", "workers", "professionals", "employment", "demand", "sector", "services", "eu", "jobs", "sectors", "health", "occupations", "expected", "occupation", "key", "service", "group", "business", "ict", "future", "www", "http", "de", "http_www", "training", "pdf", "labour", "social", "es", "amp", "employment", "pl", "en", "skills", "national", "system", "https", "czech", "romania", "la", "nbsp", "amp", "type", "type_nbsp", "indicator", "percentage", "year", "rate", "unemployment", "area", "indicator_nbsp", "euro", "euro_area", "eu", "quarter", "preffix", "preffix_nbsp", "point", "percentage_point", "nbsp_year", "education", "training", "learning", "skills", "education_training", "vocational", "young", "people", "european", "vet", "young_people", "youth", "cedefop", "support", "digital", "information", "vocational_education", "school", "apprenticeships", "work", "european", "commission", "social", "policy", "eu", "cedefop", "nbsp", "vet", "labour", "europe", "work", "european_commission", "skills", "conference", "national", "policies", "ilo", "employment", "future", "development", "employment", "labour", "social", "market", "eu", "labour_market", "people", "work", "economic", "workers", "growth", "unemployment", "low", "report", "oecd", "countries", "high", "women", "working", "million", "skills", "skill", "cedefop", "jobs", "eu", "data", "labour", "panorama", "european", "skills_panorama", "market", "job", "labour_market", "mismatch", "survey", "work", "information", "demand", "future", "employers", "skills", "anticipation", "labour", "skills_anticipation", "education", "labour_market", "market", "national", "http", "employment", "www", "regional", "ministry", "data", "information", "demand", "forecasts", "lt", "training", "gt", "de", "en", "des", "www", "la", "les", "studies", "training", "van", "http_www", "ams", "emploi", "portugal", "und", "employment", "luxembourg", "pdf", "du", "france", "het"), dim = c(20L, 10L ), dimnames = list(NULL, c("t_1", "t_2", "t_3", "t_4", "t_5", "t_6", "t_7", "t_8", "t_9", "t_10")))
运行的可视化代码
library(reshape) #visualising topics of words based on the max value of phi set.seed(1234) final_summary_words <- data.frame(top_terms = t(model$top_terms)) final_summary_words$topic <- rownames(final_summary_words) rownames(final_summary_words) <- 1:nrow(final_summary_words) final_summary_words <- final_summary_words %>% melt(id.vars = c("topic")) final_summary_words <- final_summary_words %>% rename(word = value) %>% select(-variable) final_summary_words <- left_join(final_summary_words,allterms) final_summary_words <- final_summary_words %>% group_by(topic,word) %>% arrange(desc(value)) final_summary_words <- final_summary_words %>% group_by(topic, word) %>% filter(row_number() == 1) %>% ungroup() %>% tidyr::separate(topic, into =c("t","topic")) %>% select(-t) word_topic_freq <- left_join(final_summary_words, original_tf, by = c("word" = "term")) pdf("cluster.pdf") for(i in 1:length(unique(final_summary_words$topic))) { wordcloud(words = subset(final_summary_words ,topic == i)$word, freq = subset(final_summary_words ,topic == i)$value, min.freq = 1, max.words=200, random.order=FALSE, rot.per=0.35, colors=brewer.pal(8, "Dark2"))} dev.off()
报错信息
Error in rename(., word = value) : unused argument (word = value)
错误原因
你加载的是reshape包中的rename函数,而非dplyr包的同名函数,两者语法完全不兼容:
reshape::rename的参数格式为rename(x, replace),其中replace是命名向量,写法是c(旧列名 = "新列名")- 你写的
rename(word = value)是dplyr::rename的语法(新列名 = 旧列名),套用在reshape的rename函数上会触发参数不匹配的错误。
修复方案
有两种可行的修复方式:
方式一:适配reshape包的rename语法
将rename所在行修改为:
final_summary_words <- final_summary_words %>% rename(replace = c(value = "word")) %>% select(-variable)
方式二:使用dplyr包的rename函数
- 若未安装
dplyr,先运行install.packages("dplyr")完成安装 - 为避免函数冲突,明确指定使用
dplyr的rename:
final_summary_words <- final_summary_words %>% dplyr::rename(word = value) %>% select(-variable)
注:代码中用到的%>%、left_join、group_by等都是dplyr的函数,建议直接加载dplyr包,减少潜在的函数冲突问题。
内容的提问来源于stack exchange,提问作者adrCoder
相关产品推荐
相关产品推荐

