Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert R vector to string vector of 1 element [duplicate]

Im working with the programming language R now. I have a vector:

a <- c("aa", "bb", "cc")

And I want to paste these to a system command, I'm trying it this way now:

args <- paste(a, sep=" ")
system(paste("command",args, sep=" "))

But now I'm only getting the arguments aa, and I want the arguments aa, bb and cc...

Anyone knows what I'm doing wrong?

like image 492
Jetse Avatar asked Dec 20 '12 13:12

Jetse


People also ask

How do I collapse a vector to a string in R?

To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.

How do you make a repeated value vector in R?

There are two methods to create a vector with repeated values in R but both of them have different approaches, first one is by repeating each element of the vector and the second repeats the elements by a specified number of times. Both of these methods use rep function to create the vectors.

How do you make a vector of 1 in R?

Using rep() function It creates a vector with the value x repeated t times. To create a vector of ones, pass 1 as the first argument to the rep() function and the length of the vector as its second argument.

How do you split a character vector in R?

Note that splitting into single characters can be done via split = character(0) or split = "" ; the two are equivalent.


1 Answers

Use the collapse argument to paste:

paste(a,collapse=" ")
[1] "aa bb cc"
like image 189
James Avatar answered Oct 10 '22 08:10

James