user3339391
user3339391

Reputation: 11

print column names from a data frame using for loop

I have two data frames (say frame1 and frame2). Each has a large number of variables (say frame1 has variables a1, a2, a3…and frame 2 has variables b1, b2, b3….). I am doing chi square test of independence between all possible pairs of variables from the two frames (so between a1 and b1; a1 and b2; a1 and b3,….etc.). I am using the following for loop to do this which works out perfectly EXCEPT in the output all I get is the results without specifying which analysis belongs to which sets of variables. I would like to have the results in a way so that each INDIVIDUAL analysis is preceded by the two variable names that the analysis was for.

for (i in frame1) {
for (j in frame2) 
print({chisq.test(table(i,j))})}

R is pretty new to me so any help would be appreciated!!

Upvotes: 1

Views: 8850

Answers (2)

Thomas
Thomas

Reputation: 44565

You don't need a loop for this and it is probably far more convenient to store the result in a list. Here's an example with some fake data:

frame1 <- data.frame(a1=rnorm(10), a2=rnorm(10), a3=rnorm(10), a4=rnorm(10))
frame2 <- data.frame(b1=rnorm(10), b2=rnorm(10), b3=rnorm(10), b4=rnorm(10))

# setup all column combinations
cols <- expand.grid(1:ncol(frame1), 1:ncol(frame2))

# output to named list
setNames(apply(cols, 1, function(x) {
    chisq.test(table(frame1[,x[1]], frame2[,x[2]]))
}), paste(names(frame1)[cols[,1]], names(frame2)[cols[,2]], sep=':'))

The (first few entries of the) result:

$`a1:b1`

        Pearson's Chi-squared test

data:  table(frame1[, x[1]], frame2[, x[2]])
X-squared = 90, df = 81, p-value = 0.2313


$`a2:b1`

        Pearson's Chi-squared test

data:  table(frame1[, x[1]], frame2[, x[2]])
X-squared = 90, df = 81, p-value = 0.2313


$`a3:b1`

        Pearson's Chi-squared test

data:  table(frame1[, x[1]], frame2[, x[2]])
X-squared = 90, df = 81, p-value = 0.2313


$`a4:b1`

        Pearson's Chi-squared test

data:  table(frame1[, x[1]], frame2[, x[2]])
X-squared = 90, df = 81, p-value = 0.2313


$`a1:b2`

        Pearson's Chi-squared test

data:  table(frame1[, x[1]], frame2[, x[2]])
X-squared = 90, df = 81, p-value = 0.2313

...

Upvotes: 2

Feng Mai
Feng Mai

Reputation: 3109

Can you do something like this?

for (i in 1:ncol(frame1)) {
  for (j in 1:ncol(frame2)) {
    print(colnames(frame1)[i])
    print(colnames(frame2)[j])
    print({chisq.test(table(frame1[,i],frame2[,j]))})
  }
}

Upvotes: 1

Related Questions