R paste values of list for every entry in the Data Frame
Base R using sapply
:
politicans_v2$mentions_chr <- sapply(politicans_v2$mentions, paste0, collapse = ' ')
how to create a list with the names generated by paste function?
Use setNames
like this:
setNames(list(1:4), paste("rer","erer"))
## $`rer erer`
## [1] 1 2 3 4
or
`names<-`(list(1:4), paste("rer","erer"))
## $`rer erer`
## [1] 1 2 3 4
or
L <- list(1:4)
names(L) <- paste("rer","erer")
L
## $`rer erer`
## [1] 1 2 3 4
R Paste List to Bind
We can use mget
on the paste
d strings to return the values of the object names in a list
and then rbind
the elements with do.call
`row.names<-`(do.call(rbind, mget(paste0('data', 1:4))), NULL)
Or use pattern
in ls
do.call(rbind, mget(ls(pattern = '^data\\d+$')))
With data.table
, it would be rbindlist
library(data.table)
rbindlist(mget(paste0('data', 1:4)))
Paste items in a list where some of the items contain a vector
You need to repeat the shorter vectors to be the same length as the longer vectors. One way to do this is to convert the list to a data.frame, then you can apply
your paste
function to each row:
myfun <- function(x) paste(names(x), "=", x, collapse="&", sep="")
apply(as.data.frame(paramlist2), 1, myfun)
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="")
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"
Related Topics
Solve Homogenous System Ax = 0 for Any M * N Matrix a in R (Find Null Space Basis for A)
Do I Need to Reshape This Wide Data to Effectively Use Ggplot2
Plotly - Different Colours for Different Surfaces
Calculate Difference Between Dates by Group in R
Data.Table: Sum by All Existing Combinations in Table
Select a Sequence of Columns: ':' Works But Not 'Seq'
Making Sure a Function Does Not Use a Global Variable
Merge Multiple Data.Frames in R with Varying Row Length
R Programming: Read.Csv() Skips Lines Unexpectedly
Error: Package or Namespace Load Failed for 'Car'
Error When Mapping in Ggmap with API Key (403 Forbidden)
How to Substitute Symbols in a Language Object
R: How to Match/Join 2 Matrices of Different Dimensions (Nrow/Ncol)
Out of Order Text Labels on Stack Bar Plot (Ggplot)
Why Isn't the R Function Sink() Writing a Summary Output to My Results File