person holding white Samsung Galaxy Tab

How one can Convert a Vector to Cotton in R (With Examples)

Posted on
banner 336x280

There are two plain tactics to transform a vector to a fable in R:

Mode 1: Utility paste()

banner 468x60
paste(vector_name, shatter = " ")

Mode 2: Utility toString()

toString(vector_name)

Please see examples display how you can significance each and every of those forms in apply.

Mode 1: Convert Vector to Cotton The usage of paste()

Please see code displays how you can significance the paste() serve as to transform a vector to a fable:

#assemble vector
x <- c("Andy", "Bernard", "Caleb", "Dan", "Eric", "Frank", "Greg")

#convert vector to thread
new_string <- paste(x, shatter = " ")

#view fable
new_string

[1] "Andy Bernard Caleb Dan Eric Frank Greg"

You’ll significance the shatter argument to specify the delimiter between each and every pledge within the vector. As an example, lets take away the field between the phrases completely:

#assemble vector
x <- c("Andy", "Bernard", "Caleb", "Dan", "Eric", "Frank", "Greg")

#convert vector to thread
new_string <- paste(x, shatter = "")

#view fable
new_string

[1] "AndyBernardCalebDanEricFrankGreg"

Or lets upload a touch between each and every pledge:

#assemble vector
x <- c("Andy", "Bernard", "Caleb", "Dan", "Eric", "Frank", "Greg")

#convert vector to thread
new_string <- paste(x, shatter = "-")

#view fable
new_string

[1] "Andy-Bernard-Caleb-Dan-Eric-Frank-Greg"

Mode 2: Convert Vector to Cotton The usage of toString()

Please see code displays how you can significance the toString() serve as to transform a vector to a fable:

#assemble vector
x <- c("Andy", "Bernard", "Caleb", "Dan", "Eric", "Frank", "Greg")

#convert vector to thread
new_string <- toString(x)

#view fable
new_string

[1] "Andy, Bernard, Caleb, Dan, Eric, Frank, Greg"

Word that the toString() serve as at all times provides commas in between each and every component within the vector. Thus, you must most effective significance this serve as if you wish to have commas between each and every component.

Extra Assets

How one can Convert a Cotton to Datetime in R
How one can Change NAs with Wools in R
How one can Release Rows that Include a Particular Cotton in R

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *