拆分R中的数据帧并翻转不同的地方?

时间:2017-12-10 08:51:21

标签: r dataframe split

我有一个数据框:

access-token

我已将其拆分为" /":

rs1065852 rs201377835 rs28371706 rs5030655 rs5030865 rs3892097 rs35742686 rs5030656 rs5030867 rs28371725 rs59421388
A/del     C/del       G/del      A/del     C/del     T/del     T/del    CTT/del     T/del      C/del      C/del

我想翻转值不等于(我需要将它们与特定值模式匹配)。

例如,如果我得到A / G,我希望像我一样将其拆分为A G,将订单更改为G / A并将其拆分为G A.

1 个答案:

答案 0 :(得分:0)

如果我正确理解了这个问题,那么

#sample data (disclaimer - I have added two more columns at the end!)
df <- read.table(text="rs1065852 rs201377835 rs28371706 rs5030655 rs5030865 rs3892097 rs35742686 rs5030656 rs5030867 rs28371725 rs59421388 rs87654321 rs12345678
A/del     C/del       G/del      A/del     C/del     T/del     T/del    CTT/del     T/del      C/del      C/del      A/G      G/A", header=T)

#step to make A/G & G/A same 
df1 <- as.data.frame(t(apply(df, 2, function(x) paste(sort(unlist(strsplit(x, "/"))), collapse = "/"))))    
#similarity matrix to identify which all columns are same
m <- sapply(df1, function(x) sapply(df1, function(y) sum(as.character(x)==as.character(y))))
m

输出是:

            rs1065852 rs201377835 rs28371706 rs5030655 rs5030865 rs3892097 rs35742686 rs5030656 rs5030867
rs1065852           1           0          0         1         0         0          0         0         0
rs201377835         0           1          0         0         1         0          0         0         0
rs28371706          0           0          1         0         0         0          0         0         0
rs5030655           1           0          0         1         0         0          0         0         0
rs5030865           0           1          0         0         1         0          0         0         0
rs3892097           0           0          0         0         0         1          1         0         1
rs35742686          0           0          0         0         0         1          1         0         1
rs5030656           0           0          0         0         0         0          0         1         0
rs5030867           0           0          0         0         0         1          1         0         1
rs28371725          0           1          0         0         1         0          0         0         0
rs59421388          0           1          0         0         1         0          0         0         0
rs87654321          0           0          0         0         0         0          0         0         0
rs12345678          0           0          0         0         0         0          0         0         0
            rs28371725 rs59421388 rs87654321 rs12345678
rs1065852            0          0          0          0
rs201377835          1          1          0          0
rs28371706           0          0          0          0
rs5030655            0          0          0          0
rs5030865            1          1          0          0
rs3892097            0          0          0          0
rs35742686           0          0          0          0
rs5030656            0          0          0          0
rs5030867            0          0          0          0
rs28371725           1          1          0          0
rs59421388           1          1          0          0
rs87654321           0          0          1          1
rs12345678           0          0          1          1