是否可以替换如下的for循环:
library(quanteda)
library(quanteda.dictionaries)
#dummy data
df <- data.frame(text = c("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown pr
inter took a galley of type and scrambled it to make a type specimen book."))
for (j in 1:nrow(df)) {
out <- liwcalike(df$text[j],
dictionary = data_dictionary_NRC)
dfm <- rbind(dfm, data.frame(em1 = out$trust, em2= out$anger))
}以减少执行时间?
发布于 2019-02-12 16:39:54
构建一个数据帧列表,并将重新绑定一次外部循环,并避免使用rbind内部循环进行四向复制:
df_list <- lapply(df$text, function(txt) {
out <- liwcalike(txt, dictionary = data_dictionary_NRC)
return(data.frame(em1 = out$trust, em2= out$anger, origin=txt))
}
final_df <- do.call(rbind, df_list)如果liwcalike调用有任何问题,请在tryCatch中包装过程,以返回任何错误的NA-row数据帧:
df_list <- lapply(df$text, function(txt) {
tryCatch({
out <- liwcalike(txt, dictionary = data_dictionary_NRC)
return(data.frame(em1=out$trust, em2=out$anger, origin=txt, error=NA))
}, error = function(e)
data.frame(em1=NA, em2=NA, origin=txt, error=e)
)
}
final_df <- do.call(rbind, df_list)https://stackoverflow.com/questions/54654458
复制相似问题