How to make a vector with string elements without space

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How to make a vector with string elements without space

Vahid
Hello my r friends,
I want to make a vector with elements (c1,c2,...,c10).
I wrote the below code:
c(paste("c",1:10))
My code works but it gives me elements like "c 1", "c 2" to "c 10". I mean
there is a space between each c and its corresponding number. I want the
elements of the vector to be like "c1", "c2", to "c10"  (without space
between c and number). How can I fix this problem in my code?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to make a vector with string elements without space

Boris Steipe
The c() is unnecessary. paste() returns a vector.

Paste separates elements with " " by default. Set the separator to "" instead.
paste("c",1:10, sep = "")

... or use paste0(), which has "" as default separator.
paste0("c",1:10)

?paste  is your friend.


B.




> On 2020-05-23, at 22:25, Vahid Borji <[hidden email]> wrote:
>
> Hello my r friends,
> I want to make a vector with elements (c1,c2,...,c10).
> I wrote the below code:
> c(paste("c",1:10))
> My code works but it gives me elements like "c 1", "c 2" to "c 10". I mean
> there is a space between each c and its corresponding number. I want the
> elements of the vector to be like "c1", "c2", to "c10"  (without space
> between c and number). How can I fix this problem in my code?
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: How to make a vector with string elements without space

glsnow
In reply to this post by Vahid
As Borris mentioned, paste0 works well for this.

Another option is the sprintf function:

sprintf("c%i", 1:10)

For this example they do the same thing, but as things become more
complicated sometimes you will want paste0 and sometimes sprintf will
be better.

Compare the above to
sprintf("c%02i", 1:10)

Also, you do not say why you want to do this, but one possible reason
is that you have a set of variables in the global workspace named "c1"
through "c10" that you want to loop through.  If this is the case,
then there are better ways.  You can put the variables into a list,
then use lapply, sapply, or the purrr package to loop through them in
much  better ways.

On Sat, May 23, 2020 at 6:26 AM Vahid Borji <[hidden email]> wrote:

>
> Hello my r friends,
> I want to make a vector with elements (c1,c2,...,c10).
> I wrote the below code:
> c(paste("c",1:10))
> My code works but it gives me elements like "c 1", "c 2" to "c 10". I mean
> there is a space between each c and its corresponding number. I want the
> elements of the vector to be like "c1", "c2", to "c10"  (without space
> between c and number). How can I fix this problem in my code?
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.