Daniel O
Daniel O

Reputation: 4358

String as equation in R

I have a list of tables as follows

testinjest <- list("BN 01_181102_103545.data"=
  data.frame(V1 = c(1000L, 1100L, 1200L, 1300L),
   V2 = c(28.359069, 28.357302, 28.359111, 28.353656), 
   V3 = c(0.008764, 0.009662, 0.010677, 0.011763), 
   V4 = c(9e-06, 1.2e-05, 8e-06, 3e-06), 
   V5 = c(6e-06, 9e-06, 1e-05, 4e-06), 
   V6 = c(-152.4882, -151.2109, -150.5143, -150.6976)),
"BN 03_181102_105741.data"=
  data.frame(V1 = c(1000L, 1100L, 1200L, 1300L), 
   V2 = c(28.359069, 28.357302, 28.359111, 28.353656), 
   V3 = c(0.008764, 0.009662, 0.010677, 0.011763), 
   V4 = c(9e-06, 1.2e-05, 8e-06, 3e-06), 
   V5 = c(8e-06,3e-06, 1e-05, 8e-06), 
   V6 = c(-152.862, -151.5966, -150.7392, -151.165)))

Using the following function obtains the desired result

  lapply(testinjest,transform,V6=V6+200)

But I want to be able to pass the equation in with a string. Which does not appear to do anything.

lapply(testinjest,transform,eval(parse(text="V6=V6+200")))

Does anyone know of a way for the transform/lapply to accept strings as the equation?

Thanks.

Upvotes: 1

Views: 46

Answers (1)

MrFlick
MrFlick

Reputation: 206401

Actually within would make this easier than transform. The latter requires you use have named parameters to the function which are messy. If you just do assignments to the data environment, it's a bit easier to fake.

lapply(testinjest, within, eval(parse(text="V6=V6+200")))

But in general working with code as a string is not straightforward thing to do.

Upvotes: 1

Related Questions