[R] problem with my base_df function ?

Ben Bolker bbo|ker @end|ng |rom gm@||@com
Wed Apr 16 18:45:34 CEST 2025


   When I ran your code I got

Error in `n()`:
! Must only be used inside data-masking verbs like `mutate()`, 
`filter()`, and `group_by()`.
Run `rlang::last_trace()` to see where the error occurred.


when I tried to define base_df.

   If I replace n() with length(.) it seems to work.

   General advice is to start debugging at the *first* error (or even 
warning) you see in your code; problems often cascade, leading to less 
useful diagnostics farther downstream ...

   Ben Bolker

On 2025-04-16 12:30 p.m., varin sacha wrote:
> Sorry,
> 
> At the end of the code I get an error message
> 
> Error: object 'base_df' not found
> 
> Best,
> 
> 
> 
> 
> 
> Le mercredi 16 avril 2025 à 18:23:42 UTC+2, Ben Bolker <bbolker using gmail.com> a écrit :
> 
> 
> 
> 
> 
>    Unless this is a continuation of a conversation I missed the
> beginning of, you haven't told us enough yet for us to help you.
> 
>    What is your problem?  Do you get an error? Does the code give you an
> incorrect result? How do you know? (It's possible that I could figure
> out that your code hits an error by running it myself, but if that is
> the case it would be better if you said "I get the following error <full
> text of error>, at the following point in the code ..." )
> 
> 
> 
> On 2025-04-16 12:11 p.m., varin sacha via R-help wrote:
>> R-experts,
>>
>> The R script here below.
>>
>> How to solve my problem ?
>>
>> Best,
>>
>>
>> ##############################
>> # Load packages
>>    
>> install.packages("remotes")
>> remotes::install_github("bd2kccd/r-causal")
>>    
>> # Install 'gssr' from 'ropensci' universe
>> install.packages('gssr', repos =
>>     c('https://kjhealy.r-universe.dev', 'https://cloud.r-project.org'))
>>    
>> # Also recommended: install 'gssrdoc' as well
>> install.packages('gssrdoc', repos =
>>     c('https://kjhealy.r-universe.dev', 'https://cloud.r-project.org'))
>>    
>> library(dplyr)
>> library(gssr)
>>
>> set.seed(123)
>>
>>
>> # Load dataset
>>
>> data("gss_panel06_long")
>>    
>>
>> # Filter one wave, remove variables with high NA, and keep usable types
>>
>> base_df <- gss_panel06_long %>%
>>
>>      filter(year == 2006) %>%
>>
>>      select_if(~ is.numeric(.) || is.factor(.)) %>%
>>
>>      select_if(~ sum(is.na(.)) < 0.1 * n()) %>%
>>
>>      na.omit()
>>
>>    
>> # Sample sizes to test
>>
>> sample_sizes <- c(30, 50, 100, 300, 500)
>>
>>    
>> # Set plotting layout
>>
>> par(mfrow = c(2, 3))  # 2 rows, 3 columns
>>
>>    
>> # Loop through sample sizes
>>
>> for (n in sample_sizes) {
>>
>>      df_sample <- sample_n(base_df, n)
>>
>>      result <- gfci(data = df_sample, test = "kci", score = "sem-bic")
>>
>>      plot(result, main = paste("n =", n))
>>
>> }
>> #################################
>>    
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 

-- 
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University
Director, School of Computational Science and Engineering
 > E-mail is sent at my convenience; I don't expect replies outside of 
working hours.



More information about the R-help mailing list