简体   繁体   English

如何在r中打印可变数量的参数?

[英]how to print a variable number of arguments in r?

new to r. R的新手。 this question has answers here and here . 这个问题在这里这里都有答案。

But these do not seem answer the question in the case of: 但是在以下情况下,这些似乎无法回答问题:

vec <- c("a","b","c","d")
s<-do.call(sprintf, c(list("%s %s"), vec))

The doc says: " ... The arguments (including fmt) are recycled if possible a whole number of times to the length of the longest, and then the formatting is done in parallel. ... " 文档说:“ ...如果可能,将参数(包括fmt)循环使用多次,直到最长的长度,然后并行进行格式化。...”

The code below shows that this is not happening: 下面的代码表明这没有发生:

> vec <- c("a","b","c","d")
> s<-do.call(sprintf, c(list("%s %s"), vec))
> print(s)
[1] "a b"
> v1 <- c("foo", "bar", "baz","foo", "bar", "baz")
> base_string = "%s, %s, %s"
> s<-do.call(sprintf, c(fmt = base_string, as.list(v1)))
> print(s)
[1] "foo, bar, baz"
> 

How do i make this print out all of the values? 我该如何打印出所有值?

Edit: according to @mt1022, i misread the doc. 编辑:根据@ mt1022,我误读了文档。 he suggests: sprintf(paste0(v1, collapse = ' ')) which works. 他建议:sprintf(paste0(v1,crash =''))有效。

Thanks to @chinsoon12 for the hint. 感谢@ chinsoon12的提示。

What I really want to do is something like this: 我真正想做的是这样的:

> s<-NA
> v<-c(1,"2",pi,2,"foo",2*pi)
> s<-do.call(sprintf, c(v, list(fmt=paste(rep("%d %s %f", length(v)/3), collapse=" "))))
Error in (function (fmt, ...)  : 
  invalid format '%d'; use format %s for character objects
> print(s)
[1] NA
>

The answer (thanks to @mt1022) is to use a list instead of a vector: 答案(感谢@ mt1022)是使用列表而不是向量:

v<-list(1,"2",pi,2,"foo",2*pi)
s<-do.call(sprintf, c(v, list(fmt=paste(rep("%d %s %f", length(v)/3), collapse=" "))))

The doc is right. 该文档是正确的。 In first case, do.call(sprintf, c(list("%s %s"), vec)) is equal to: 在第一种情况下, do.call(sprintf, c(list("%s %s"), vec))等于:

sprintf("%s %s", "a","b","c","d")

The fmt string "%s %s" requires two vectors while you provided four and the last two ("c", "d") were not used for printing. fmt字符串"%s %s"需要两个向量,而您提供了四个,而最后两个(“ c”,“ d”)未用于打印。

The second case is similar. 第二种情况相似。 do.call(sprintf, c(fmt = base_string, as.list(v1))) is equal to: do.call(sprintf, c(fmt = base_string, as.list(v1)))等于:

sprintf(fmt = "%s, %s, %s", "foo", "bar", "baz","foo", "bar", "baz")

Three variables were to be printed based on fmt but you provided six. 将基于fmt打印三个变量,但您提供了六个。


Then, what does "recycled" in the doc mean? 那么,文档中的“回收”是什么意思?

and I guess you might misunderstand it. 我想您可能会误解它。 It means when the formating string and vectors are of different lengths, the short er ones will be recycled to the longest one. 这意味着当格式化字符串和向量的长度不同时,较短的格式化字符串和向量将被回收到最长的一个。 An example: 一个例子:

> sprintf(c('%s %s', '%s, %s'), c('a', 'b', 'c'), 1:6)
[1] "a 1"  "b, 2" "c 3"  "a, 4" "b 5"  "c, 6"

How to print variable number of arguments : You can try paste : 如何打印可变数量的参数 :您可以尝试paste

> sprintf(paste0(vec, collapse = ' '))
[1] "a b c d"
> sprintf(paste0(v1, collapse = ', '))
[1] "foo, bar, baz, foo, bar, baz"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM