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="")
R - add same element to vector without use for loop
From the above comments we had found some useful answers where every new OP can view in aswer window rather than comment sections, thanks OP for your valuable answers
v <- c(v, rep(NA, 4)) # joel.wilson
length(v)<-length(v)+4 # Nicola
'length<-'(v, 8) # akrun
Please note:
in general the Joel.Wilson's option is the good one 'cause can be used to append several times a specific value (numeric, character, boolean, etc.), while other two solutions only NA values as they play on the length property.
How to concatenate vectors of different lengths without recycling and without using a loop?
Find the length of the longest vector and set the length of each vector to that, then concatenate.
v1 <- c("A", "B", "C", "D", "E")
v2 <- c("F", "G", "H")
n <- max(length(v1), length(v2))
length(v1) <- n
length(v2) <- n
paste(v1, v2, sep = "-")
#> [1] "A-F" "B-G" "C-H" "D-NA" "E-NA"
Created on 2021-09-26 by the reprex package (v2.0.1)
using paste to collapse multiple vectors
You just need option sep=
.
paste(a, b, sep=" + ")
# [1] "one + two + 3 + 4"
How to include a vector with multiple elements in a paste() statement?
Concatenate Strings
Your approach will work For inputs of length 1
but you have a vector.
Because
error.list
is avector
, use one of the following methods:
error.list <- c("a", "b", "c")
paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = " ")
Provide the output:
[1] "Hi, Your data has the following errors:" "a"
[3] "b" "c"
[5] " Regards, XYZ"
One line use parameter collapse =
:
paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = " ", collapse = " ")
Provide the output:
"Hi, Your data has the following errors: a b c Regards, XYZ"
or you can use
paste0()
with parametercollapse =
to get one line output, for also using theerror.list
as vector:
paste0(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), collapse = " ")
Provide the output:
[1] "Hi, Your data has the following errors: a b c Regards, XYZ"
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>
How to generate a sequential character vector without a loop?
Here is another way to with rep
and lapply
.
seq_fun <- function(n, m){
unlist(lapply(1:n, function(x) c(rep("", times = m), x)))
}
seq_fun(3, 2)
# [1] "" "" "1" "" "" "2" "" "" "3"
Pasting two vectors with combinations of all vectors' elements
You can use this, but there may be a simpler solution :
R> apply(expand.grid(vars, vis), 1, paste, collapse=".")
[1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"
expand.grid
gives back a data.frame
which when used with apply
, apply
will convert it to a matrix
. This is just unnecessary (and inefficient on large data). outer
gives a matrix
and also takes function argument. It'll be much efficient on huge data as well.
Using outer
:
as.vector(outer(vars, vis, paste, sep="."))
# [1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"
Related Topics
Knitr: Include Figures in Report *And* Output Figures to Separate Files
How to Add a Page Break in Word Document Generated by Rstudio & Markdown
Time Series Plot with X Axis in "Year"-"Month" in R
Extract the Coefficients for the Best Tuning Parameters of a Glmnet Model in Caret
Ggpairs Plot with Heatmap of Correlation Values
Regression (Logistic) in R: Finding X Value (Predictor) for a Particular Y Value (Outcome)
R Xml - Combining Parent and Child Nodes into Data Frame
Partially Read Really Large CSV.Gz in R Using Vroom
Split Character Columns and Get Names of Field in String
Filter a Vector of Strings Based on String Matching
Add Regression Plane to 3D Scatter Plot in Plotly
Add Text on Right of Shinydashboard Header
Obtaining Connected Components of Neighboring Values
Keeping Only Certain Rows of a Data Frame Based on a Set of Values