merge (opposite of split) pair of rows in r

前端 未结 1 681
醉酒成梦
醉酒成梦 2021-02-06 08:39

I have the column like the following. Each column has two pairs each with suffix \"a\" and \"b\" - for example col1a, col1b, colNa, colNb and so on till end of the file (> 50000

1条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 09:38

    mydataf
      Ind col1a col1b colNa colNb K_a K_b
    1   1     2     1     1     1   A   A
    2   2     1     2     1     3   B   A
    3   3     1     2     3     2   A   A
    4   4     1     2     3     1   A   B
    5   5     1     2     2     1   A   A
    n <- names(mydataf)
    nm <- c("Ind", unique(substr(n, 1, nchar(n)-1)[-1]))
    df <- data.frame(sapply(nm, function(x){
                                 idx <- grep(paste0(x, "[ab]?$"), n)
                                 cols <- mydataf[idx]
                                 x <- apply(cols, 1, 
                                           function(z) paste(sort(z), collapse = ""))
                                 return(x)
                                }))
    names(df) <- nm
    df
      Ind col1 colN K_
    1   1   12   11 AA
    2   2   12   13 AB
    3   3   12   23 AA
    4   4   12   13 AB
    5   5   12   12 AA
    

    0 讨论(0)
提交回复
热议问题