string 连接字符串/字符的向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2098368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate a vector of strings/character
提问by Nick
If I have a vector of type character, how can I concatenate the values into string? Here's how I would do it with paste():
如果我有一个字符类型的向量,如何将这些值连接成字符串?这是我将如何使用paste():
sdata = c('a', 'b', 'c')
paste(sdata[1], sdata[2], sdata[3], sep ='')
yielding "abc"
.
屈服"abc"
。
But of course, that only works if I know the length of sdataahead of time.
但是当然,这只有在我提前知道sdata的长度时才有效。
回答by Matt Turner
Try using an empty collapseargument within the paste function:
尝试在 paste 函数中使用空的折叠参数:
paste(sdata, collapse = '')
paste(sdata, collapse = '')
回答by Ken Williams
Matt's answer is definitely the right answer. However, here's an alternative solution for comic relief purposes:
马特的回答绝对是正确的答案。但是,这是用于喜剧救济目的的替代解决方案:
do.call(paste, c(as.list(sdata), sep = ""))
回答by bartektartanus
You can use stri_paste
function with collapse
parameter from stringi
package like this:
您可以使用stri_paste
带有包中collapse
参数的函数,stringi
如下所示:
stri_paste(letters, collapse='')
## [1] "abcdefghijklmnopqrstuvwxyz"
And some benchmarks:
以及一些基准:
require(microbenchmark)
test <- stri_rand_lipsum(100)
microbenchmark(stri_paste(test, collapse=''), paste(test,collapse=''), do.call(paste, c(as.list(test), sep="")))
Unit: microseconds
expr min lq mean median uq max neval
stri_paste(test, collapse = "") 137.477 139.6040 155.8157 148.5810 163.5375 226.171 100
paste(test, collapse = "") 404.139 406.4100 446.0270 432.3250 442.9825 723.793 100
do.call(paste, c(as.list(test), sep = "")) 216.937 226.0265 251.6779 237.3945 264.8935 405.989 100
回答by Patrick
For sdata
:
对于sdata
:
gsub(", ","",toString(sdata))
For a vector of integers:
对于整数向量:
gsub(", ","",toString(c(1:10)))
回答by C8H10N4O2
Matt Turner's answer is definitely the right answer. However, in the spirit of Ken Williams' answer, you could also do:
马特特纳的答案绝对是正确的答案。但是,本着 Ken Williams 回答的精神,您也可以这样做:
capture.output(cat(sdata, sep=""))
回答by fan
Here is a little utility function that collapses a named or unnamed list of values to a single string for easier printing. It will also print the code line itself. It's from my list examples in Rpage.
这是一个小实用函数,它将命名或未命名的值列表折叠为单个字符串,以便于打印。它还将打印代码行本身。它来自我在 R页面中的列表示例。
Generate some lists named or unnamed:
生成一些命名或未命名的列表:
# Define Lists
ls_num <- list(1,2,3)
ls_str <- list('1','2','3')
ls_num_str <- list(1,2,'3')
# Named Lists
ar_st_names <- c('e1','e2','e3')
ls_num_str_named <- ls_num_str
names(ls_num_str_named) <- ar_st_names
# Add Element to Named List
ls_num_str_named$e4 <- 'this is added'
Here is the a function that will convert named or unnamed list to string:
这是一个将命名或未命名列表转换为字符串的函数:
ffi_lst2str <- function(ls_list, st_desc, bl_print=TRUE) {
# string desc
if(missing(st_desc)){
st_desc <- deparse(substitute(ls_list))
}
# create string
st_string_from_list = paste0(paste0(st_desc, ':'),
paste(names(ls_list), ls_list, sep="=", collapse=";" ))
if (bl_print){
print(st_string_from_list)
}
}
Testing the function with the lists created prior:
使用之前创建的列表测试函数:
> ffi_lst2str(ls_num)
[1] "ls_num:=1;=2;=3"
> ffi_lst2str(ls_str)
[1] "ls_str:=1;=2;=3"
> ffi_lst2str(ls_num_str)
[1] "ls_num_str:=1;=2;=3"
> ffi_lst2str(ls_num_str_named)
[1] "ls_num_str_named:e1=1;e2=2;e3=3;e4=this is added"
Testing the function with subset of list elements:
使用列表元素的子集测试函数:
> ffi_lst2str(ls_num_str_named[c('e2','e3','e4')])
[1] "ls_num_str_named[c(\"e2\", \"e3\", \"e4\")]:e2=2;e3=3;e4=this is added"
> ffi_lst2str(ls_num[2:3])
[1] "ls_num[2:3]:=2;=3"
> ffi_lst2str(ls_str[2:3])
[1] "ls_str[2:3]:=2;=3"
> ffi_lst2str(ls_num_str[2:4])
[1] "ls_num_str[2:4]:=2;=3;=NULL"
> ffi_lst2str(ls_num_str_named[c('e2','e3','e4')])
[1] "ls_num_str_named[c(\"e2\", \"e3\", \"e4\")]:e2=2;e3=3;e4=this is added"