在一个数据框中合并具有相同名称的列(包含行中的文本)

时间:2017-09-11 14:35:22

标签: r dataframe merge aggregate

我有一个包含2300+列和300行的数据帧。列以成对命名=




  df<  -  read.csv(file =“RmergeTest.csv”,header = TRUE,sep =“;”,check .names = FALSE)
 df
 SNP_1 SNP_1 SNP_2 SNP_2 SNP_3 SNP_3等......
 1 ATTGG C
 2 ATTGG C
 3 ATTGN N
 4 ATTGG C
  
&#xA ;


我想组合/合并/等每个列对=




  SNP_1 SNP_2 SNP_3
 1 AT TG GC&# xA; 2 AT TG GC
 3 AT TG NN
 4 AT TG GC
  



1 个答案:

答案 0 :(得分:1)

您可以使用Reduce(paste0, ...)基于相似的列名将data.frame拆分为组。然后,您可以将每个子组中的列粘贴到sapply(split.default(df, names(df)), function(a) Reduce(paste0, a)) # SNP_1 SNP_2 SNP_3 #[1,] "AT" "TG" "GC" #[2,] "AT" "TG" "GC" #[3,] "AT" "TG" "NN" #[4,] "AT" "TG" "GC"

df = structure(list(SNP_1 = c("A", "A", "A", "A"), SNP_1 = c("T", 
"T", "T", "T"), SNP_2 = c("T", "T", "T", "T"), SNP_2 = c("G", 
"G", "G", "G"), SNP_3 = c("G", "G", "N", "G"), SNP_3 = c("C", 
"C", "N", "C")), .Names = c("SNP_1", "SNP_1", "SNP_2", "SNP_2", 
"SNP_3", "SNP_3"), class = "data.frame", row.names = c("1", "2", 
"3", "4"))

数据

<div>
    <div class="cusDlg PP_noselect" tabindex="0" style="position: relative; border: 1px solid rgb(192, 192, 192); padding: 5px; background-color: rgb(214, 214, 214); font-family: arial; font-size: 13px; overflow: hidden; width: 400px; height: 125px;">
        <div class="dlghdr" style="position: relative; top: 0px; height: 20px; width: auto; padding: 2px; line-height: 20px;">
            <div style="float: left;">ROW X COLUMN</div>
        </div>
        <div style="position: relative; width: 395px; padding: 2px; background-color: rgb(255, 255, 255); display: table;">
            <div style="width: 30%; float: left; display: table-cell; border-spacing: 2px; border-collapse: separate;">
                <div unselectable="on" index="0" title="0 X 0 - SOME DATA" class="ofd_sel_fnm" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">0 X 0</div>
                <div unselectable="on" index="1" title="0 X 1 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">0 X 1</div>
                <div unselectable="on" index="2" title="0 X 2 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">0 X 2</div>
                <div unselectable="on" index="3" title="0 X 3 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">0 X 3</div>
            </div>
            <div style="width: 30%; float: left; display: table; border-spacing: 2px; border-collapse: separate;">
                <div unselectable="on" index="4" title="1 X 0 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">1 X 0</div>
                <div unselectable="on" index="5" title="1 X 1 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">1 X 1</div>
                <div unselectable="on" index="6" title="1 X 2 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">1 X 2</div>
                <div unselectable="on" index="7" title="1 X 3 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">1 X 3</div>
            </div>
            <div style="width: 30%; float: left; display: table-cell; border-spacing: 2px; border-collapse: separate;">
                <div unselectable="on" index="8" title="2 X 0 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">2 X 0</div>
                <div unselectable="on" index="9" title="2 X 1 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">2 X 1</div>
                <div unselectable="on" index="10" title="2 X 2 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">2 X 2</div>
                <div unselectable="on" index="11" title="2 X 3 - SOME DATA" style="float: left; clear: both; display: table-cell; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; width: 133px;">2 X 3</div>
            </div>
        </div>
    </div>
</div>