一般来说R和编码都很新,所以有一些严重的问题! 我有一个包含来自GWAS的1,887,181个条目的数据框。我正在尝试使用qqman包来创建曼哈顿情节。我遇到的问题是,创建BP列所需的数据是以混合字符串格式给出的,其中染色体编号位于ID列中。对于顶部相关的SNP(其中只有45个条目),我一直在使用as.numeric_version()来从ID列中提取数字部分,但这绝对是一种不正确的方法,因为它给出以下错误,但仍然输出正确的值:
BP<-as.numeric_version(PDLS_top_mp$CHR)
Error: invalid version specification ‘2R_19509740_SNP’,
‘2R_19509741_SNP’, ‘2R_15116838_SNP’, ‘3R_10159602_SNP’,
‘2L_2855787_INS’, ‘X_11169099_SNP’, ‘2L_2855805_SNP’,
‘3R_19567990_SNP’, ‘2R_19751690_SNP’, ‘2L_17155446_INS’,
‘3R_4610617_SNP’, ‘3L_1114964_SNP’, ‘2L_17155370_SNP’,
‘3L_3548725_SNP’, ‘2R_16873693_SNP’, ‘2R_15527873_SNP’,
‘3L_3548727_SNP’, ‘2L_2855816_SNP’, ‘3R_19576606_SNP’,
‘3L_11508729_DEL’, ‘2L_17155422_SNP’, ‘3R_22503130_INS’,
‘3R_4610630_SNP’, ‘2R_12331096_SNP’, ‘3R_8777795_SNP’,
‘3L_10615500_SNP’, ‘X_17577589_SNP’, ‘X_17577622_SNP’,
‘2L_7047925_SNP’, ‘2L_7047917_SNP’, ‘3R_9564478_SNP’, ‘3R_9590636_SNP’,
‘3R_3614137_SNP’, ‘2R_4682340_INS’, ‘3L_6056407_SNP’,
‘3L_11503648_SNP’, ‘3R_9580772_SNP’, ‘2L_18252469_SNP’,
‘2L_7047939_SNP’, ‘3R_25530801_DEL’, ‘3L_1111454_SNP’,
‘2L_17324557_SNP’, ‘3L_9799858_SNP�
我尝试设置正则表达式并使用以下代码替换值,但也无效:
regexp<-"_[[:digit:]]_+"
s<-str_extract(PDLS_top_mp$ID,regexp)
View(s)
Error in View : 'names' attribute [2] must be the same length as the vector [1]
as.numeric_version()不适用于包含所有关联SNP的非常大的数据帧,它只为每个条目返回NA。以下是所有相关的SNP数据框的样子:
print(head(PDLS_ALL_dtf))
# A tibble: 6 × 3
ID SinglePval SingleMixedPval
<chr> <dbl> <dbl>
2L_5372_SNP 0.6712 7.328082
2L_5390_SNP 0.8176 8.459388
2L_5403_SNP 0.9967 8.304397
2L_5465_SNP 0.7897 6.187142
2L_5516_SNP 0.4967 5.223919
2L_5598_SNP 0.7099 5.169412
我希望它最终像这样:(其中有5种不同的染色体ID,X,2L,2R,3L,3R和4都是上述格式)
ID BP SinglePval SingleMixedPval
<chr> <num> <dbl> <dbl>
1 2L 5372 0.6712 7.328082
2 2L 5390 0.8176 8.459388
3 2L 5403 0.9967 8.304397
4 2L 5465 0.7897 6.187142
5 2L 5516 0.4967 5.223919
6 2L 5598 0.7099 5.169412
任何帮助都会非常感激!
答案 0 :(得分:1)
strsplit
仅用于此目的
ID = sapply(strsplit(PDLS_ALL_dtf$ID, "_"), function(x) x[[1]])
ID
[1] "2L" "2L" "2L" "2L" "2L" "2L"
BP = sapply(strsplit(PDLS_ALL_dtf$ID, "_"), function(x) x[[2]])
BP
[1] "5372" "5390" "5403" "5465" "5516" "5598"
将它们放在一个数据框中:
PDLS_ALL_dtf$BP = sapply(strsplit(PDLS_ALL_dtf$ID, "_"), function(x) x[[2]])
PDLS_ALL_dtf$ID = sapply(strsplit(PDLS_ALL_dtf$ID, "_"), function(x) x[[1]])
PDLS_ALL_dtf = PDLS_ALL_dtf[,c(1,4,2,3)]
PDLS_ALL_dtf
ID BP SinglePval SingleMixedPval
1 2L 5372 0.6712 7.328082
2 2L 5390 0.8176 8.459388
3 2L 5403 0.9967 8.304397
4 2L 5465 0.7897 6.187142
5 2L 5516 0.4967 5.223919
6 2L 5598 0.7099 5.169412
答案 1 :(得分:1)
可以使用dplyr / tidyr:
df_new<-df%>%separate(ID,into=c("ID","BP"),sep="_")
注意:由于第二个下划线(_SNP),会出现警告。但我猜这种情况并不重要。
Warning message:
Too many values at 7 locations: 1, 2, 3, 4, 5, 6, 7
如果你想保留它:
df_new<-df%>%separate(ID,into=c("ID","BP"),extra="merge")
或放弃它:
df_new<-df%>%separate(ID,into=c("ID","BP"),extra="drop")
df_new
ID BP SinglePval SingleMixedPval
1 2L 5372 0.6712 7.328082
2 2L 5390 0.8176 8.459388
3 2L 5403 0.9967 8.304397
4 2L 5465 0.7897 6.187142
5 2L 5516 0.4967 5.223919
6 2L 5598 0.7099 5.169412
7 2L 5598 0.7099 5.169412
答案 2 :(得分:1)
以下是使用extract
library(tidyverse)
PDLS_ALL_dtf %>%
extract(ID, into = c("ID", "BP"), "^([^_]+)_([^_]+).*")
# ID BP SinglePval SingleMixedPval
#1 2L 5372 0.6712 7.328082
#2 2L 5390 0.8176 8.459388
#3 2L 5403 0.9967 8.304397
#4 2L 5465 0.7897 6.187142
#5 2L 5516 0.4967 5.223919
#6 2L 5598 0.7099 5.169412