0

I have a tall data.frame of data from multiple experiments in the format (toy example)

experiment  parameter  value
exp1        temp       25
exp1        press      1
exp2        temp       30
exp2        press      1

I want to feed an individual experiment to a function my.fun(filter(df, experiment == exp1)) and within that function, I want to break out the variables to temp = 25 and press = 1. I can use df2 = spread(df, parameter, value) but then I have df2$temp and df2$press. How can I just assign them as variables with the name of the parameter? I can do:

for(j in 1:length(names(df2))){
  assign(names(df2)[j],df2[j])
}

but is there a way to do it without the loop? I've tried things like sapply(names(df2),assign,df2) but I don't understand the error message given. It sounds like assign is kind of a maligned function. Is there a better way to do this generally?

Werner Hertzog
  • 2,002
  • 3
  • 24
  • 36
  • 1
    Why do you think you need to do this? Can't a simple `with()` statement help you? It would be better if you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that made it more clear what problem you were really trying to solve. – MrFlick Nov 22 '17 at 16:55
  • `assign` is oft maligned in part because your idea of using `spread`, or frankly just leaving all the info in the original form, is better in the long run, but you're trying to force yourself to use it anyway. – joran Nov 22 '17 at 16:58
  • If you're trying to call a single function over vectors of multiple arguments, there are tools for that like `mapply`, or whole packages like **purrr**. – joran Nov 22 '17 at 17:00
  • mapply is what I was missing, thank you! I don't *need* to do it. I could use dfspread$a, dfspread$b, dfspread$c... for my variables, but it's simpler to write and simpler to read if I can assign them to a, b, c... What would be the benefit of leaving it tall? (Genuinely asking, I'm more engineer than programmer and struggle not to hate tall data structures) If I wanted to calculate volume from temperature and pressure I could use mapply then say vol = R\*temp/pres. Leaving it as a tall df I think I'd have to make it vol = R\*filter(df,variable==temp)[3]/filter(df,variable==press)[3] a – SenatorNyxen Nov 22 '17 at 18:50

0 Answers0