How would one go about getting sprintf to use the values of a vector without having to specify each argument individually?> v <- c(1, 2, -1.197114, 0.1596687) > iv <- c(3, 1, 2, 4) > sprintf("%9.2f\t%d\t%d\t%8.3f", v[3], v[1], v[2], v[4])[1] " -1.20\t1\t2\t 0.160" Essentially, desired effect would be something like:> sprintf("%9.2f\t%d\t%d\t%8.3f", v[iv]) # wish it worked---------------------------------------------------------- SIGSIG -- signature too long (core dumped)
Try this: do.call(sprintf, c("%9.2f\t%d\t%d\t%8.3f", as.list(v[iv]))) On 5/3/06, Paul Roebuck <roebuck at mdanderson.org> wrote:> How would one go about getting sprintf to use the > values of a vector without having to specify each > argument individually? > > > v <- c(1, 2, -1.197114, 0.1596687) > > iv <- c(3, 1, 2, 4) > > sprintf("%9.2f\t%d\t%d\t%8.3f", v[3], v[1], v[2], v[4]) > [1] " -1.20\t1\t2\t 0.160" > > Essentially, desired effect would be something like: > > sprintf("%9.2f\t%d\t%d\t%8.3f", v[iv]) # wish it worked > > ---------------------------------------------------------- > SIGSIG -- signature too long (core dumped) > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
You could finagle things with do.call (maybe right your own function that does this if you will use it often). Here is your example:> v <- c(1, 2, -1.197114, 0.1596687) > iv <- c(3, 1, 2, 4) > tmp <- c(list("%9.2f\t%d\t%d\t%8.3f"),as.list(v[iv])) > do.call('sprintf',tmp)[1] " -1.20\t1\t2\t 0.160" Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Paul Roebuck Sent: Wednesday, May 03, 2006 10:32 AM To: R Help Mailing List Subject: [R] sprintf question How would one go about getting sprintf to use the values of a vector without having to specify each argument individually?> v <- c(1, 2, -1.197114, 0.1596687) > iv <- c(3, 1, 2, 4) > sprintf("%9.2f\t%d\t%d\t%8.3f", v[3], v[1], v[2], v[4])[1] " -1.20\t1\t2\t 0.160" Essentially, desired effect would be something like:> sprintf("%9.2f\t%d\t%d\t%8.3f", v[iv]) # wish it worked---------------------------------------------------------- SIGSIG -- signature too long (core dumped) ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On 5/3/2006 12:31 PM, Paul Roebuck wrote:> How would one go about getting sprintf to use the > values of a vector without having to specify each > argument individually? > >> v <- c(1, 2, -1.197114, 0.1596687) >> iv <- c(3, 1, 2, 4) >> sprintf("%9.2f\t%d\t%d\t%8.3f", v[3], v[1], v[2], v[4]) > [1] " -1.20\t1\t2\t 0.160" > > Essentially, desired effect would be something like: >> sprintf("%9.2f\t%d\t%d\t%8.3f", v[iv]) # wish it workedLike most R functions, the latter would be interpreted as a request for 4 strings corresponding to the 4 input values. What you want to do is probably easiest the way you did it, but it could also be done as do.call(sprintf, c("%9.2f\t%d\t%d\t%8.3f", as.list(v[iv]))) where the argument list to sprintf is being constructed programmatically. I'd certainly find the original version preferable if this were my code, but maybe the real situation is more complicated. Another possibility is to name the elements of v, then you can do things like v["foo"], v["bar"], etc. Duncan Murdoch