Markm0705
Markm0705

Reputation: 1440

R substituting a variable when subsetting somedata

Consider the following

#make some data
x<-c("A","B","C","D")
y<-c(1,2,3,4)
test1<-as.data.frame(cbind(x,y))
test1

 x y
1 A 1
2 B 2
3 C 3
4 D 4

#subset 
test2<-test1[test1$x=="A",]
test2

  x y
1 A 1

Now I would like to be able to substitute a variable for the expression test$x in the subsetting stage, to something like:

blah<-"test1$x"
test2<-test1[blah=="A",]

(Of course, this does not work)

The idea here is I can set this at the top of the script and don't have to search and replace the particular selection criteria each time I wish to change it.

Any suggestions?

Upvotes: 1

Views: 73

Answers (2)

Itamar
Itamar

Reputation: 2151

The following should work:

subset1 <- (test1[[name1]] == val1)
test2[[name2]] <- test1[[name1]][subset1]

where name1 and name2 containe the names of the data frame columns you're working with.

Upvotes: 1

Stefan Bonn
Stefan Bonn

Reputation: 53

The eval() function is definitely the way to go. I also took the liberty to optimize your code a little:

test1 = data.frame(x = c("A","B","C","D"), y = c(1:4)) # no cbind() needed, you can directly insert your vectors into data.frame()
blah = quote(test1$x)
test2 = test1[ eval(blah) == "A", ]

You could even make your substitutions more fine-grained using get() and subset():

object = "test1"
column = "x"

test2 = subset( get(object), get(column) == "A" )

Upvotes: 0

Related Questions