jrzelling - 1 year ago 68

R Question

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)`

$CSC

[1] "CSC" "1"

$CSC

[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)))`

CSC CSC CSC CSC CSC ...

[1,] "CSC" "CSC" "CSC" "CSC" "CSC" ...

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

`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.

`n`

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

lalalala <- f(csc.list)

`[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)
```