Sverre Stausland
2011-Nov-15 04:49 UTC
[R] Remove names and more from list with capture.output()
Hi R users, I end up with a list object after running an anova:> lm(speed ~ 1 + dist + speed:dist, data = cars) -> Int > lm(speed ~ 1 + dist, data = cars) -> NoInt > anova(Int, NoInt) -> test > test <- test[c("Df", "F", "Pr(>F)")][2,] > is.list(test)[1] TRUE> testDf F Pr(>F) 2 -1 18.512 8.481e-05 *** --- Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 I would like to use capture.output() when writing this information to a text file, but I would like to only print the row, not the names (Df F Pr(>F)), and not the significance codes. That is, I want the text printed to my text file to be: 2 -1 18.512 8.481e-05 *** Is there a way to do this? Thanks Sverre
R. Michael Weylandt
2011-Nov-15 05:13 UTC
[R] Remove names and more from list with capture.output()
The easiest thing is almost certainly going to be to redefine print.aov as desired. You can get it at by typing stats:::print.aov. Copy the code edit and then reassign it to print.aov in the global environment and voila! Michael On Mon, Nov 14, 2011 at 11:49 PM, Sverre Stausland <johnsen at fas.harvard.edu> wrote:> Hi R users, > > I end up with a list object after running an anova: > >> lm(speed ~ 1 + dist + speed:dist, data = cars) -> Int >> lm(speed ~ 1 + dist, data = cars) -> NoInt >> anova(Int, NoInt) -> test >> test <- test[c("Df", "F", "Pr(>F)")][2,] >> is.list(test) > [1] TRUE >> test > ?Df ? ? ?F ? ?Pr(>F) > 2 -1 18.512 8.481e-05 *** > --- > Signif. codes: ?0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 > > I would like to use capture.output() when writing this information to > a text file, but I would like to only print the row, not the names (Df > ? ? F ? ?Pr(>F)), and not the significance codes. That is, I want the > text printed to my text file to be: > > 2 -1 18.512 8.481e-05 *** > > Is there a way to do this? > > Thanks > Sverre > > ______________________________________________ > R-help at 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. >
David Winsemius
2011-Nov-15 13:11 UTC
[R] Remove names and more from list with capture.output()
On Nov 14, 2011, at 11:49 PM, Sverre Stausland wrote:> Hi R users, > > I end up with a list object after running an anova: > >> lm(speed ~ 1 + dist + speed:dist, data = cars) -> Int >> lm(speed ~ 1 + dist, data = cars) -> NoInt >> anova(Int, NoInt) -> test >> test <- test[c("Df", "F", "Pr(>F)")][2,] >> is.list(test) > [1] TRUE >> test > Df F Pr(>F) > 2 -1 18.512 8.481e-05 *** > --- > Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 > > I would like to use capture.output() when writing this information to > a text file, but I would like to only print the row, not the names (Df > F Pr(>F)), and not the significance codes. That is, I want the > text printed to my text file to be: > > 2 -1 18.512 8.481e-05 *** >the output of capture.output is just going to be a character vector and you should select the second element. > vec <- capture.output(test) > vec[2] [1] "2 -1 18.512 8.481e-05 ***"> Is there a way to do this? > > Thanks > Sverre > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT
Sverre Stausland
2011-Nov-15 19:43 UTC
[R] Remove names and more from list with capture.output()
Thanks David - this is pretty close to what I am looking for. However, the output of vec[2] now includes the row number [1] and quotations marks at the endpoints of the row. Is there an easy way to exclude those? Thanks Sverre On Tue, Nov 15, 2011 at 8:11 AM, David Winsemius <dwinsemius at comcast.net> wrote:> > On Nov 14, 2011, at 11:49 PM, Sverre Stausland wrote: > >> Hi R users, >> >> I end up with a list object after running an anova: >> >>> lm(speed ~ 1 + dist + speed:dist, data = cars) -> Int >>> lm(speed ~ 1 + dist, data = cars) -> NoInt >>> anova(Int, NoInt) -> test >>> test <- test[c("Df", "F", "Pr(>F)")][2,] >>> is.list(test) >> >> [1] TRUE >>> >>> test >> >> ?Df ? ? ?F ? ?Pr(>F) >> 2 -1 18.512 8.481e-05 *** >> --- >> Signif. codes: ?0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 >> >> I would like to use capture.output() when writing this information to >> a text file, but I would like to only print the row, not the names (Df >> ? ?F ? ?Pr(>F)), and not the significance codes. That is, I want the >> text printed to my text file to be: >> >> 2 -1 18.512 8.481e-05 *** >> > the output of capture.output is just going to be a character vector and you > should select the second element. > >> vec <- capture.output(test) >> vec[2] > [1] "2 -1 18.512 8.481e-05 ***" > >> Is there a way to do this? >> >> Thanks >> Sverre >> >> ______________________________________________ >> R-help at 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. > > David Winsemius, MD > West Hartford, CT > >
David Winsemius
2011-Nov-15 20:00 UTC
[R] Remove names and more from list with capture.output()
On Nov 15, 2011, at 2:43 PM, Sverre Stausland wrote:> Thanks David - this is pretty close to what I am looking for. However, > the output of vec[2] now includes the row number [1] and quotations > marks at the endpoints of the row. Is there an easy way to exclude > those? >The usual method is to use cat() rather than print(). Those items are not part of the vector. -- David.> Thanks > Sverre > > On Tue, Nov 15, 2011 at 8:11 AM, David Winsemius <dwinsemius at comcast.net > > wrote: >> >> On Nov 14, 2011, at 11:49 PM, Sverre Stausland wrote: >> >>> Hi R users, >>> >>> I end up with a list object after running an anova: >>> >>>> lm(speed ~ 1 + dist + speed:dist, data = cars) -> Int >>>> lm(speed ~ 1 + dist, data = cars) -> NoInt >>>> anova(Int, NoInt) -> test >>>> test <- test[c("Df", "F", "Pr(>F)")][2,] >>>> is.list(test) >>> >>> [1] TRUE >>>> >>>> test >>> >>> Df F Pr(>F) >>> 2 -1 18.512 8.481e-05 *** >>> --- >>> Signif. codes: 0 ?***? 0.001 ?**? 0.01 ?*? 0.05 ?.? 0.1 ? ? 1 >>> >>> I would like to use capture.output() when writing this information >>> to >>> a text file, but I would like to only print the row, not the names >>> (Df >>> F Pr(>F)), and not the significance codes. That is, I want the >>> text printed to my text file to be: >>> >>> 2 -1 18.512 8.481e-05 *** >>> >> the output of capture.output is just going to be a character vector >> and you >> should select the second element. >> >>> vec <- capture.output(test) >>> vec[2] >> [1] "2 -1 18.512 8.481e-05 ***" >> >>> Is there a way to do this? >>> >>> Thanks >>> Sverre >>> >>> ______________________________________________ >>> R-help at 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. >> >> David Winsemius, MD >> West Hartford, CT >> >>David Winsemius, MD West Hartford, CT