Home > Software design >  How to have a list iterate the name of a variable as it runs through a loop
How to have a list iterate the name of a variable as it runs through a loop

Time:11-15

thank you for your time!

I am trying to run a chi-squared test through each of the variables in my dataset, and I am executing it with a loop that runs through a list. However, I am trying to figure out the syntax to make the loop print the variable name as it iterates through. Here is my code

#Defining Datalist
dataList = list(db.binom$Immigrant,db.binom$`Known to Police or FBI`, db.binom$`Criminal Record`,
                db.binom$`History of Physical Altercations`, db.binom$`History of Animal Abuse`,
                db.binom$`History of Domestic Abuse`,db.binom$`History of Sexual Offenses`,db.binom$`Gang Affiliation`
,db.binom$`Terror Group Affiliation`,db.binom$Bully,db.binom$Bullied,db.binom$`Raised by Single Parent`
,db.binom$`Parental Divorce / Separation`,db.binom$`Parental Death in Childhood`
,db.binom$`Parental Suicide`,db.binom$`Childhood Trauma`,db.binom$`Physically Abused`
,db.binom$`Sexually Abused`,db.binom$`Emotionally Abused`,db.binom$Neglected
,db.binom$`Mother Violent Treatment`,db.binom$`Parental Substance Abuse`
,db.binom$`Parent Criminal Record`,db.binom$`Family Member Incarcerated`
,db.binom$`Signs of Being in Crisis`,db.binom$`Inability to Perform Daily Tasks`
,db.binom$`Notably Depressed Mood`,db.binom$`Unusually Calm or Happy`,db.binom$`Rapid Mood Swings`
,db.binom$`Increased Agitation`,db.binom$`Abusive Behavior`,db.binom$`Isolation`
,db.binom$`Losing Touch with Reality`,db.binom$`Paranoia`,db.binom$`Prior Hospitalization`  
,db.binom$`Prior Counseling`,db.binom$`Psychiatric Medication`,db.binom$`Treatment 6 Months Prior to Shooting`
,db.binom$`Autism Spectrum`,db.binom$`Health Issues`,db.binom$`Head Injury / Possible TBI`
,db.binom$`Motive: Racism/Xenophobia`,db.binom$`Motive: Religious Hate`,db.binom$`Motive: Misogyny`
,db.binom$`Motive: Homophobia`,db.binom$`Motive: Employment Issue`  
,db.binom$`Motive: Economic Issue`,db.binom$`Motive: Legal Issue`,db.binom$`Motive: Relationship Issue`
,db.binom$`Motive: Fame-Seeking`,db.binom$`Motive: Unknown`,db.binom$`Role of Psychosis in the Shooting`
,db.binom$`Interest in Past Mass Violence`,db.binom$`Relationship with Other Shooting(s)`
,db.binom$Planning,db.binom$Performance,db.binom$`Interest in Firearms`)

#Running the for-loop
for(i in dataList){
  print(names(dataList[[i]]))
  print(chisq.bintest(as.factor(db.binom$shooter) ~ (i), db.binom, correct = FALSE, alpha = 0.05, p.method = "fdr"))
}

CodePudding user response:

Create a vector of column names and then loop over the column names

library(janitor)
db.binom <- clean_names(db.binom)
db.binom$shooter <- factor(db.binom$shooter)
nm1 <- c("Immigrant", ..., "Interest in Firearms")
out <- vector("list", length(nm1))
names(out) <- nm1
for(nm in nm1)
  {
 print(nm)
 out[[nm]] <- chisq.bintest(reformulate(i, response = 'shooter'), db.binom,
     correct = FALSE, alpha = 0.05, p.method = "fdr")
print(out[[nm]])
}

  • Related