R编程:自动化字符串的合并

时间:2010-10-13 04:09:09

标签: r string automation character

我正在尝试自动化这里的一些系统,特别是根据调查数据生成报告。

假设我对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")

1 个答案:

答案 0 :(得分:7)

paste(current_comments, collapse=" - ")