loops - referencing a list of variables to do calculations with in r -
loops - referencing a list of variables to do calculations with in r -
i trying calculate number of pairwise differences between strings of 5 variables s1 vs s3, s1 vs s4, s1 vs s5, s2 vs s3, s2 vs s4, s2 vs s5
s1 <- unlist(strsplit('tgtagcaacggattccacccatatgggctcagcaggcaggtcctggattgtttgct','')) s2 <- unlist(strsplit('tgtaacaacgaattccatccatatgggctcagcaggcaggtcctggactatttgct','')) s3 <- unlist(strsplit('tgtaacgatgggtcctgcttacgcgggttcagtaggtaaattctaggccgcctatt','')) s4 <- unlist(strsplit('tgtaacaacggattccacccatatgggctcagcaggcaggtcctggactgtttgct','')) s5 <- unlist(strsplit('tgtaacgacgggccctgcttacgcgggttcagtaggtaaattctgggccgcttatt','')) for(i in 1:2) { for(j in 3:5){ diff(i.j) <- sum(mapply(function(x,y) sum(x!=y),s(i),s(j))) } }
so keeping variables , style, think want this:
s1 <- unlist(strsplit('tgtagcaacggattccacccatatgggctcagcaggcaggtcctggattgtttgct','')) s2 <- unlist(strsplit('tgtaacaacgaattccatccatatgggctcagcaggcaggtcctggactatttgct','')) s3 <- unlist(strsplit('tgtaacgatgggtcctgcttacgcgggttcagtaggtaaattctaggccgcctatt','')) s4 <- unlist(strsplit('tgtaacaacggattccacccatatgggctcagcaggcaggtcctggactgtttgct','')) s5 <- unlist(strsplit('tgtaacgacgggccctgcttacgcgggttcagtaggtaaattctgggccgcttatt','')) strlist <- rbind(s1,s2,s3,s4,s5) diff <- matrix(nrow=2, ncol=3) for(i in 1:2) { for(j in 1:3) { diff[i,j] <- sum(strlist[i,] != strlist[j+2,]) } }
r loops
Comments
Post a Comment