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
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