How to uniquely merge two elements of a list in r while eliminating the others -


i have problem done in r:

i have list has elements need matched , merged. example, element 1 of list should matched merged element 25. take vector in 25 , concatenate vector in 1 , delete element 25. elaborate using data below.

   > mydata=read.csv("mydata.csv",h=t)    > mydata             v1    v2      v3       1      1   236    2457       2      1   236  134567       3      1  2457  134567       4      2   136    1457       5      2   136  234567       6      2  1457  234567       7      3   126  123457       8      3   126    4567       9      3  4567  123457       10     4  1257   12346        :     :    :      :       90   136  1457  234567        :     :    :      :       108  236  2457  134567        :     :    :      :       121  567  1246   12357       122 1236 12457   34567       123 1245  3456   12367       124 1247  3467   12356       125 1257  3567   12346 

looking @ data above 1 see row 1 , 2 contain elements of row 108 , row 3 contain elements of row 1and desire combine rows 1,2,3 , 108 together, take unique values , give them name 1 while getting rid of other 3 rows . rows 4,5,6 , 90 should merged , given name 2. how go this? best criteria solve this? love other elements in data. there way go it? not mind arrangement or order of resulting vectors. small example , data quite huge , needs cleaned up.

    dput(mydata)      structure(list(v1 = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,      5, 5, 6, 6, 6, 7, 7, 7, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15,      15, 15, 16, 16, 16, 17, 17, 17, 23, 24, 24, 24, 25, 25, 25, 26,      27, 27, 27, 34, 34, 34, 35, 35, 35, 36, 37, 37, 37, 45, 45, 45,      46, 46, 46, 47, 47, 47, 56, 56, 56, 57, 57, 57, 67, 67, 67, 123,      124, 125, 126, 127, 134, 134, 134, 135, 135, 135, 136, 137, 137,      137, 145, 146, 146, 146, 147, 156, 156, 156, 157, 167, 167, 167,      234, 235, 236, 237, 245, 246, 247, 256, 257, 267, 345, 347, 357,      456, 467, 567, 1236, 1245, 1247, 1257), v2 = c(236, 236, 2457,      136, 136, 1457, 126, 126, 4567, 1257, 1257, 3567, 1247, 1247,      3467, 123, 123, 3457, 1245, 1245, 3456, 36, 36, 457, 26, 26,      14567, 257, 257, 2346, 247, 247, 2356, 23, 23, 13457, 245, 245,      2367, 13457, 157, 157, 1346, 147, 147, 1356, 14567, 145, 145,      1367, 567, 567, 1246, 467, 467, 1256, 457, 456, 456, 1267, 127,      127, 367, 357, 357, 1234, 125, 125, 356, 347, 347, 1235, 124,      124, 346, 345, 345, 1237, 3457, 346, 356, 4567, 367, 246, 246,      1567, 256, 256, 1467, 1457, 267, 267, 1456, 1367, 234, 234, 1357,      1356, 235, 235, 1347, 1346, 237, 237, 1345, 1357, 1347, 2457,      1345, 2367, 1567, 2356, 1467, 2346, 1456, 1237, 1235, 1234, 1267,      1256, 1246, 12457, 3456, 3467, 3567), v3 = c(2457, 134567, 134567,      1457, 234567, 234567, 123457, 4567, 123457, 12346, 3567, 12346,      12356, 3467, 12356, 124567, 3457, 124567, 12367, 3456, 12367,      457, 1234567, 1234567, 23457, 14567, 23457, 2346, 13567, 13567,      2356, 13467, 13467, 24567, 13457, 24567, 2367, 13456, 13456,      24567, 1346, 23567, 23567, 1356, 23467, 23467, 23457, 1367, 23456,      23456, 1246, 12357, 12357, 1256, 12347, 12347, 1234567, 1267,      12345, 12345, 123456, 367, 123456, 1234, 12567, 12567, 123467,      356, 123467, 1235, 12467, 12467, 123567, 346, 123567, 1237, 12456,      12456, 124567, 123567, 123467, 123457, 123456, 2357, 1567, 2357,      2347, 1467, 2347, 234567, 2345, 1456, 2345, 23456, 2567, 1357,      2567, 23467, 2467, 1347, 2467, 23567, 2456, 1345, 2456, 2567,      2467, 134567, 2456, 13456, 2357, 13467, 2347, 13567, 2345, 12456,      12467, 12567, 12345, 12347, 12357, 34567, 12367, 12356, 12346     )), .names = c("v1", "v2", "v3"), row.names = c(na, -125l), class = "data.frame") 

