[R] Add column index number to str() output

David Winsemius dwinsemius at comcast.net
Fri Nov 23 22:03:06 CET 2012


On Nov 23, 2012, at 12:40 PM, Peter Ehlers wrote:

> On 2012-11-23 08:54, seth.fore wrote:
>> I often find it would be very useful when inspecting a data  
>> structure using str() to know the column index number in order to  
>> rearrange the data in a manner amenable to my purpose. Is there a  
>> way to modify the display options of the str() function to add  
>> column index? I know this is really a trivial matter but it would  
>> increase the utility of the str() function for me and increase the  
>> speed with which I can manipulate the data structure. Thanks,
>>
>> Seth
>
> Presumably, you're talking about dataframes. I don't see this
> as particularly useful unless you're in the habit of processing
> your data by column _number_ rather than variable _name_.
> If so, I would strongly discourage that habit.
> And a cursory look at str.default() suggests that it may not be
> all that trivial a code change.

I would offer the possibility of using names(dfrm) instead of  
str(dfrm) for this purpose. It would be reasonably easy to look at the  
output and figure out numbering. If you wanted every number in a  
single column. then use as.matrix(names(dfrm)).

 > names(apistrat)
  [1] "cds"      "stype"    "name"     "sname"    "snum"     "dname"
  [7] "dnum"     "cname"    "cnum"     "flag"     "pcttest"  "api00"
[13] "api99"    "target"   "growth"   "sch.wide" "comp.imp" "both"
[19] "awards"   "meals"    "ell"      "yr.rnd"   "mobility" "acs.k3"
[25] "acs.46"   "acs.core" "pct.resp" "not.hsg"  "hsg"      "some.col"
[31] "col.grad" "grad.sch" "avg.ed"   "full"     "emer"     "enroll"
[37] "api.stu"  "pw"       "fpc"
 > as.matrix(names(apistrat))
       [,1]
  [1,] "cds"
  [2,] "stype"
  [3,] "name"
  [4,] "sname"
  [5,] "snum"
  [6,] "dname"
  [7,] "dnum"
  [8,] "cname"
  [9,] "cnum"
[10,] "flag"
[11,] "pcttest"
[12,] "api00"
[13,] "api99"
[14,] "target"
[15,] "growth"
[16,] "sch.wide"
[17,] "comp.imp"
[18,] "both"
[19,] "awards"
[20,] "meals"
[21,] "ell"
[22,] "yr.rnd"
[23,] "mobility"
[24,] "acs.k3"
[25,] "acs.46"
[26,] "acs.core"
[27,] "pct.resp"
[28,] "not.hsg"
[29,] "hsg"
[30,] "some.col"
[31,] "col.grad"
[32,] "grad.sch"
[33,] "avg.ed"
[34,] "full"
[35,] "emer"
[36,] "enroll"
[37,] "api.stu"
[38,] "pw"
[39,] "fpc"

-- 
David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list