0

This seems to be a basic problem. I am attempting to loop through a list of variables to determine the shapiro-wilk statistic. However, the var is not registering anything in the loop. It appears as "NULL (empty)" in the environment window of RStudio.

The test works fine outside of the for loop. Any thoughts on what needs to be corrected here?

library(broom)    
data = "some data.csv"    
vars = list("var1", "var2")
     for (var in names(vars)) {    
          sw <- tidy(shapiro.test(data$var))
          write.csv(sw, file = "shapirowilk_pvalue_" + var + ".csv")
}

Thanks in advance

EDIT1 Following the advice in the comments, the revised (and now working) code is:

vars = c("var1", "var2")
for (var in vars) {
  sw <- tidy(shapiro.test(Data[[var]]))

The syntax for the write.csv is not working...

EDIT2 The full working code is below, which writes to a text file:

sw_pvalues <- data.frame()
vars = c("var1", "var2")
for (var in vars) {

  sw <- tidy(shapiro.test(Data[[var]]))

  tempvalues <- cbind(var,sw)
  colnames(tempvalues) <- c('variable', 'sw_stat', 'pvalue', 'method')
  sw_pvalues = rbind(sw_pvalues, tempvalues)

}
write.csv(sw_pvalues, file = "shapirowilk_pvalues.csv")
viridius
  • 477
  • 5
  • 17
  • 1
    `data$var` is shorthand for the data's `"var"` column (which doesn't exist). You want to use `data[[var]]`. – Nathan Werth Sep 15 '17 at 18:07
  • Try using the data[[var]] instead of data$var. One needs to programmatically access the variable. – Dave2e Sep 15 '17 at 18:07
  • Also, (I'm not sure if your code is just pseudo code, but) `names(list('var1', 'var2'))` evaluates to `NULL`, so that might be impacting your for loop. – tblznbits Sep 15 '17 at 18:09
  • I switched to `[[var]]` but no change in the output. It seems that `var1` should be set in the `for` line, regardless of how the shapiro test is specified. – viridius Sep 15 '17 at 18:17
  • The elements of the list (`var1` and `var2`) are field headings of the `data` table. – viridius Sep 15 '17 at 18:22
  • @MrFlick, I looked at the two questions you considered to be duplicated with and it appears that my situation is unique given that replacing `$var` with `[[var]]`did not help. Any other thoughts? – viridius Sep 15 '17 at 18:30
  • Use `vars = c("var1", "var2")` (just use a vector rather than a list), and use `for (var in vars)` for the loop. Still use `shaprio.text(data[[var]])` in the loop. – MrFlick Sep 15 '17 at 18:32
  • @MrFlick, changing to `vars = c("var1", "var2")` still does not help. This is very perplexing to me. – viridius Sep 15 '17 at 18:36
  • And you change the for loop to not use `names()`? Edit your question to show what you are currently actually trying. – MrFlick Sep 15 '17 at 18:40
  • Ah, yes. All set now. Many thanks MrFlick. Still need to work on concatenating my filename, but should be ok with that – viridius Sep 15 '17 at 18:46
  • The original question has been edited to include the final working code, including writing results to a single text file with variable names – viridius Sep 15 '17 at 21:09

0 Answers0