views:

218

answers:

2

I'm not sure how to do this without getting an error. Here is a simplified example of my problem.

Say I have this data frame DF

a   b  c  d
1   2  3  4
2   3  4  5
3   4  5  6

Then I have a variable

x <- min(c(1,2,3))

Now I want do do the following

y <- DF[a == x]

But when I try to refer to some variable like "x" I get an error because R is looking for a column "x" in my data frame. I get the "undefined columns selected" error

How can I do what I am trying to do in R?

+4  A: 

You may benefit from reading an Introduction to R, especially on matrices, data.frames and indexing. Your a is a column of a data.frame, your x is a scalar. The comparison you have there does not work.

Maybe you meant

R> DF$a == min(c(1,2,3))
[1]  TRUE FALSE FALSE
R> DF[,"a"] == min(c(1,2,3))
[1]  TRUE FALSE FALSE
R> 

which tells you that the first row fits but not the other too. Wrapping this in which() gives you indices instead.

Dirk Eddelbuettel
+2  A: 

I think this is what you're looking for:

> x <- min(DF$a)
> DF[DF$a == x,]
  a b c d
1 1 2 3 4

An easier way (avoiding the 'x' variable) would be this:

> DF[which.min(DF$a),]
  a b c d
1 1 2 3 4

or this:

> subset(DF, a==min(a))
  a b c d
1 1 2 3 4
Ken Williams
The "x" was an example because I'm comparing to some other data. First part you listed is the syntax I didn't know about but needed
Matt