please take consideration there may may rows related single element. rows may or may not duplicated. thank please help. thank you.

i understand data follows:

> dput(dat)  structure(list(abcdd = c("abbccd", "abbcdd", "abccdd", "abc",  "abcdde", "abccd", "accd", "abbcd", "abbd", "abbcc", "abbccdd",  "abbccdee", "abbccde", "abbccdd", "abbdd", "abbc", "abbcd", "abbcdde",  "abbcddee", "abcc", "abccd", "abccdde", "abccddee", "abce", "abcee" ), aabbccd = c("bcdd", "acdd", "abdd", "abcd", "abcddee", "cdd",  "bccd", "bdd", "bccdd", "bcd", "bc", "bcddee", "bcdde", "add",  "accdd", "acd", "ac", "acddee", "acdde", "abd", "ab", "abddee",  "abdde", "abcdee", "abcde"), abbcce = c("abcee", "acce", "abbe",  "abbccde", "abbccee", "acee", "abbcee", "abee", "abccee", "abcddee",  "abcdee", "abce", "abc", "ae", "ace", "accde", "accdde", "accee",  "acc", "abbde", "abbdde", "abbee", "abb", "abbccdee", "abbccd" ), aabcee = c("bcee", "abcce", "abbce", "abbccdde", "abbcc",  "bcce", "cee", "bccee", "bee", "bcdee", "bcddee", "bc", "bce",  "abce", "abe", "abccdde", "abccde", "abcc", "abccee", "abbcdde",  "abbcde", "abbc", "abbcee", "abbccdd", "abbccddee"), adee = c("adde",  "abdee", "acdee", "addee", "ad", "abbdde", "abdde", "accdde",  "acdde", "ade", "ae", "add", "addee", "abcdee", "abccdee", "abddee",  "abee", "abd", "abde", "acddee", "acee", "acd", "acde", "add",  "adde"), aadde = c("dee", "abbdee", "accdee", "aee", "ade", "bdde",  "bdee", "cdde", "cdee", "e", "de", "de", "d", "abbccdee", "abbcdee",  "abbee", "abbddee", "abbde", "abbd", "accee", "accddee", "accde",  "accd", "ae", "a"), bcde = c("abcde", "bccddee", "bccde", "bcdde",  "bcdee", "abbcde", "acde", "abccde", "abde", "abcdde", "abce",  "abcdee", "abcd", "bcddee", "bddee", "bccdee", "bccee", "bccdde",  "bccdd", "bccdde", "bcce", "bccdee", "bccd", "bcddee", "bcdd" ), bbccdde = c("abbccddee", "cde", "bde", "bce", "bcd", "accddee",  "abccddee", "abbddee", "abbcddee", "abbccee", "abbccdee", "abbccdd",  "abbccdde", "de", "cddee", "ce", "cdde", "cd", "cdee", "be",  "bdde", "bd", "bdee", "bc", "bcee")), .names = c("abcdd", "aabbccd",  "abbcce", "aabcee", "adee", "aadde", "bcde", "bbccdde"), class = "data.frame", row.names = c("a",  "b", "c", "d", "e", "ab", "abb", "ac", "acc", "ad", "add", "ae",  "aee", "bc", "bcc", "bd", "bdd", "be", "bee", "cd", "cdd", "ce",  "cee", "de", "dee")) 

what assume row names meaningful, column names not. therefore, suggest:

library(tidyverse) dat %>% rownames_to_column() %>%           # convert row names separate column,  t() %>% as_data_frame() %>% as.list() %>% # split data frame row list,  map(sort) %>%                             # sort each element of list,  do.call(rbind, .) %>% as_data_frame() %>% # reassemble list data frame (by row), ,    distinct()                                # eliminate duplicates 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -