Reputation: 606
I have a test_list
test_list <- list("hg38:Chr12:8823762", "hg38:Chr10:50814012", "hg19:Chr12:8990070",
"hg38:chr1:16949", "hg38:chr9:342484")
and I want to check if each element in my list partially match my column Extra_information
in df
df <- structure(list(Extra_information = c("hg38:Chr10:50814012, hg19:Chr10:52573772, CpG:Mutation may have occured by deamination of methylated CpG dinucleotide",
"hg38:Chr12:8822661, hg19:Chr12:8975257, COM:Patient is homozygous for c.706C>G p.Leu236Val in SLC26A4., dbSNP:http://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?type=rs&rs=rs1409944554",
"hg38:Chr12:8823729, hg19:Chr12:8976325, COM:Variant of unknown significance. Clinical features descr. in supplementary table 2. functional study., dbSNP:http://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?type=rs&rs=rs766201825",
"hg38:Chr12:8823762, hg19:Chr12:8976358, COM:VUS Table 2. RIT1 variant also present.",
"hg38:Chr12:8835642, hg19:Chr12:8988238, COM:VUS Table 2. SOS1 and CBL variants also present., dbSNP:http://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?type=rs&rs=rs11047499",
"hg38:Chr12:8837474, hg19:Chr12:8990070, dbSNP:http://www.ncbi.nlm.nih.gov/SNP/snp_ref.cgi?type=rs&rs=rs863224952"
)), row.names = c(NA, 6L), class = "data.frame")
to obtain a dataframe of my list with the values 1
for TRUE
and 0
for FALSE
:
test_df <- structure(list(Entries = c("hg38:Chr12:8823762", "hg38:Chr10:50814012", "hg19:Chr12:8990070",
"hg38:chr1:16949", "hg38:chr9:342484"), Values = c(1,1,1,0,0)), row.names = c(NA, 5L), class = "data.frame"))
How can I achieve the desired output ?
Thanks in advance.
Upvotes: 0
Views: 45
Reputation: 24790
Here's a base R approach.
data.frame(Entries = unlist(test_list),
Values = sapply(test_list,function(x){
as.numeric(length(grep(x,df$Extra_information)) > 0)
}))
# Entries Values
#1 hg38:Chr12:8823762 1
#2 hg38:Chr10:50814012 1
#3 hg19:Chr12:8990070 1
#4 hg38:chr1:16949 0
#5 hg38:chr9:342484 0
Upvotes: 2
Reputation: 887078
We could use agrepl
to check for partial matches between the 'test_list' elements and the 'Extra_information' (from base R
only)
Values <- +(sapply(test_list, function(x) any(agrepl(x, df$Extra_information))))
data.frame(Entries = unlist(test_list), Values)
# Entries Values
#1 hg38:Chr12:8823762 1
#2 hg38:Chr10:50814012 1
#3 hg19:Chr12:8990070 1
#4 hg38:chr1:16949 0
#5 hg38:chr9:342484 0
Upvotes: 1