1

如何将单词的单个字符向量拆分为向量列表,其中列表中的每个向量都包含一定数量的单词?

例如:

# reproducible data
examp <- "Before asking a technical question by e-mail, or in a newsgroup, or on a website chat board, do the following:  Try to find an answer by searching the archives of the forum you plan to post to. Try to find an answer by searching the Web. Try to find an answer by reading the manual. Try to find an answer by reading a FAQ. Try to find an answer by inspection or experimentation. Try to find an answer by asking a skilled friend. If you're a programmer, try to find an answer by reading the source code. When you ask your question, display the fact that you have done these things first; this will help establish that you're not being a lazy sponge and wasting people's time. Better yet, display what you have learned from doing these things. We like answering questions for people who have demonstrated they can learn from the answers. Use tactics like doing a Google search on the text of whatever error message you get (searching Google groups as well as Web pages). This might well take you straight to fix documentation or a mailing list thread answering your question. Even if it doesn't, saying “I googled on the following phrase but didn't get anything that looked promising” is a good thing to do in e-mail or news postings requesting help, if only because it records what searches won't help. It will also help to direct other people with similar problems to your thread by linking the search terms to what will hopefully be your problem and resolution thread. Take your time. Do not expect to be able to solve a complicated problem with a few seconds of Googling. Read and understand the FAQs, sit back, relax and give the problem some thought before approaching experts. Trust us, they will be able to tell from your questions how much reading and thinking you did, and will be more willing to help if you come prepared. Don't instantly fire your whole arsenal of questions just because your first search turned up no answers (or too many). Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. The more you do to demonstrate that having put thought and effort into solving your problem before seeking help, the more likely you are to actually get help. Beware of asking the wrong question. If you ask one that is based on faulty assumptions, J. Random Hacker is quite likely to reply with a uselessly literal answer while thinking Stupid question..., and hoping the experience of getting what you asked for rather than what you needed will teach you a lesson."

目前我正在这样做(基于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))))

这对于我目前的目的来说大致足够了,但我怀疑它可以改进。什么是更有效和准确的方法?

4

1 回答 1

8

你可以试试下面

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."     
## 
于 2013-04-05T10:53:47.303 回答