You could create a simple function using this as a starting point: str.ext <- function(x) { if(!is.data.frame(x)) stop("No data frame!") st <- gsub(" \\$", "", capture.output(str(x))) nm <- prettyNum(0:length(x), width=3) cat("\n", paste(nm, st, "\n", sep=" ")) } str.ext(iris)
0 'data.frame': 150 obs. of 5 variables: 1 Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... 2 Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... 3 Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... 4 Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... 5 Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ... ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352 > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of David Winsemius > Sent: Friday, November 23, 2012 3:03 PM > To: Peter Ehlers > Cc: seth.fore; r-help@r-project.org > Subject: Re: [R] Add column index number to str() output > > > 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 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.