我正在处理一个由可变数量的引文键组成的列表对象。
mylist <- structure(list(steele1998pulsus = "steele1998pulsus", wright1997evaluation = "wright1997evaluation", wright1996continuous = "wright1996continuous"), .Names = c("steele1998pulsus", "wright1997evaluation", "wright1996continuous"))
paste0("@", unlist(mylist))
产生
[1] "@steele1998pulsus" "@wright1997evaluation" "@wright1996continuous"
我尝试了cat()和paste()的各种组合但没有成功 - 我的目标是一个格式如下的文本字符串(多个键的pandoc引用)。
[@steele1998pulsus; @wright1997evaluation; @wright1996continuous]
答案 0 :(得分:1)
我对R的字符串处理命令不太满意(我发现很难记住collapse
和sep
之间的区别)但我可以这样做:
paste("[", paste(paste0("@", mylist), collapse="; "), "]", sep="")
产生所需的输出:
> paste("[", paste(paste0("@", mylist), collapse="; "), "]", sep="")
[1] "[@steele1998pulsus; @wright1997evaluation; @wright1996continuous]"
另外,因为对于我们的任何步骤,我们都不需要sep
参数
可以只对paste0
使用{<1}}:
paste0("[", paste0(paste0("@", mylist), collapse="; "), "]")