Error: Can't subset columns that don't exist. x Column `nobs` doesn't exist.

Hello!

I need any help with that issue:

Error: Can't subset columns that don't exist. x Column nobs doesn't exist.

Nobs is not a columm is a fuction:

mutate(var = "TC_log")) %>%
select(var, r.squared:nobs) %>%
pivot_longer(-var) %>%
filter(name %in% c("r.squared", "statistic", "df",
"df.residual", "p.value")) %>%

I appreciate you help!!

Hello @LuciaFerrer,

Welcome to the forum :slight_smile: select cannot handle a function as it is looking for a column. I can help you if you can post what it is you're trying to achieve and a reprex of your code (See here: FAQ: How to do a minimal reproducible example ( reprex ) for beginners)

There is an extra parenthesis at the end of the statement above. After you remove it, please let us know if the error persists.

Note that the example below works, when using the built-in mtcars data frame:

library(tidyverse)
library(broom)

glance(lm(mpg ~ hp, data=mtcars)) %>% 
  mutate(var = "TC_log") %>%
  select(var, r.squared:nobs) %>%
  pivot_longer(-var) %>%
  filter(name %in% c("r.squared", "statistic", "df",
                     "df.residual", "p.value")) 
  var    name               value
  <chr>  <chr>              <dbl>
1 TC_log r.squared    0.602      
2 TC_log statistic   45.5        
3 TC_log p.value      0.000000179
4 TC_log df           1          
5 TC_log df.residual 30 

Hello, thanks for reply to me: This is the complete script

modelo_dp2 %>% 
  glance() %>% 
  mutate(var = "DP") %>% 
  bind_rows(modelo_dasr2 %>% 
  glance() %>% 
  mutate(var = "DASR_log")) %>% 
  bind_rows(modelo_dlr2 %>% 
  glance() %>% 
  mutate(var = "DLR_log")) %>% 
  bind_rows(modelo_tc2 %>% 
  glance() %>% 
  mutate(var = "TC_log")) %>% 
  select(var, r.squared:nobs) %>% 
  pivot_longer(-var) %>% 
  filter(name %in% c("r.squared", "statistic", "df", 
                     "df.residual", "p.value")) %>% 
  mutate(value = round(value, 3)) %>% 
  pivot_wider(names_from = var, 
              values_from = value) %>% 
  mutate(name = factor(c("R²", "F", "P valor", 
                         "Grados de libertad", 
                         "Grados de libertad residual"))) %>% 
  gt() %>% 
  cols_label(name = "Medida") %>% 
  fmt_number(2:5, rows = 1:2, decimals = 2) %>% 
  fmt_number(2:5, rows = 3, decimals = 3) %>% 
  fmt_number(2:5, rows = 4:5, decimals = 0) %>% 
  tab_options(table.font.size = "small")

In your code above, three of the mutate steps have two parentheses at the end, but they should have only one parenthesis. For example:

Change this: \hspace{0.5cm} mutate(var = "DASR_log")) %>%
to this: \hspace{1.55cm} mutate(var = "DASR_log") %>%

Still appearing the error:

Error: Incomplete expression: modelo_dp2 %>%

I think is not the problem, I tried also with 2 parenthesis in : mutate(var = "DP") %>% . It dosnt works

Ignore my previous comment. It applies only to the incomplete code in your first example.

Here's a working example that's analogous to the complete code in your response:

library(tidyverse) 
library(broom)

# Set up three regression models
modelo_dp2 = lm(mpg ~ hp, data=mtcars)
modelo_dasr2 = lm(mpg ~ wt, data=mtcars) 
modelo_tc2 = lm(mpg ~ cyl, data=mtcars) 

modelo_dp2 %>% 
  glance() %>% 
  mutate(var = "DP") %>% 
  bind_rows(modelo_dasr2 %>% 
              glance() %>% 
              mutate(var = "DASR_log")) %>% 
  bind_rows(modelo_tc2 %>% 
              glance() %>% 
              mutate(var = "TC_log")) %>% 
  select(var, r.squared:nobs) %>% 
  pivot_longer(-var) %>% 
  filter(name %in% c("r.squared", "statistic", "df", 
                     "df.residual", "p.value")) %>% 
  mutate(value = round(value, 3)) %>% 
  pivot_wider(names_from = var, 
              values_from = value) %>% 
  mutate(name = factor(c("R²", "F", "P valor", 
                         "Grados de libertad", 
                         "Grados de libertad residual")))

#> # A tibble: 5 x 4
#>   name                            DP DASR_log TC_log
#>   <fct>                        <dbl>    <dbl>  <dbl>
#> 1 R²                           0.602    0.753  0.726
#> 2 F                           45.5     91.4   79.6  
#> 3 P valor                      0        0      0    
#> 4 Grados de libertad           1        1      1    
#> 5 Grados de libertad residual 30       30     30

Created on 2020-10-19 by the reprex package (v0.3.0)

And here is a different approach using the imap_dfr function. This version avoids having to keep calling the glance function explicitly:

library(tidyverse) 
library(broom)

# Set up three regression models
modelo_dp2 = lm(mpg ~ hp, data=mtcars)
modelo_dasr2 = lm(mpg ~ wt, data=mtcars) 
modelo_tc2 = lm(mpg ~ cyl, data=mtcars) 

# Vector of model object names
nm = paste0("modelo_", c("dp2", "dasr2", "tc2"))

# Combine outtput of glance for all of the models
model_summary = nm %>% 
  set_names() %>% 
  imap_dfr(~get(.x) %>% 
             glance() %>% 
             mutate(var=toupper(str_replace(.y, "modelo_", "")))) %>% 
  select(var, everything())

model_summary
#> # A tibble: 3 x 13
#>   var   r.squared adj.r.squared sigma statistic  p.value    df logLik   AIC
#>   <chr>     <dbl>         <dbl> <dbl>     <dbl>    <dbl> <dbl>  <dbl> <dbl>
#> 1 DP2       0.602         0.589  3.86      45.5 1.79e- 7     1  -87.6  181.
#> 2 DASR2     0.753         0.745  3.05      91.4 1.29e-10     1  -80.0  166.
#> 3 TC2       0.726         0.717  3.21      79.6 6.11e-10     1  -81.7  169.
#> # … with 4 more variables: BIC <dbl>, deviance <dbl>, df.residual <int>,
#> #   nobs <int>

model_summary %>% 
  select(var, r.squared:nobs) %>% 
  pivot_longer(-var)
#> # A tibble: 36 x 3
#>    var   name                  value
#>    <chr> <chr>                 <dbl>
#>  1 DP2   r.squared       0.602      
#>  2 DP2   adj.r.squared   0.589      
#>  3 DP2   sigma           3.86       
#>  4 DP2   statistic      45.5        
#>  5 DP2   p.value         0.000000179
#>  6 DP2   df              1          
#>  7 DP2   logLik        -87.6        
#>  8 DP2   AIC           181.         
#>  9 DP2   BIC           186.         
#> 10 DP2   deviance      448.         
#> # … with 26 more rows

Created on 2020-10-19 by the reprex package (v0.3.0)

which the problem? your works so well, mine not

I don't have your model objects, so I can't tell why your code isn't working. You should try running shorter pieces of your code in order to find first statement that causes an error.

thankyou for your help!!

Were you able to solve your problem? If not, let us know where your code is failing and we'll see if we can help.

Hello Dear Joels,

I have resolved the problem, it was some packages outdated!

Thanks for helping me, I appreciate a lot!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.