[R] looping through rows of a logical matrix for combination with and testing of a numeric vector

debra ragland ragland.debra at yahoo.com
Thu Dec 3 16:28:58 CET 2015


I have read in a sequence alignment and have done the necessary steps to separate and store the elements of the original input into a new list of character vectors. I have compared the sequence list to a "standard" vector, such that the return is a matrix of logical values indicating TRUE if there is a match to the standard and FALSE where there is no match.

An example;

mylist=c("AAEBCC", "AABDCC", "AABBCD")
list.2 <- strsplit(mylist, split=NULL)
# setting a standard for comparison
std.string <- "AABBCC"
standard <- unlist(strsplit(std.string, split=NULL))
#create a logical matrix 
mat<-sapply(list.2, function(x) x==standard)
>mat


[,1]  [,2]  [,3]
[1,]  TRUE  TRUE  TRUE
[2,]  TRUE  TRUE  TRUE
[3,] FALSE  TRUE  TRUE
[4,]  TRUE FALSE  TRUE
[5,]  TRUE  TRUE  TRUE
[6,]  TRUE  TRUE FALSE

Where the number of columns is the same length as the original input strings I compared (15) and the number of rows corresponds is the same as the number of strings from the input (99).

I also have a named numeric vector(of length 15)--where the "names" of the the values match those of the columns of the logical matrix. For the example

x2 = runif(3, 5.0, 7.5)
names(x2) = 1:3
> x2 
1        2        3 
5.352611 7.058169 6.993105

For each row in the in the logical matrix I want to combine the logical values with the values from the numeric vector so that I can run a wilcox.test using those values that are "TRUE" against those that are "FALSE".

For instance if each row&vector pairing was a mini data.frame it would look like

df=data.frame(x2, mat[3,])
>df
1 5.352611 FALSE
2 7.058169 TRUE
3 6.993105 TRUE
wilcox.test(df) #based on all true values vs. all false values

How can this be achieved?



More information about the R-help mailing list