I have a data frame which is like this(4 rows and 5 column):
Marker ind1 ind2 ind3 ind4
mark1 CT TT CT TT
mark2 AG AA AG AA
mark3 AC AA AC AA
mark4 CT TT CT TT
what I want to do is to split each of the columns (except first coloumn) to two column. so the output should be like this (4 rows and 9 column):
Marker ind1 ind1 ind2 ind2 ind3 ind3 ind4 ind4
mark1 C T T T C T T T
mark2 A G A A A G A A
mark3 A C A A A C A A
mark4 C T T T C T T T
I know how to split one column
do.call(rbind,strsplit(test$JRP4RA6119.039, ""))
which gives this:
[,1] [,2]
[1,] "C" "T"
[2,] "A" "G"
[3,] "A" "C"
[4,] "C" "T"
what I would like is to be able to loop this and make it for all columns in one dataframe.
Thanks in advance.
I've got the feeling that it is a bit far-fetched but:
test_split <- data.frame(Marker=test$Marker,
do.call("cbind", lapply(apply(test[, -1], 2, strsplit, ""),
function(x) do.call("rbind", x))),
stringsAsFactors=F)
colnames(test_split)[-1] <- paste(rep(colnames(test)[-1], e=2), 1:2, sep="_")
test_split
# Marker JRP4RA6119.039_1 JRP4RA6119.039_2 JRP4RA6124.029_1 JRP4RA6124.029_2 JRP4RA6133.051_1 JRP4RA6133.051_2 JRP4RA6125.009_1 JRP4RA6125.009_2
#1 s7e4419xxx C T T T C T T T
#2 s7e7001s01 A G A A A G A A
#3 s7e3049xxx A C A A A C A A
#4 s7e4727xxx C T T T C T T T
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With