-5

I would like to know how to use stings stored in a variable/vector/matrix etc

in the middle of coding in order to edit names of variables/vectors or functions

e.g.

instead of creating the below

v_name1<- rep(5,10) v_name2<- rep(30,10) . . . function_name1<- { ... } function_name2<-{ ... } . . . 

using a for loop instead

I already know e.g. (this is just an example to illustrate)

 s<-c("name1","name2", ... ) for (i in 1:(length(s))) { eval(parse(text=paste("v_",s[1],"<-c(rep(i,10))", sep=""))) } v_name1 [1] 1 1 1 1 1 1 1 1 1 1 

I have two problems :

1) How do you make this for functions, I get the below error? e.g.

st<- "(x,y)<-{ 3*x + y}" eval(parse(text=paste("function_",s[1],st, sep=""))) > eval(parse(text=paste("function_",s[1],st, sep=""))) Error in eval(expr, envir, enclos) : object 'x' not found 

2) is there any faster/smarter way to do just that (substitute part of the coding with strings saved into another object) ?

I do not want to share what is my end goal or why I need to do this, I just want to know if the 2 things above are plausible.

13
  • 1
    Use lists - dataList <- split(data, data$country), why function names have to be "country" specific? Commented Jun 29, 2016 at 10:45
  • because in reality they are many countries (not just 3) and although I could name them like e.g. fun_country1, fun_country2 etc .. that means that whenever I or someone else would have to use the code they would need to refer to a reference table to see which country_number is which country etc when in a practical manner most of the people know the country codes. In a more general manner, the idea here for me is to see how I could use wherever in my coding strings saved in other variables. Commented Jun 29, 2016 at 10:52
  • 1
    I think you are over-complicating it. Say you have data object with Country column, at the beginning set a variable countyCode <- "UK", then work with subsetted data dataSubset <- data[ data$Country == countryCode, ], and the rest of your codes and functions should stay the same, working on dataSubset object only. Commented Jun 29, 2016 at 11:02
  • thank you very much for the answer and your time, I have already thought of what you are suggesting ... my point is bigger here, not just for this specific case. I wan to know in general how I could use stings saved somewhere (variable, vector etc) in coding on the fly, dynamically . Imagine of about 15 countries (which might change in the future) and giving my code to someone else and asking them to use a lot of functions and data and for each country to remember the naming (countrycode1, countrycode2 etc ... ) i.e. every person using the code would need to learn my personal country reference Commented Jun 29, 2016 at 11:12
  • 1
    Oh don't, I still don't understand why working on subsetted data option wouldn't work. And you didn't clarify why would you need function names having country names attached to it. Commented Jun 29, 2016 at 11:29

1 Answer 1

2

The very first comment told you to use lists. You can put anything into a list, data as well as functions:

set.seed(42) v <- list(uk = rnorm(10, mean = 5), us = rnorm(10, mean = 30), fr = rnorm(10, mean = 300)) funs <- list(uk = mean, us = median, fr = min) 

You can then do this:

country <- "us" funs[[country]](v[[country]]) 

R is not a language with macros. Work with the language and not against its design. And forget eval(parse()).

Sign up to request clarification or add additional context in comments.

10 Comments

You have not adequately described your problem if this dorsn't solve it. In fact, you are probably approaching the whole task in a wrong way.
I see your and zx8754's point for the list usage and it is a nice implementation . Yet still my problem is that e.g. in a months time I might get a new data frame with 15 other countries (or not even countries), so imagine having to go back to several lines of code in order to change each name. I do have an alternative way to implement what I need but running time will get 11 times slower. I want the code to work just by giving a different vector with the new names (countries,or whatever) so that the people using it (not programmers), know that function1_"new_name" does what supposed to.
It's quite simple. You are asking for something you do not need. I'm sure there is an easy way to solve your actual problem, which however you have not described sufficiently.
That doesn't change the answer. Just use lists.
What you don't seem to grasp: Your suggested approach is considered extremely bad practice. And for good reasons, among them that it is difficult to maintain and debug. There are better approaches, which are also quite easy. However, it's hard to show you without a representative example. Your problem seems to be that you are stuck at your bad approach and unable to consider better approaches.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.