[R] Removing description from lm()

Duncan Murdoch murdoch.duncan at gmail.com
Sun Oct 5 16:23:41 CEST 2014


On 05/10/2014, 10:06 AM, billy am wrote:
> Thank you both very much. It is the unname that is what I am looking
> for. Thanks!
> 
> Btw , must the [1] be there? I am writing a Shiny web app hence I would
> like to display the value alone.

Try leaving it out, and you'll see what it's for.  (It is generally
pretty safe to experiment in R.  Don't worry, you won't break it.)

Duncan Murdoch

> 
> Thanks!
> 
>> unname(lm(x~y)$coefficient[1])
> [1] 0.5714286
>> coef(lm(x~y))["(Intercept)"]
> (Intercept) 
>   0.5714286 
> 
> 
>> 
> 
> 
> 
> 
> 
> 
> On Sun, Oct 5, 2014 at 9:09 PM, Uwe Ligges
> <ligges at statistik.tu-dortmund.de
> <mailto:ligges at statistik.tu-dortmund.de>> wrote:
> 
> 
> 
>     On 05.10.2014 15:02, Duncan Murdoch wrote:
> 
>         On 05/10/2014, 7:21 AM, billy am wrote:
> 
>             Hi ,
> 
>             When I run the following code , I get both the description
>             and the value ,
>             eg : Intercept and 0.5714286.
> 
>             Is there a way to extract just the value 0.5714286? Thanks!
> 
> 
>                 x <- c(1,5,3,1)> y <- c(5,8,2,3)> lm(x~y)
> 
>             Call:
>             lm(formula = x ~ y)
> 
>             Coefficients:
>             (Intercept)            y
>                   0.5714       0.4286
> 
>                 lm(x~y)$coefficient[1](__Intercept)
> 
>                0.5714286
> 
> 
>         It's a name, not a description.  The result is a named vector.
> 
>         To get rid of the name, call unname() on it, i.e.
> 
>         unname(lm(x~y)$coefficient[1])
> 
> 
>     I guess the OP is going to use the name (here "(Intercept)" without
>     the quotes) to extract the value, hence (also using the extractior
>     function coef()):
> 
>     coef(lm(x~y))["(Intercept)"]
> 
>     Best,
>     Uwe Ligges
> 
> 
> 
> 
>         Duncan Murdoch
> 
>         ________________________________________________
>         R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>         https://stat.ethz.ch/mailman/__listinfo/r-help
>         <https://stat.ethz.ch/mailman/listinfo/r-help>
>         PLEASE do read the posting guide
>         http://www.R-project.org/__posting-guide.html
>         <http://www.R-project.org/posting-guide.html>
>         and provide commented, minimal, self-contained, reproducible code.
> 
>



More information about the R-help mailing list