0

I have multiple dataframes which I created for every category name of a list (category.levels = c("Art","Music",...) - one dataframe for each category level. The dataframes are called Data_Art, Data_Music etc.

I now try to access those dataframes while looping in a for loop, and perform a merge for each one of them with some other general dataframe. My problem is that all my trials to get the real variable that is represented by the variable fail... for example I tried the following:

for (cat in category.levels)
{
  curr.dataset = eval(parse(text=paste("Data",cat,sep='_')))
  merged.data = merge(curr.dataset,other_data,by=c("user_id"))
 ... 
}

I also tried to user get() but still - in all my trials eventually the curr.dataset is a string variable and not the variable that the string represents... (I see in the environment viewer that it is a string, and I get error message for the merge operation). If I try the same outside the loop everything works fine. Is there a problem with using eval/get inside a loop?

989
  • 12,579
  • 5
  • 31
  • 53
user3017075
  • 351
  • 3
  • 16
  • http://stackoverflow.com/questions/1743698/evaluate-expression-given-as-a-string – xxfelixxx May 31 '16 at 10:00
  • Thanks @xxfelixxx but I did use eval(parse(text=.. so this might not be the same case as the question you referred to.. – user3017075 May 31 '16 at 10:05
  • Put these data.frames together in a list. You should never use `eval(parse())` (never ever, and for this you also should not use `get`). – Roland May 31 '16 at 10:24
  • Thanks @Roland! Do you have a suggestion how to do this and still keep the original category name ('Art','Music' etc.)? So that I'll be able to create new variables for the results, print and more..? – user3017075 May 31 '16 at 10:40
  • There is not enough information here. In fact, it is not even clear why you need separate data.frames and can't combine everything into one data.frame. – Roland May 31 '16 at 10:46

1 Answers1

0

Are you sure that your Data_ objects are dataframes? Your code works for me:

test_eval.R

user_id <- c(1001,1004,1007)
favorite_artist <- c("Monet","Van Gogh", "Picasso")
Data_Art <- data.frame( user_id, favorite_artist )

user_id <- c(1002,1005,1008)
favorite_composer <- c("Bach","Beethoven", "Bernstein")
Data_Music <- data.frame( user_id, favorite_composer )

user_id <- c(1003,1006,1009)
favorite_scientist <- c("Einstein","Newton", "Galen")
Data_Science <- data.frame( user_id, favorite_scientist )

user_id <- c(1001:1009)
favorite_philospher <- c("Aristotle", "Plato", "Kant", "Nietzsche", "Descartes", "Marx", "Buddha", "Confucius", "Averroes")
Data_Philosophy <- data.frame( user_id, favorite_philospher )

user_id <- c(1001:1009)
name <-c("Aaron","Betty","Charles","Dolly","Edgar","Felicia","Grover","Helen","Isaac")
other_data <- data.frame( user_id, name )

category.levels = c("Art","Music","Science","Philosophy")
for ( cat in category.levels) {
    dfname = paste("Data",cat,sep='_')
    print(dfname)
    curr.dataset = eval(parse(text=dfname))
    merged.data = merge(curr.dataset, other_data, by=c("user_id"))
    print(merged.data)
}

Output

source('test_eval.R')
[1] "Data_Art"
  user_id favorite_artist   name
1    1001           Monet  Aaron
2    1004        Van Gogh  Dolly
3    1007         Picasso Grover
[1] "Data_Music"
  user_id favorite_composer  name
1    1002              Bach Betty
2    1005         Beethoven Edgar
3    1008         Bernstein Helen
[1] "Data_Science"
  user_id favorite_scientist    name
1    1003           Einstein Charles
2    1006             Newton Felicia
3    1009              Galen   Isaac
[1] "Data_Philosophy"
  user_id favorite_philospher    name
1    1001           Aristotle   Aaron
2    1002               Plato   Betty
3    1003                Kant Charles
4    1004           Nietzsche   Dolly
5    1005           Descartes   Edgar
6    1006                Marx Felicia
7    1007              Buddha  Grover
8    1008           Confucius   Helen
9    1009            Averroes   Isaac
xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
  • Thanks for trying!! They are results of matching, maybe they aren't datafrmaes? I'll check. The strange thing was that it seems like the merge action somehow destroyed the variable... very very strange... – user3017075 Jun 02 '16 at 07:23