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
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
Concatenate unique combinations of elements in a character vector into new vector of strings in R
Try combn
> sprintf("_%s_", combn(vec, 2, paste0, collapse = "_"))
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_"
[8] "_X3_X4_" "_X3_X5_" "_X4_X5_"
> paste0("_", combn(vec, 2, paste0, collapse = "_"), "_")
[1] "_X1_X2_" "_X1_X3_" "_X1_X4_" "_X1_X5_" "_X2_X3_" "_X2_X4_" "_X2_X5_"
[8] "_X3_X4_" "_X3_X5_" "_X4_X5_"
Concatenate Strings to a Vector of Strings
From the last line of your question, may be you had a patttern like this in mind:
julia> string.("The font face is ", faces)
3-element Vector{String}:
"The font face is bold"
"The font face is ital"
"The font face is code"
Semantically, this and the .*
solution and the map solution are ultimately doing the same thing, but perhaps you have a preference for this syntax.
How can I concatenate a vector?
Try using:
> paste(blah, collapse = "")
[1] "p30s4p28s4"
or if you want the space in between:
> paste(blah, collapse = " ")
[1] "p30s4 p28s4"
Julia: how to concatenate characters of vector together ([a, b, c] - abc)
There are a variety of ways to concatenate a vector of strings:
join
functionstring
function*
concatenate function
as shown by the various comments.
However, the calling signatures for these functions are not identical. I did not notice this at first, and someone else new to Julia might appreciate the details.
julia> j = join(a)
"abc"
julia> s = string(a...)
"abc"
julia> m = *(a...)
"abc"
# When called correctly all three functions return equivalent results.
julia> j == s == m
true
However, when someone, like myself, is new to Julia, they might not immediately recognize (I didn't) the critical importance of ...
for the string
and *
string concatenate functions, in contrast to the join
function.
For example:
julia> s2 = string(a)
"[\"a\", \"b\", \"c\"]"
julia> s == s2
false
# or simply:
julia> join(a) == string(a)
false
What is the difference between s = join(a)
and s2 = string(a)
?
# Note that join(a) produces a string of 3 letters, "abc".
julia> length(s)
3
# string(a) produces a string of punctuation characters with the letters.
julia> length(s2)
15
julia> s[1]
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> s2[1]
'[': ASCII/Unicode U+005b (category Ps: Punctuation, open)
julia> s[1:3]
"abc"
julia> s2[1:3]
"[\"a"
The *()
concatenation function is also quite different from the join
function:
julia> a = ["a", "b", "c"]
3-element Array{String,1}:
"a"
"b"
"c"
julia> j = join(a)
"abc"
julia> m = *(a)
ERROR: MethodError: no method matching *(::Array{String,1})
julia> m = *(a...)
"abc"
Thus the "splat" operator, ...
, which is used to apply a function to a sequence of arguments is crucial to string
and *
, but not to join
.
In fact, the join
function with the "splat" operator does something you probably don't want:
julia> join(a...)
"a"
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.
concatenate strings in a character vector based on element names
We can concatenate with paste
by using grouping variables as the names
of the vector
'a'
tapply(a, names(a), FUN = paste, collapse = ' ')
# item 1 item 2
#"first_i1 second_i2" "only_i2"
Related Topics
Replacing Nas With Latest Non-Na Value
How to Import Multiple .Csv Files At Once
Filter Data.Frame Rows by a Logical Condition
Plotting Two Variables as Lines Using Ggplot2 on the Same Graph
Add Legend to Ggplot2 Line Plot
Calculate Group Mean, Sum, or Other Summary Stats. and Assign Column to Original Data
R Memory Management/Cannot Allocate Vector of Size N Mb
Split Data.Frame Based on Levels of a Factor into New Data.Frames
Add Regression Line Equation and R^2 on Graph
Understanding Exactly When a Data.Table Is a Reference to (Vs a Copy Of) Another Data.Table
Select Rows from a Data Frame Based on Values in a Vector
Create Stacked Barplot Where Each Stack Is Scaled to Sum to 100%
Looping Over a Date or Posixct Object Results in a Numeric Iterator
Specify Custom Date Format For Colclasses Argument in Read.Table/Read.Csv
How to Spread Repeated Measures of Multiple Variables into Wide Format