Getting strings recognized as variable names in R

后端 未结 6 1731
我在风中等你
我在风中等你 2020-11-28 18:39

Related: Strings as variable references in R
Possibly related: Concatenate expressions to subset a dataframe


I\'ve simplified the question per the comment

相关标签:
6条回答
  • 2020-11-28 19:03

    If you want to use a string as a variable name, you can use assign:

    var1="string_name"
    
    assign(var1, c(5,4,5,6,7))
    
    string_name 
    
    [1] 5 4 5 6 7
    
    0 讨论(0)
  • 2020-11-28 19:03

    What works best for me is using quote() and eval() together.

    For example, let's print each column using a for loop:

    Columns <- names(dat)
    for (i in 1:ncol(dat)){
      dat[, eval(quote(Columns[i]))] %>% print
    }
    
    0 讨论(0)
  • 2020-11-28 19:06

    The basic answer to the question in the title is eval(as.symbol(variable_name_as_string)) as Josh O'Brien uses. e.g.

    var.name = "x"
    assign(var.name, 5)
    eval(as.symbol(var.name)) # outputs 5
    

    Or more simply:

    get(var.name) # 5
    
    0 讨论(0)
  • 2020-11-28 19:06

    Subsetting the data and combining them back is unnecessary. So are loops since those operations are vectorized. From your previous edit, I'm guessing you are doing all of this to make bubble plots. If that is correct, perhaps the example below will help you. If this is way off, I can just delete the answer.

    library(ggplot2)
    # let's look at the included dataset named trees.
    # ?trees for a description
    data(trees)
    ggplot(trees,aes(Height,Volume)) + geom_point(aes(size=Girth))
    # Great, now how do we color the bubbles by groups?
    # For this example, I'll divide Volume into three groups: lo, med, high
    trees$set[trees$Volume<=22.7]="lo"
    trees$set[trees$Volume>22.7 & trees$Volume<=45.4]="med"
    trees$set[trees$Volume>45.4]="high"
    
    ggplot(trees,aes(Height,Volume,colour=set)) + geom_point(aes(size=Girth))
    
    
    # Instead of just circles scaled by Girth, let's also change the symbol
    ggplot(trees,aes(Height,Volume,colour=set)) + geom_point(aes(size=Girth,pch=set))
    
    # Now let's choose a specific symbol for each set. Full list of symbols at ?pch
    trees$symbol[trees$Volume<=22.7]=1
    trees$symbol[trees$Volume>22.7 & trees$Volume<=45.4]=2
    trees$symbol[trees$Volume>45.4]=3
    
    ggplot(trees,aes(Height,Volume,colour=set)) + geom_point(aes(size=Girth,pch=symbol))
    
    0 讨论(0)
  • 2020-11-28 19:08

    You found one answer, i.e. eval(parse()) . You can also investigate do.call() which is often simpler to implement. Keep in mind the useful as.name() tool as well, for converting strings to variable names.

    0 讨论(0)
  • 2020-11-28 19:15

    Without any example data, it really is difficult to know exactly what you are wanting. For instance, I can't at all divine what your object set (or is it sets) looks like.

    That said, does the following help at all?

    set1 <- data.frame(x = 4:6, y = 6:4, z = c(1, 3, 5))
    
    plot(1:10, type="n")
    XX <- "set1"
    with(eval(as.symbol(XX)), symbols(x, y, circles = z, add=TRUE))
    

    EDIT:

    Now that I see your real task, here is a one-liner that'll do everything you want without requiring any for() loops:

    with(dat, symbols(sq, cu, circles = num,
                      bg = c("red", "blue")[(num>5) + 1]))
    

    The one bit of code that may feel odd is the bit specifying the background color. Try out these two lines to see how it works:

    c(TRUE, FALSE) + 1
    # [1] 2 1
    c("red", "blue")[c(F, F, T, T) + 1]
    # [1] "red"  "red"  "blue" "blue"
    
    0 讨论(0)
提交回复
热议问题