# [R] function lm, get back the coefficient

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jul 26 22:48:08 CEST 2011

```On Tue, 2011-07-26 at 13:42 -0700, Bert Gunter wrote:
> Not quite, Gavin. You have to assign the value of unname back:

Yes; thought that would be a given. The point was to give a simple
example to show that `unname()` removes names/dimnames.

G

> > z <- structure(2, names="a")
> > unname(z)
> [1] 2
> > z
> a
> 2
> > zz <- unname(z)
> > zz
> [1] 2
> > names(z) <- NULL
> > z
> [1] 2
>
> Cheers,
> Bert
>
>
> On Tue, Jul 26, 2011 at 1:18 PM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
> > On Tue, 2011-07-26 at 16:43 +0100, Barry Rowlingson wrote:
> >> On Tue, Jul 26, 2011 at 4:21 PM, ascoquel <ascoquel at yahoo.fr> wrote:
> >> > Hi,
> >> >
> >> > I've done a linear fit on my data and I would like to get back the a (time)
> >> > coefficient ...
> >> >
> >> > mod<-lm(res_sql2\$Lx0x~0+time)
> >> > result<-data.frame()
> >> > result<-coef(mod)
> >> > print("result")
> >> > print(result)
> >> > [1] "result"
> >> >      time
> >> > 0.02530191
> >> >
> >> > But I would like just the value 0.02530191 ... I tried result\$time but it
> >> > doesn't work ...
> >>
> >>  It is 'just the value'. It happens to be in a named vector with a
> >> length of 1. You can do anything to it that you want to do with any
> >> other number. Try result * 2, or sqrt(result).
> >>
> >>  If it really annoys you, try names(result)=NULL to get rid of the name.
> >>
> >> Barry
> >
> > `unname()` exists for this purpose:
> >
> >> foo <- 1
> >> names(foo) <- "bar"
> >> foo
> > bar
> >  1
> >> unname(foo)
> > [1] 1
> >
> > G
> > --
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> >  Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
> >  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
> >  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
> >  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
> >  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>

--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```