[R] Avoiding slow for-loops (once again)

Kulupp kulupp at online.de
Mon Aug 5 08:39:33 CEST 2013


# Dear R-experts,

# (Once again) I want to avoid the usage of for-loops but unfortunately 
I don't know how.
# I know functions like e.g. 'apply' but don't know how to use them for 
the solution of the following problem:
# I have a data frame 'a' giving the number of columns in data frame 'b' 
that belong to one group
a <- data.frame(group1=5, group2=4)

b <- data.frame(col1=c(0,0,0), col2=c(0,1,0.5), col3=c(0,0,0), 
col4=c(1/3,0,0.5), col5=c(2/3,0,0),
                 col6=c(0,0,0), col7=c(1,1/3,0), col8=c(0,2/3,0), 
col9=c(0,0,0))

# ... thus columns 1-5 in 'b' belong to group 1 and columns 6-9 in 'b' 
belong to group 2

# then I created a data frame giving all possible row combinations of 'b'
r <- as.data.frame(t(combn(nrow(b), 2)))

# .. so e.g. the second row of 'r' tells me that I have to perform an 
equation with the values of the
# first and third row of table 'b'. The equation has to be calculated 
for each group seperately.
# e.g. within group 2 (columns 6-9 in 'b') I have to calculate e.g. for 
rows 1 and 3 in 'b'
#      (abs(b[row1,col6] - b[row3, col6]) + abs(b[row1, col7] - b[row3, 
col7]) + .... + abs(b[row1, col9] - b[row3, col9]))/2

# the resulting data frame shall look as follows:
result <- cbind(r, data.frame(group1=c(1,2/3,0.5), group2=c(2/3,0.5,0.5)))

# The original tables are much larger and I don't know how to solve this 
problem w/o a lot of very slow for-loops.
# Is there any possible solution w/o using 'for'-loops?

# I'd be happy for any suggestions
# Thank you very much in anticipation
# Best regards
# Thomas



More information about the R-help mailing list