Loop for inside a list with a group argument function

Asked

Viewed 70 times

1

I have this data:

df_1 <- data.frame(
  x = replicate(
    n = 3, expr = runif(n = 30, min = 20, max = 100), simplify = TRUE
  ), 
  y = as.factor(sample(x = 1:3, size = 30, replace = TRUE))
)

And this list:

lt_1 <- split(
  x = df_1, 
  f = df_1[['y']]
)

names(lt_1) <- paste('df', seq_along(lt_1), sep = '_')
names(lt_1)

I want to apply the function pairwise.t.test for each df from this list. I tried:

for (i in lt_1) {
  for (f in names(i[, 1:3])) {
    print(pairwise.t.test(x = lt_1[, f], g = lt_1[['y']], p.adj = 'bonferroni'))
  }
}

Error in lt_1[, f] incorrect number of Dimensions

Unsuccessful.

This function has a group argument (g). Maybe the problem is there, I believe.

  • 3

    When you are applying the test, 'g' has only one factor, which makes the test impossible.

  • 1

    If the doubt remains with the lists we can create an example that makes sense in the context of the test. The problem was in the lists or in the test?

  • I provided a wrong example. There’s only one factor in the lists. It’s actually a typo. You can close the question. I thank you all for your attention and forgiveness for.

2 answers

1


The problem with is in x and in the g, you must replace the lt_1 for i:

for (i in lt_1) {
for (f in names(i[, 1:3])) {
    print(pairwise.t.test(x = i[, f], g = i[['y']], p.adj = 'bonferroni'))
  }
}

However, for the data you are using, no matrix is generated, because the response variable y is a factor of only 1 value. To generate the table you need the response variable to have at least 1 value.

  • Filipe Lauar, It doesn’t work. See: <0 x 0 matrix>

  • But what do you want to return? The adjustment I made was for you to use the correct structure. The variable i corresponds to each date.frame of the variable lt_1 and fis the date column.frame.

  • Must return the p-values of each group comparison. And there is no return. If the function is applied in df_1, works.

  • But the error then is in logic, I just fixed the error of your code. You want to find the p_value of each of the columns "x.1" "x.2" "x.3" by relating them to the columns "y"?

  • Yes. But nothing comes back.

  • 1

    Maybe it’s because the variable gis a factor of only 1 value.

  • I tested here include new values in the column y and the data.frame is generated. I will update my answer explaining this.

  • You are absolutely right. I was wrong in the example. I divided by groups, and each df was with only one group. Sorry. You can update the answer.

Show 3 more comments

1

If I understand the question, it is not necessary split, just pass the factor y at each test.

t_tests <- lapply(names(df_1)[1:3], function(nms){
  pairwise.t.test(df_1[[nms]], df_1[['y']], p.adjust.method = 'bonferroni')
})

t_tests  
#[[1]]
#
#   Pairwise comparisons using t tests with pooled SD 
#
#data:  df_1[[nms]] and df_1[["y"]] 
#
#  1 2
#2 1 -
#3 1 1
#
#P value adjustment method: bonferroni 
#
#[[2]]
#
#   Pairwise comparisons using t tests with pooled SD 
#
#data:  df_1[[nms]] and df_1[["y"]] 
#
#  1    2   
#2 0.79 -   
#3 1.00 1.00
#
#P value adjustment method: bonferroni 
#
#[[3]]
#
#   Pairwise comparisons using t tests with pooled SD 
#
#data:  df_1[[nms]] and df_1[["y"]] 
#
#  1    2   
#2 1.00 -   
#3 1.00 0.89
#
#P value adjustment method: bonferroni 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.