r - split a character vector every n words -
how can split single character vector of words list of vectors each vector in list contains number of words?
for example:
# reproducible data examp <- "before asking technical question e-mail, or in newsgroup, or on website chat board, following: try find answer searching archives of forum plan post to. try find answer searching web. try find answer reading manual. try find answer reading faq. try find answer inspection or experimentation. try find answer asking skilled friend. if you're programmer, try find answer reading source code. when ask question, display fact have done these things first; establish you're not being lazy sponge , wasting people's time. better yet, display have learned doing these things. answering questions people have demonstrated can learn answers. use tactics doing google search on text of whatever error message (searching google groups web pages). might take straight fix documentation or mailing list thread answering question. if doesn't, saying “i googled on following phrase didn't looked promising” thing in e-mail or news postings requesting help, if because records searches won't help. direct other people similar problems thread linking search terms problem , resolution thread. take time. not expect able solve complicated problem few seconds of googling. read , understand faqs, sit back, relax , give problem thought before approaching experts. trust us, able tell questions how reading , thinking did, , more willing if come prepared. don't instantly fire whole arsenal of questions because first search turned no answers (or many). prepare question. think through. hasty-sounding questions hasty answers, or none @ all. more demonstrate having put thought , effort solving problem before seeking help, more help. beware of asking wrong question. if ask 1 based on faulty assumptions, j. random hacker quite reply uselessly literal answer while thinking stupid question..., , hoping experience of getting asked rather needed teach lesson."
currently doing (based on https://stackoverflow.com/a/3318390/1036500):
xx <- unlist(strsplit(examp, " ")) xxdf <- data.frame(examp = xx) n <- 50 # split every how many words? exampsplit <- split(xxdf, factor(sort(rank(row.names(xxdf))%%round(length(unlist(strsplit(examp, " ")))/n,0))))
this enough current purpose, suspect can improved on. more efficient , accurate method?
you can try below
x <- unlist(strsplit(examp, "\\s+")) y <- split(x, seq_along(x)%/%50) y ## $`0` ## [1] "before" "asking" "a" "technical" "question" ## [6] "by" "e-mail," "or" "in" "a" ## [11] "newsgroup," "or" "on" "a" "website" ## [16] "chat" "board," "do" "the" "following:" ## [21] "" "try" "to" "find" "an" ## [26] "answer" "by" "searching" "the" "archives" ## [31] "of" "the" "forum" "you" "plan" ## [36] "to" "post" "to." "try" "to" ## [41] "find" "an" "answer" "by" "searching" ## [46] "the" "web." "try" "to" ## ## $`1` ## [1] "find" "an" "answer" "by" ## [5] "reading" "the" "manual." "try" ## [9] "to" "find" "an" "answer" ## [13] "by" "reading" "a" "faq." ## [17] "try" "to" "find" "an" ## [21] "answer" "by" "inspection" "or" ## [25] "experimentation." "try" "to" "find" ## [29] "an" "answer" "by" "asking" ## [33] "a" "skilled" "friend." "if" ## [37] "you're" "a" "programmer," "try" ## [41] "to" "find" "an" "answer" ## [45] "by" "reading" "the" "source" ## [49] "code." "when" ## ## $`2` ## [1] "you" "ask" "your" "question," "display" ## [6] "the" "fact" "that" "you" "have" ## [11] "done" "these" "things" "first;" "this" ## [16] "will" "help" "establish" "that" "you're" ## [21] "not" "being" "a" "lazy" "sponge" ## [26] "and" "wasting" "people's" "time." "better" ## [31] "yet," "display" "what" "you" "have" ## [36] "learned" "from" "doing" "these" "things." ## [41] "we" "like" "answering" "questions" "for" ## [46] "people" "who" "have" "demonstrated" "they" ## ## $`3` ## [1] "can" "learn" "from" "the" ## [5] "answers." "use" "tactics" "like" ## [9] "doing" "a" "google" "search" ## [13] "on" "the" "text" "of" ## [17] "whatever" "error" "message" "you" ## [21] "get" "(searching" "google" "groups" ## [25] "as" "well" "as" "web" ## [29] "pages)." "this" "might" "well" ## [33] "take" "you" "straight" "to" ## [37] "fix" "documentation" "or" "a" ## [41] "mailing" "list" "thread" "answering" ## [45] "your" "question." "even" "if" ## [49] "it" "doesn't," ## ## $`4` ## [1] "saying" "“i" "googled" "on" "the" ## [6] "following" "phrase" "but" "didn't" "get" ## [11] "anything" "that" "looked" "promising”" "is" ## [16] "a" "good" "thing" "to" "do" ## [21] "in" "e-mail" "or" "news" "postings" ## [26] "requesting" "help," "if" "only" "because" ## [31] "it" "records" "what" "searches" "won't" ## [36] "help." "it" "will" "also" "help" ## [41] "to" "direct" "other" "people" "with" ## [46] "similar" "problems" "to" "your" "thread" ## ## $`5` ## [1] "by" "linking" "the" "search" "terms" ## [6] "to" "what" "will" "hopefully" "be" ## [11] "your" "problem" "and" "resolution" "thread." ## [16] "take" "your" "time." "do" "not" ## [21] "expect" "to" "be" "able" "to" ## [26] "solve" "a" "complicated" "problem" "with" ## [31] "a" "few" "seconds" "of" "googling." ## [36] "read" "and" "understand" "the" "faqs," ## [41] "sit" "back," "relax" "and" "give" ## [46] "the" "problem" "some" "thought" "before" ## ## $`6` ## [1] "approaching" "experts." "trust" "us," "they" ## [6] "will" "be" "able" "to" "tell" ## [11] "from" "your" "questions" "how" "much" ## [16] "reading" "and" "thinking" "you" "did," ## [21] "and" "will" "be" "more" "willing" ## [26] "to" "help" "if" "you" "come" ## [31] "prepared." "don't" "instantly" "fire" "your" ## [36] "whole" "arsenal" "of" "questions" "just" ## [41] "because" "your" "first" "search" "turned" ## [46] "up" "no" "answers" "(or" "too" ## ## $`7` ## [1] "many)." "prepare" "your" "question." ## [5] "think" "it" "through." "hasty-sounding" ## [9] "questions" "get" "hasty" "answers," ## [13] "or" "none" "at" "all." ## [17] "the" "more" "you" "do" ## [21] "to" "demonstrate" "that" "having" ## [25] "put" "thought" "and" "effort" ## [29] "into" "solving" "your" "problem" ## [33] "before" "seeking" "help," "the" ## [37] "more" "likely" "you" "are" ## [41] "to" "actually" "get" "help." ## [45] "beware" "of" "asking" "the" ## [49] "wrong" "question." ## ## $`8` ## [1] "if" "you" "ask" "one" "that" ## [6] "is" "based" "on" "faulty" "assumptions," ## [11] "j." "random" "hacker" "is" "quite" ## [16] "likely" "to" "reply" "with" "a" ## [21] "uselessly" "literal" "answer" "while" "thinking" ## [26] "stupid" "question...," "and" "hoping" "the" ## [31] "experience" "of" "getting" "what" "you" ## [36] "asked" "for" "rather" "than" "what" ## [41] "you" "needed" "will" "teach" "you" ## [46] "a" "lesson." ##
Comments
Post a Comment