0

The following code

df <- data.frame(place = c("South","South","North","East"),
                 temperature = c(30,30,20,12),
                 outlookfine=c(TRUE,TRUE,FALSE,FALSE)
                 )
glm.fit <- glm(outlookfine ~ .,df , family= binomial)
coef.glm <-coef(summary(glm.fit))
coef.glm

outputs

             Estimate Std. Error       z value  Pr(>|z|)
(Intercept) -23.56607   79462.00 -0.0002965703 0.9997634
placeNorth    0.00000  112376.25  0.0000000000 1.0000000
placeSouth   47.13214   97320.68  0.0004842972 0.9996136

I want to re-display the list without the intercept and without places containing the phrase "South"

I thought of trying to name the index column and then subset on it but have had no success.

[Update] I added more data to understand why George Sava's answer also stripped out "North"

df <- data.frame(place = c("South","South","North","East","West"),
                 temperature = c(30,30,20,12,15),
                 outlookfine=c(TRUE,TRUE,FALSE,FALSE,TRUE)
                 )
glm.fit <- glm(outlookfine ~ .,df, family= binomial )
coef.glm <-coef(summary(glm.fit))
coef.glm[!grepl(pattern = ("South|Intercept"), rownames(coef.glm)),]

outputs

               Estimate Std. Error      z value  Pr(>|z|)
placeNorth 3.970197e-14   185277.1 2.142843e-19 1.0000000
placeWest  4.913214e+01   185277.2 2.651818e-04 0.9997884
Kirsten
  • 15,730
  • 41
  • 179
  • 318

2 Answers2

2

To keep only rows that match (or do not match) a certain pattern, you can use:

coef.glm[!grepl("South|Intercept", rownames(coef.glm)),]

Note when there's only one row selected this becomes a vector.

George Savva
  • 4,152
  • 1
  • 7
  • 21
1

If you want to retain row names as a column then you could do something like:

library(tibble)
library(dplyr)

as.data.frame(coef.glm) %>% 
  rownames_to_column("x") %>% 
  filter(!grepl("Intercept|South", x))

Output

           x      Estimate   Std. Error    t value  Pr(>|t|)
1 placeNorth -1.281975e-16 3.140185e-16 -0.4082483 0.7532483
LMc
  • 12,577
  • 3
  • 31
  • 43