R, How to extract blocks of values from correlation similarity matrix? -
i have correlation similarity data frame, example 1 shown below:
1 2 3 4 1 1.0000000 0.9991846 0.7648333 0.3517951 2 0.9991846 1.0000000 0.7563048 0.3569311 3 0.7648333 0.7563048 1.0000000 0.6568740 4 0.3517951 0.3569311 0.6568740 1.0000000 i'd extract clusters of values above threshold (0.95,in case). i'd have following returned each 1 separate data frames:
1 2 1 1.0000000 0.9991846 2 0.9991846 1.0000000 3 3 1.0000000 4 4 1.0000000 i have thought subsetting or using logical operators, such as:
subset(blah, blah[1,] >.95) blah > .95 which think right direction, i'm kind of stumped how approach problem? appreciated?
here solution no looping:
mytabletxt <- " 1 2 3 4 1 1.0000000 0.9991846 0.7648333 0.3517951 2 0.9991846 1.0000000 0.7563048 0.3569311 3 0.7648333 0.7563048 1.0000000 0.6568740 4 0.3517951 0.3569311 0.6568740 1.0000000" mytable <- read.table(textconnection(mytabletxt), header = true, row.names = 1) mytable <- mytable[mytable > .95] newbloc <- mytable==1 & c(1, mytable[-length(mytable)])==1 blocid <- rep(1:sum(newbloc), c(which(newbloc), length(newbloc) + 1)[-1] - which(newbloc)) blocsplit <- split(mytable, factor(blocid)) lapply(blocsplit, function(x) tmp <- as.data.frame(matrix(x, ncol = max(c(1, length(x)/2))))) # $`1` # v1 v2 # 1 1.0000000 0.9991846 # 2 0.9991846 1.0000000 # # $`2` # v1 # 1 1 # # $`3` # v1 # 1 1
Comments
Post a Comment