0

I have saved text using paste, now i want to run as code.

eg: i have below data

data

     a b c
[1,] 1 2 3
[2,] 2 3 2
[3,] 3 4 3
[4,] 4 2 1

I want to create subset data1 having a,b

i created a variable having a,b

variable<-paste("a,","b")
script=paste("data1=subset(data,select=c(",variable,"))")
script
[1] "data1=subset(data,select=c( a, b ))"

now how to run this as script and get data1?

nicola
  • 24,005
  • 3
  • 35
  • 56
  • Why do you have to do this way? It is easier to `variable <- c('a', 'b'); data1 <- data[,variable]` or if it is already `paste`d, then `data[,strsplit(variable, ', ')[[1]]]` – akrun Jun 09 '15 at 07:40

1 Answers1

1

Just try:

eval(parse(text=script))
data1
#     a b
#[1,] 1 2
#[2,] 2 3
#[3,] 3 4
#[4,] 4 2
nicola
  • 24,005
  • 3
  • 35
  • 56
  • 1
    [but](http://stackoverflow.com/questions/13647046/avoiding-the-infamous-evalparse-construct)... ;-) – Cath Jun 09 '15 at 07:39