比较2个字符并分配给新列

时间:2013-07-12 05:14:34

标签: r

我有一个数据列表,如下所示:

ID    col1    col1.1    col2    col2.1    col3    col3.1
rat    AG       AB       AG       AC       AA       AA
cat    BB       GG       BB       CC       AB       AG
dog    --       AB       AG       GG       CC       GG

我想比较两列中的每一列(即col1与col1.1,col2与col2.1 ......),如果它们符合条件,则会将结果添加到新列中。

所以我们说条件是这样的:

if any base of one column matched with -- of another column, assign 0
if AG or AC of one column matched with AB of another column, assign 1
if AA of one column matched with AA of another column, assign 2
if BB of one column matched with GG or CC of another column, assign 3
if one does not match any of the condition above, assign 4

这样输出如下:

ID    col1    col1.1  OUT1  col2    col2.1  OUT2  col3  col3.1   OUT3  
rat    AG       AB     1     AG       AC     1     AA     AA      2
cat    BB       GG     3     BB       CC     3     AB     AG      1
dog    --       AB     0     AG       GG     4     BB     GG      3

如何在两个字符串之间进行比较并为其添加新列?

谢谢!

1 个答案:

答案 0 :(得分:2)

这可以让你通过一些重新安排得到你需要的东西:

fnpair <- function(a) { if( a[1] =="--" | a[2]=="--"){0}else{
                  if( (a[1] %in% c("AG", "AC") & a[2] == "AB")|
                      (a[2] %in% c("AG", "AC") & a[1] == "AB") ){1}else{
                     if(  a[1] =="AA" & a[2] == "AA" ){2} else{
                        if(  (a[1] %in% c("GG","CC") & a[2] == "BB")|
                             (a[2] %in% c("GG","CC") & a[1] == "BB") ) {3} else{4} }}}}

 df1 <- read.table(text="ID    col1    col1.1    col2    col2.1    col3    col3.1
 rat    AG       AB       AG       AC       AA       AA
 cat    BB       GG       BB       CC       AB       AG
 dog    --       AB       AG       GG       CC       GG", header=TRUE)

 t( apply(df1[,2:7], 1, function(x) t( sapply(0:2, function(z) fnpair(x[2*z+c(1,2)]) ) ) ) )

#------------------

     [,1] [,2] [,3]
[1,]    1    4    2
[2,]    3    3    1
[3,]    0    4    4

仅分配给新列:

df1$newcol <- .Last.value

我认为您展示的手牌计算不符合规则。