Home > front end >  Combining multiple values (character class) in R using c()
Combining multiple values (character class) in R using c()

Time:12-07

I was doing bioinformatics analysis when I needed to combine a set of values The values have similar names like pruned_11, pruned_12, and each of them consists of several characters.

The values look like this: The values look like this

I have tried combine = c(list = ls(pattern = "^pruned_"))but it returned a value with only the "pruned_" names in it.

I could also use combine = c(pruned_12, pruned_13, pruned_14, pruned_15...)to get what I needed, but it would take too much time if I had to type all 23 chromosomes for every single analysis. Is there any way I could conbine them without typing them each? Many thanks!

CodePudding user response:

Use mget to get a list of variables from their names

combine = mget(ls(pattern = "^pruned_"))

CodePudding user response:

You're on the right track with ls(). Try this:

combine <- c(sapply(ls(pattern = "^pruned_"), function(name) eval(parse(text = name))))
  •  Tags:  
  • r
  • Related