debra ragland
2015-Dec-03 15:28 UTC
[R] looping through rows of a logical matrix for combination with and testing of a numeric vector
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> x21 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,])>df1 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?
Boris Steipe
2015-Dec-03 16:04 UTC
[R] looping through rows of a logical matrix for combination with and testing of a numeric vector
Use your logical vector to extract the x, y values for the test from the rows of the matrix: x <- mat[3, x2] y <- mat[3, !x2] Or: use the formula version of wilcox.test as explained in ?wilcox.test B. On Dec 3, 2015, at 10:28 AM, debra ragland via R-help <r-help at r-project.org> wrote:> 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? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
debra ragland
2015-Dec-03 16:43 UTC
[R] looping through rows of a logical matrix for combination with and testing of a numeric vector
Thanks again! And no Bert, this is not homework. I have a very minimal background in R and struggle with putting concepts together. But thanks anyway. On Thursday, December 3, 2015 11:04 AM, Boris Steipe <boris.steipe at utoronto.ca> wrote: Use your logical vector to extract the x, y values for the test from the rows of the matrix: x <- mat[3, x2] y <- mat[3, !x2] Or: use the formula version of wilcox.test as explained in ?wilcox.test B. On Dec 3, 2015, at 10:28 AM, debra ragland via R-help <r-help at r-project.org> wrote:> 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? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.