[R] Select single column, preserve name?

Brian Diggs diggsb at ohsu.edu
Tue Apr 20 00:30:40 CEST 2010


On 4/19/2010 3:15 PM, Duncan Elkins wrote:
> 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 am assuming line is a data.frame.

 > line <- data.frame(v1=-0.32, v2=0.66, v3=-0.35, v4=-0.82, v5=0.38, 
v6=0.66, v7=-0.02, v8=-0.11, v9=-0.64)
 > 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

 > line[,line <= -0.7, drop=FALSE]
      v4
1 -0.82
 > str(line[,line <= -0.7])
  num -0.82
 > str(line[,line <= -0.7, drop=FALSE])
'data.frame':   1 obs. of  1 variable:
  $ v4: num -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
>

--
Brian Diggs
Senior Research Associate, Department of Surgery, Oregon Health & 
Science University



More information about the R-help mailing list