- [R编程:字符串的合并自动化 [英] R Programming: Automating Merge of Character Strings
问题描述
我想在这里工作特别是自动执行某些系统的基于调查数据生成报表做的。
可以说我有1问题3的评论。
current_comments< - C(太慢,不够快,速度差)
基本上我想要做的是注释合并到相隔一个字符串中 - 太像这样
> current_comments
[1]太慢了 - 还不够快 - 坏的速度
,这样我可以把它粘成一个单元出口。
我知道我可以通过粘贴功能做到这一点。
>糊(current_comments [1], - ,current_comments [2], - ,current_comments [3])
[1]太慢了 - 还不够快 - 坏的速度
但是,从自动化的角度来看如何将我做了不同数量的意见。
对不起,一个新手的问题,但是这已经把我难倒了一下午的大部分时间。
编辑:根据要求继承人 dput(头(clean_data,10))
的名称,改变了问题。
TURE(列表(res_qnumber = 1:10,RES_ID = C(44024431L,44024431L,
44024431L,44024431L,44024431L,44024431L,44024431L,44024431L,
44024431L,44024431L),RES_NAME = C(名称1,名称1
名1,名1,名1,名1,名称1
名1,名1,名1),res_pos = C(NA,
NA,不适用,不适用,不适用,不适用,不适用,不适用,不适用,不适用),res_ceo = C(FALSE,
FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE
),res_qtype = C(标准,标准,标准,标准,
标准,标准,标准,标准,标准,标准
),res_qtext = C(问题1
问题2
问题3
问题4,
Question5
Question6
Question7
Question8
Question9
),res_response = C(2,5,5,5,5,5,5,5,5,
5),res_comment = C(太慢,NA_character_,NA_character_,
NA_character_,NA_character_,NA_character_,NA_character_,NA_character_,
NA_character_,NA_character_),res_scale = C(量程1,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)
粘贴(current_comments,塌陷= - )
I'm trying to automate some of the systems at work here specifically to do with report generation based on survey data.
Lets say i have 3 comments for 1 question.
current_comments <- c("too slow", "not fast enough", "bad speed")
Basically what i want to do is merge the comments into one string separated by a "-" too look like this
>current_comments
[1] "too slow - not fast enough - bad speed"
So that i can stick it into one cell for exporting.
I know that i can do this by using the paste function.
> paste(current_comments[1], " - ", current_comments[2], " - ", current_comments[3])
[1] "too slow - not fast enough - bad speed"
But from an automation standpoint how would i do this with a varying number of comments.
Sorry for a newbie question but this has had me stumped for the better part of a afternoon.
edit: as requested heres dput(head(clean_data, 10))
with the names and the questions changed
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编程:字符串的合并自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!