Economist_Ayahuasca
Economist_Ayahuasca

Reputation: 1642

Loop within a loop with column names in R

I have the following data:

id A B C
1  1 1 0
2  1 1 1  
3  0 1 1 

I will like to create a function that computes the following three information between columns: the number of individuals i) with A and B, ii) with A but not B, iii) B but not A. Similarly, I will like a recursive loop that computes these three numbers for A and C, and B and C. Is there a smart way to do so? a loop within a loop? So far, I have tried the following:

for(ii in colnames(df)){
for(jj in (ii+1):df){
print(ii,jj)
}}

Upvotes: 0

Views: 28

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145755

Perhaps something like this:

# function to return your metrics
foo = function(x, y) {
  c(
    "x and y" = sum(x & y),
    "x not y" = sum(x & !y),
    "y not x" = sum(!x & y)
  )
}


# generate combinations of columns
col_combos = combn(names(df)[-1], 2)

result = apply(col_combos, 2, function(x) foo(df[[x[1]]], df[[x[2]]]))
colnames(result) = apply(col_combos, 2, toString)
result
#         A, B A, C B, C
# x and y    2    1    2
# x not y    0    1    1
# y not x    1    1    0

Using this data:

df = read.table(text = 'id A B C
1  1 1 0
2  1 1 1  
3  0 1 1 ', header = TRUE)

Upvotes: 1

Related Questions