BioMan
BioMan

Reputation: 704

Split string and create two new columns

I would like to split the column miRNAs into two columns based on the |symbol. I tried this: split <- unlist(strsplit(as.character(fitted.values.plot),'|'))

head(fitted.values.plot)
                      miRNAs               100              106               122              124               126
1 hsa-let-7a-5p|hsa-let-7a-1 0.689673028877691 2.05061067282612  1.05656799134149 1.75048593733063 0.310608256464213
2 hsa-let-7a-5p|hsa-let-7a-2 0.689964636197034 2.05147771134477  1.05701472906612 1.75122607720905 0.310739587743298
3 hsa-let-7a-5p|hsa-let-7a-3 0.689420828637648 2.04986080371093  1.05618162462874 1.74984581809282 0.310494672963684
4   hsa-let-7b-5p|hsa-let-7b 0.819027066280732 2.43522013059115  1.25473629682568  2.0788044504954  0.36886547451107
5   hsa-let-7c-5p|hsa-let-7c  1.71613593527086 5.10260154817646  2.62909265996488 4.35579136120192 0.772896674787318
6   hsa-let-7d-5p|hsa-let-7d 0.581521608151111 1.72904313525816 0.890881753699624 1.47598261016372 0.261900067482724

Upvotes: 1

Views: 60

Answers (2)

Victorp
Victorp

Reputation: 13866

You can do this with tidyr too :

library("tidyr")
library("dplyr") # for pipe operator %>%
fitted.values.plot %>% separate(col = miRNAs, into = c("miRNAs_1", "miRNAs_2"), sep = "\\|")
# otherwise
separate(data = fitted.values.plot, col = miRNAs, into = c("miRNAs_1", "miRNAs_2"), sep = "\\|") 
##       miRNAs_1     miRNAs_2      X100     X106      X122     X124      X126
##1 hsa-let-7a-5p hsa-let-7a-1 0.6896730 2.050611 1.0565680 1.750486 0.3106083
##2 hsa-let-7a-5p hsa-let-7a-2 0.6899646 2.051478 1.0570147 1.751226 0.3107396
##3 hsa-let-7a-5p hsa-let-7a-3 0.6894208 2.049861 1.0561816 1.749846 0.3104947
##4 hsa-let-7b-5p   hsa-let-7b 0.8190271 2.435220 1.2547363 2.078804 0.3688655
##5 hsa-let-7c-5p   hsa-let-7c 1.7161359 5.102602 2.6290927 4.355791 0.7728967
##6 hsa-let-7d-5p   hsa-let-7d 0.5815216 1.729043 0.8908818 1.475983 0.2619001

You need to escape | with \\| because separate use regular expression

Upvotes: 1

user227710
user227710

Reputation: 3194

library(splitstackshape)
cSplit(fitted.values.plot,"miRNAs",sep="|")

Upvotes: 2

Related Questions