[R] Vector of symbols?

Boris Steipe boris.steipe at utoronto.ca
Sat Oct 31 19:36:52 CET 2015


You are describing an awkward way of doing this (and your example is unreadable because you are not following posting instructions for the list) ... but the following contains the essence of what I think is needed: parse() and eval().

v <- character()

v[1] <- sprintf("%d ^ %s", 2, "duck")
v[2] <- sprintf("%d * %s", 4, "crow")
# [1] "2 ^ duck" "4 * crow"

duck <- -2
crow <- pi

eval(parse(text=v[1]))
# [1] 0.25

eval(parse(text=v[2]))
# 12.56637


However, myself, I would  not use symbols, but a named vector of values - unless it's important to you that these are _actually_ symbols. You probably avoid some conversions from and to symbols.

v1 <- c(2, 4)
v2 <- c(duck = -2, crow = pi)

v1[1] * v2["duck"]
v1[2] * v2["crow"]

You can of course freely interconvert and cast your symbol names to character:
duck <- 100
v2[as.character(quote(duck))] <- duck
v1[1] * v2["duck"]
#[1] 200



B.




On Oct 31, 2015, at 11:52 AM, Judson <judsonblake at msn.com> wrote:

> 
> 
> Is
> there a way to multiply a matrix 
> 
> times
> a vector of symbols?  
> 
> For
> instance, could I do this: 
> 
> 1       
> 0       0                          a
> 
> 0        1      
> 0      times           b
> 
> -1       2      
> 1                          c                
> 
> 
> 
> which
> should result in :  
> 
>                                                      a
> 
>                                                      b
> 
>                                                      c  �a 
> -2*b 
> where a, b, and c 
> 
> are
> as yet not assigned numeric values?  
> 
> ........................... judson blake
> 
> 
> 		 	   		  
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list