jrzelling jrzelling - 1 year ago 74
R Question

Merge two lists into single list containing single character vectors R

Okay I'm stumped, I know there are answers about merging lists, and my attempt builds on those answers, but they don't return a single char vector. I have a function that merges lists but the values are separate character vectors:

I dont want the characters as separate strings

csc.list <- mapply(c, rep("CSC", 16), c(1:16), SIMPLIFY=FALSE)

[1] "CSC" "1"
[1] "CSC" "2"

I don't know how to combine the characters in rows with a wierd heading

csc.list <- mapply(unlist, c(mapply(c, rep("CSC", 16), c(1:16), SIMPLIFY=FALSE)))

[1,] "CSC" "CSC" "CSC" "CSC" "CSC" ...
[2,] "1" "2" "3" "4" "5" ...

Desired Result of two merged lists

c("CSC 1", "CSC 2", "CSC 3", "CSC 4", "CSC 5", ... , "CSC 16")

[1] "CSC 1" "CSC 2" "CSC 3" "CSC 4" "CSC 5" ... "CSC 16"

Bonus if your answer scales to merging more than two, i.e.
lists into single vector of merged characters:

csc.list <- mapply(c, rep("CSC", 16), c(1:16), rep(".R", 16), SIMPLIFY=FALSE)
lalalala <- f(csc.list)

Desired result of three merged lists

[1] "CSC 1.R" "CSC 2.R" ...


Answer Source

Are you looking for something like this?:

csc.list <- mapply(c, rep("CSC", 16), c(1:16), SIMPLIFY=FALSE)

#merge the list into a string
output<-sapply(csc.list, toString)
#remove the added commas
output<-gsub(",", "", output)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download