Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7079775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:42:48+00:00 2026-05-28T06:42:48+00:00

I am trying to create a loop that will perform a series of analyses.

  • 0

I am trying to create a loop that will perform a series of analyses. I am using geeglm from geepack, which fails if there are any null values. Creating a subset solves this, but do not seem to be able to set the subset dynamically based on a changing variable.

while (j <= y.num) {

     strSubset = as.character(df.IV$IV[j])  #Gives column name in quotes
     df.data.sub = subset(df.data, strSubset>=0)

 #subset dataset is not created

 # analyses on subset take place

    j = j + 1
 }

If I type the variable name in the formula it works, so I assume that I am not creating the variable in a manner that allows it to be evaluated in the subset function. Any help would be greatly appreciated!

Reproducible example:

# dataset
age<-18:29
height<-58:69
df.ex=data.frame(age=age,height=height)
df.ex[4,1]<-NA

# dataset of columns that will be used for analysis
values<-c("age", "height")
df.variables=data.frame(values)

 # Age column has a null (NA) value.  The row must be removed for the analysis to run
 # explicit creation  
df.ex.sub.explicit<-subset(df.ex, age >= 0)
dim(df.ex.sub.explicit) #11 obs of 2 variables


i=1
strFilter=as.character(df.variables$values[i])
df.ex.sub.passvar<-subset(df.ex,strFilter>=0)
dim(df.ex.sub.explicit) #12 obs of 2 variables
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:42:49+00:00Added an answer on May 28, 2026 at 6:42 am

    I would suggest:

    df.ex=data.frame(age=18:29,height=58:69)
    df.ex[4,1]<-NA
    

    It’s a little easier to store this list of variables as a character vector, unless you need the variables to be coupled with other information about the variables …

    df.variables <- c("age", "height")
    
    for (i in seq_along(df.variables)) {
      vname <- df.variables[i]  ## get variable name
      df.ex.sub.passvar <- df.ex[!is.na(df.ex[[vname]]),]
      print(dim(df.ex.sub.passvar))
    }
    

    subset and $ are great for interactive use, but for programming it is probably best to use “machine-style” indexing with [ and [[ … also, you need to use is.na() to test for NA values. subset() has a quirk in that it will drop values for which the result of the test is either FALSE or NA, but it is probably clearer to use the explicit test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a loop that will output multiple where statements for a
I am trying to create a for loop that will parse out different elements
I'm trying to create a script that will loop through files that have their
having a small problem. I'm trying to create a loop that will create an
I'm trying to create a simple for loop that will take all the outputs
I'm trying to create a function that will loop through an array of various
I am trying to create a script that will get all keys from the
I'm trying to create a hotkey toggle(f12) that will turn on a loop when
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I'm trying to create a method that will send a PDF file directly to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.