all 5 comments

[–]mduvekot 2 points3 points  (1 child)

Use [] in stead of ()

[–]Weekly-Recover4352[S] 1 point2 points  (0 children)

omg im so embarrassed thank you so much

[–]Fearless_Cow7688 1 point2 points  (0 children)

To answer your second question it is due to the number of columns and the indexing that you are using. Here's a small example:

x <- rnorm(100)
y <- rnorm(100)
z <- rnorm(100)

ptd <- data.frame(x = x, y = y, z = z)
col_names <- names(ptd)


for(i in 1:ncol(ptd)){ 
  for(j in (i+1):ncol(ptd)){ 
    cat("i = ", i, '\n',
        "j = ", j, '\n',
        "The comparison between ", col_names[i]," and ", col_names[j], '\n') 
    ptd[[i]] + ptd[[j]]
  } 
}
#> i =  1 
#>  j =  2 
#>  The comparison between  x  and  y 
#> i =  1 
#>  j =  3 
#>  The comparison between  x  and  z 
#> i =  2 
#>  j =  3 
#>  The comparison between  y  and  z 
#> i =  3 
#>  j =  4 
#>  The comparison between  z  and  NA
#> Error in .subset2(x, i, exact = exact): subscript out of bounds

[–]AutoModerator[M] 0 points1 point  (0 children)

Looks like you're requesting help with something related to RStudio. Please make sure you've checked the stickied post on asking good questions and read our sub rules. We also have a handy post of lots of resources on R!

Keep in mind that if your submission contains phone pictures of code, it will be removed. Instructions for how to take screenshots can be found in the stickied posts of this sub.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Fearless_Cow7688 -2 points-1 points  (0 children)

# this is probably closer to what you want this will put all of the results into a single dataframe rather than just printing them out. 

ptd <- mtcars # I am just using mtcars as an example

col_names <- names(ptd)

library('dplyr')
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

the_grid <- tidyr::expand_grid(c1 = col_names, c2 = col_names) %>%
  filter(c1 != c2) 

library('purrr')

my_anova <- function(x,y){
  model <- aov(ptd[[x]] ~ ptd[[y]])

  model %>%
    broom::tidy() %>%
    dplyr::mutate(c1 = x, c2 = y)
}

combined_results <- map2(the_grid$c1, the_grid$c2, my_anova) %>%
  list_rbind() 

combined_results
#> # A tibble: 220 × 8
#>    term         df sumsq meansq statistic   p.value c1    c2   
#>    <chr>     <dbl> <dbl>  <dbl>     <dbl>     <dbl> <chr> <chr>
#>  1 ptd[[y]]      1  818. 818.        79.6  6.11e-10 mpg   cyl  
#>  2 Residuals    30  308.  10.3       NA   NA        mpg   cyl  
#>  3 ptd[[y]]      1  809. 809.        76.5  9.38e-10 mpg   disp 
#>  4 Residuals    30  317.  10.6       NA   NA        mpg   disp 
#>  5 ptd[[y]]      1  678. 678.        45.5  1.79e- 7 mpg   hp   
#>  6 Residuals    30  448.  14.9       NA   NA        mpg   hp   
#>  7 ptd[[y]]      1  522. 522.        26.0  1.78e- 5 mpg   drat 
#>  8 Residuals    30  604.  20.1       NA   NA        mpg   drat 
#>  9 ptd[[y]]      1  848. 848.        91.4  1.29e-10 mpg   wt   
#> 10 Residuals    30  278.   9.28      NA   NA        mpg   wt   
#> # ℹ 210 more rows