拆分字符串并创建两个新列

时间:2015-07-10 15:46:52

标签: r

我想根据miRNAs符号将列|拆分为两列。 我试过这个: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

2 个答案:

答案 0 :(得分:2)

library(data.table)
x <- 1:10
address(x)
# [1] "0x713cfd0"
setattr(x,"names",letters[1:10])
address(x)
# [1] "0x713cfd0"

答案 1 :(得分:1)

您也可以使用tidyr执行此操作:

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

您需要使用|转义\\|因为separate使用正则表达式