How to Paste a String on Each Element of a Vector of Strings Using Apply in R

How to paste a string on each element of a vector of strings using apply in R?

No need for apply(), just use paste():

R> d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun")
R> week <- paste(d, "day", sep="")
R> week
[1] "Monday" "Tuesday" "Wednesday" "Thursday"
[4] "Friday" "Saturday" "Sunday"
R>

Concatenate a vector of strings/character

Try using an empty collapse argument within the paste function:

paste(sdata, collapse = '')

Thanks to http://twitter.com/onelinetips/status/7491806343

How to concatenate a string to each element of a possible empty character vector?

OUTDATED (still valid for R <4.0.1):


While writing this question, I found an almost satisfying answer:

new_people <- c(" R. A. Becker", "J. M. Chambers", "A. R. Wilks")
sprintf("Hello %s!", new_people)
# [1] "Hello R. A. Becker!" "Hello J. M. Chambers!" "Hello A. R. Wilks!"

new_people <- character()
sprintf("Hello %s!", new_people)
# character(0)

This, however, lacks the nice linkage between the variables and their position in the string that paste has and requires an additional surrounding paste(..., collapse="") if one wants to smush the strings together.

Pasting a string on every other element of a vector

How about

paste0(c("a","b","c","d"), c("", "_2"))

[1] "a" "b_2" "c" "d_2"

Is there a way to paste together the elements of a vector in R without using a loop?

You just need to use the collapse argument:

paste(x,collapse="")

Adding % to values in a column in R

In base R:

df$`Percent Completed` <- paste(df$`Percent Completed`, "%", sep = "")

Result:

df
# A tibble: 7 x 5
# Groups: Mentor Name [7]
`Mentor Name` Completed `In Progress` `Not Started` `Percent Completed`
<chr> <int> <int> <int> <chr>
1 Betsy 6 11 18 17%
2 Feliciano 8 11 16 23%
3 Gloria 5 15 16 14%
4 Linda 5 12 19 14%
5 Shannon 9 5 22 25%
6 Shaune 4 16 15 11%
7 Sheryl 5 12 19 14%

concatenate vector of strings into a single string - for each row in df

You are close to the right code, just add collapse and work on rows with margin=1:

apply(data, 1, paste,collapse=" ")
[1] "abc fghi m" " j " "de kl "

from documentation

collapse an optional character string to separate the results.

To integrate the output in your dataset:

data$pasted<-apply(data, 1, paste,collapse=" ")
> data
x1 x2 x3 pasted
1 abc fghi m abc fghi m
2 j j
3 de kl de kl

Apply paste over a list of vectors to get a list of strings

Your initial approach was almost correct, you just need to add collapse = " " in order to concatenate the vectors into one string in each element of your list

lapply(data, paste, collapse = " ") 
# $foo
# [1] "first m last"
#
# $bar
# [1] "first m last"

Collapse character vector into single string with each string on its own row

There is no way to create an object that stores data in the way that you want except when you parse the \n characters. Whitespace in strings are meaningful, i.e. spaces will be preserved when a string is printed. However, how those whitespaces appear in your console will vary for a number of reasons. As it is now, your string already has the characters to tell other functions that will process it to print each substring on its own line.

text <- c("This should be first row",
"This should be second row",
"This should be third row")

# This is what you did
# Notice that you can't assign to an object and it prints to console!
test <- cat(paste(text, collapse = "\n"))
#> This should be first row
#> This should be second row
#> This should be third row

print(test)
#> NULL

# This is what you want: sending the object itself
test2 <- paste(text, collapse = "\n")

print(test2)
#> [1] "This should be first row\nThis should be second row\nThis should be third row"

How do I apply this for loop over my character vector and output another vector?

The out is not initialized in the function

concat <- function(vector,SID){
out <- character(length(vector))

decrement_append <- "&decrementQuotas=true"
SID_append <- "?surveyId="

for(i in 1:length(vector)){
out[i] <- paste0(v[i],SID_append,SID,decrement_append)
}
out
}

-testing

> concat(v, "SV_55tYjKDRKYTRNIh")
[1] "R_2wmKOSbPWHl4VtT2?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_2TtslLEVNeHs2r73?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[3] "R_ZF79IJ60LaxxsuR4?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_3JJDUkrZ07eIwnh5?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[5] "R_3JrWuv9fsLK6qNx6?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"

paste/paste0 are vectorized. So, looping is not really needed

concat2 <- function(vector,SID){


decrement_append <- "&decrementQuotas=true"
SID_append <- "?surveyId="


paste0(v, SID_append,SID,decrement_append)


}

-testing

> concat2(v, "SV_55tYjKDRKYTRNIh")
[1] "R_2wmKOSbPWHl4VtT2?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_2TtslLEVNeHs2r73?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[3] "R_ZF79IJ60LaxxsuR4?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true" "R_3JJDUkrZ07eIwnh5?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"
[5] "R_3JrWuv9fsLK6qNx6?surveyId=SV_55tYjKDRKYTRNIh&decrementQuotas=true"


Related Topics



Leave a reply



Submit