我正在尝试使此处使用的某些系统自动化,以专门基于调查数据生成报告。

可以说我对1个问题有3条评论。

current_comments <- c("too slow", "not fast enough", "bad speed")


基本上我想做的就是将注释合并到一个字符串中,并用“-”分隔,看起来也像这样

>current_comments
[1] "too slow - not fast enough - bad speed"


这样我就可以将其粘贴到一个单元中进行导出。

我知道我可以通过使用粘贴功能来做到这一点。

> paste(current_comments[1], " - ", current_comments[2], " - ", current_comments[3])
[1] "too slow  -  not fast enough  -  bad speed"


但是从自动化的角度来看,我将如何使用不同数量的注释来做到这一点。

对不起,我想问一个新手问题,但这使我在一个下午的大部分时间里受了挫折。

编辑:根据要求在此处dput(head(clean_data, 10))更改名称和问题

ture(list(res_qnumber = 1:10, res_ID = c(44024431L, 44024431L,
44024431L, 44024431L, 44024431L, 44024431L, 44024431L, 44024431L,
44024431L, 44024431L), res_name = c("name1", "name1",
"name1", "name1", "name1", "name1", "name1",
"name1", "name1", "name1"), res_pos = c("NA",
"NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA", "NA"), res_ceo = c(FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
), res_qtype = c("standard", "standard", "standard", "standard",
"standard", "standard", "standard", "standard", "standard", "standard"
), res_qtext = c("Question1",
"Question2",
"Question3",
"Question4",
"Question5",
"Question6",
"Question7",
"Question8",
"Question9"
), res_response = c("2", "5", "5", "5", "5", "5", "5", "5", "5",
"5"), res_comment = c("too slow", NA_character_, NA_character_,
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,
NA_character_, NA_character_), res_scale = c("scale1", "scale2",
"scale3", "scale4", "scale5", "scale6", "scale7", "scale8", "scale9",
"scale10")), .Names = c("res_qnumber", "res_ID", "res_name",
"res_pos", "res_ceo", "res_qtype", "res_qtext", "res_response",
"res_comment", "res_scale"), row.names = c(NA, 10L), class = "data.frame")

最佳答案

paste(current_comments, collapse=" - ")

关于r - R编程:自动合并字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3920704/

10-11 02:56
查看更多