[R] Select single column, preserve name?

Duncan Elkins duncan at duncanelkins.com
Tue Apr 20 00:15:00 CEST 2010


Hi, list-
   I've got a large list of multi-column data and I'd like to filter
it according to a threshold, such as, "show me the values that are
above 0.4 for each line."

For instance, if the line of data were:
> line
     v1     v2    v3      v4     v5     v6    v7    v8      v9
1 -0.32 0.66 -0.35 -0.82 0.38 0.66 -0.02 -0.11 -0.64

I can do this
> line[,line >= 0.4]
    v2   v6
1 0.66 0.66

and

> names(line[,line >= 0.4])
[1] "v2" "v6"

That's great.  But, if there's only one value which passes the test, as in:

> line[,line <= -0.7]
[1] -0.82

The single value loses its name attribute (in this case, I want "v4").
 I guess I could kludge this by adding a dummy column that always
passes and trimming it out of the output, later, but it seems like
there ought to be an easier way.  Am I misusing the bracket notation,
or ignorant of some obvious way to subset just one column and retain
its name?

Thanks,
Duncan



More information about the R-help mailing list