简体   繁体   English

undo str_split 组合单词是一个段落? (右)

[英]undo str_split to combine words is a paragraph? (R)

I currently have a list of 27 paragraphs called psw_list and can refer to the first one by calling psw_list$p1 , and it looks like this:我目前有一个名为psw_list的 27 个段落的列表,可以通过调用psw_list$p1来引用第一个,它看起来像这样:

> psw_list$p1

 [1] "For"          "more"         "than"         "five"         "years,"       "William"      "Sencion"     
 [8] "did"          "the"          "same"         "task"         "over"         "and"          "over."       
[15] "He"           "signed"       "onto"         "the"          "New"          "York"         "City’s"      
[22] "housing"      "lottery"      "site"         "and"          "applied"      "for"          "one"         
[29] "of"           "the"          "city’s"       "highly"       "coveted,"     "below-market" "apartments." 
[36] "Each"         "time,"        "he"           "got"          "the"          "same"         "response:"   
[43] "silence."  

I want to remove the spaces between each word and this line does so to individual lines, paste(psw_list$p1,collapse=" ") , but does not work in a for loop:我想删除每个单词之间的空格,并且这一行对单独的行执行此操作, paste(psw_list$p1,collapse=" ") ,但在 for 循环中不起作用:

for (i in seq_along(psw_list)) {
  print(paste(psw_list$[i], collapse = " "))
}

I'm getting an unexpected '[' error when I run this loop.运行此循环时unexpected '['错误。 I want the for loop because I want it to go through each paragraph and collapse the words together separated by that one space, any ideas?我想要 for 循环,因为我希望它通过每一段 go 并将单词折叠在一起,由一个空格分隔,有什么想法吗?

Here is a solution without the for -loop:这是一个没有for循环的解决方案:

lapply(psw_list, paste, collapse = " ")

or if you really want the for -loop:或者如果你真的想要for循环:

for (i in seq_along(psw_list)) {
  print(paste(psw_list[[i]], collapse = " "))
}

Your error is due to psw_list$[i] if you replace this with psw_list[[i]] which selects the column i it will run without a problem.您的错误是由于psw_list$[i]如果您将其替换为psw_list[[i]]选择的列i它将毫无问题地运行。

Another solution to this would be to use the apply() family.另一个解决方案是使用apply()系列。 For instance running the following:例如运行以下命令:

lapply(psw_list, function(i) paste(i, collapse = " "))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM