Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
撤消stru_拆分以合并单词是一个段落吗?(R)_R_For Loop - Fatal编程技术网

撤消stru_拆分以合并单词是一个段落吗?(R)

撤消stru_拆分以合并单词是一个段落吗?(R),r,for-loop,R,For Loop,我目前有一个27段的列表,名为psw_list,可以通过调用psw_list$p1来引用第一段,如下所示: > psw_list$p1 [1] "For" "more" "than" "five" "years," "William" "Sencion"

我目前有一个27段的列表,名为
psw_list
,可以通过调用
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."  
我想删除每个单词之间的空格,这一行会删除单个行,
粘贴(psw_list$p1,collapse=“”)
,但在for循环中不起作用:

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

运行此循环时,我遇到了一个意外的“[”错误。我希望使用for循环,因为我希望它遍历每个段落,并将单词折叠在一起,用一个空格隔开,有什么想法吗?

这里有一个不使用
for
-循环的解决方案:

lapply(psw_list, paste, collapse = " ")
for (i in seq_along(psw_list)) {
  print(paste(psw_list[[i]], collapse = " "))
}
或者,如果您真的想要
for
-循环:

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

以下是一个不带
for
-循环的解决方案:

lapply(psw_list, paste, collapse = " ")
for (i in seq_along(psw_list)) {
  print(paste(psw_list[[i]], collapse = " "))
}
或者,如果您真的想要
for
-循环:

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

您的错误是由于
psw\u list$[i]
导致的,如果您将其替换为
psw\u list[[i]]
,它将选择列
i
,它将毫无问题地运行

另一个解决方案是使用
apply()
系列。例如,运行以下程序:

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

您的错误是由于
psw\u list$[i]
导致的,如果您将其替换为
psw\u list[[i]]
,它将选择列
i
,它将毫无问题地运行

另一个解决方案是使用
apply()
系列。例如,运行以下程序:

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

如果要在列表的每个元素上循环,请尝试使用
sw_list[[i]]
。您的意思是在循环的参数中吗?如果要在列表的每个元素上循环,请尝试使用
sw_list[[i]]
。您的意思是在循环的参数中吗?