[英]how to generate a vector or factor from a numeric matrix based on logic (no for loop)
[英]How to split out numeric vector of bigrams from TDM matrix
我在R中有一個大數字(46201個元素,3.3 Mb)。
tdm_pairs.matrix <- as.matrix(tdm_pairs)
top_pairs <- colSums(tdm_pairs.matrix)
head(sort(top_pairs, decreasing = T),2)
i know i dont i think i can i just i want
46 42 41 31 30 28
我試着把每個分開:
unlist(strsplit(as.character(top_pairs)," "))
"46" "42" "41" "31" "30" "28"
我正在尋找拆分每個,所以輸出將類似於此:
"i" "know" "46"
"i" "dont" "42"
由於文件很大,因此您可能要使用stringi
library(stringi)
data.frame(stri_split_fixed(names(top_pairs), " ", simplify=T),
count=top_pairs, row.names=seq_along(top_pairs))
# X1 X2 count
# 1 i know 46
# 2 i dont 42
像這樣嗎
> top_pairs <- structure(c(46, 42), .Names = c("i know", "i dont"))
> do.call(rbind, strsplit(paste(names(top_pairs), top_pairs), " "))
[,1] [,2] [,3]
[1,] "i" "know" "46"
[2,] "i" "dont" "42"
或者,如果您想保留數值,則可以使用tidyr
轉換為數據框:
> library(magrittr)
> library(tidyr)
> data.frame(names=names(top_pairs), count=top_pairs) %>%
separate(names, into=c("name1", "name2"), sep=" ") %>%
set_rownames(NULL)
name1 name2 count
1 i know 46
2 i dont 42
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.