Question

I have two variables with names stored in them. I want to see how many names in variable ScanName are in vector B while ignoring the capitals. Also, what are the differences? I want to ignore the difference between a capital letter in the search (for example it should consider hsa-mir-1 and hsa-miR-1 as the same). My data are like this :

 str(B)
 Factor w/ 1046 levels "hsa-let-7a-1",..: 1 2 3 4 5 6 7 8 9 10 ...

>B
   [1] hsa-let-7a-1     hsa-let-7a-2     hsa-let-7a-3     hsa-let-7b       hsa-let-7c       hsa-let-7d      
   [7] hsa-let-7e       hsa-let-7f-1     hsa-let-7f-2     hsa-let-7g       hsa-let-7i       hsa-mir-1-1     
  [13] hsa-miR-1238     hsa-mir-100      hsa-mir-101-1    hsa-mir-101-2    hsa-mir-103-1    hsa-mir-103-1-as
  [19] hsa-mir-103-2    hsa-mir-103-2-as hsa-mir-105-1    hsa-mir-105-2    hsa-mir-106a     hsa-mir-106b    

and

> str(ScanName)
 chr [1:1146] "hsa-miR-103b" "hsa-miR-1178" "hsa-miR-1179" "hsa-miR-1180" "hsa-miR-1181
> ScanName
   [1] "hsa-miR-103b"     "hsa-miR-1178"     "hsa-miR-1179"     "hsa-miR-1180"     "hsa-miR-1181"     "hsa-miR-1182"    
   [7] "hsa-miR-1183"     "hsa-miR-1184"     "hsa-miR-1193"     "hsa-miR-1197"     "hsa-miR-1200"     "hsa-miR-1203"    
  [13] "hsa-miR-1204"     "hsa-miR-1205"     "hsa-miR-1206"     "hsa-miR-1208"     "hsa-miR-1224-3p"  "hsa-miR-1225-3p" 
  [19] "hsa-miR-1225-5p"  "hsa-miR-1227"     "hsa-miR-1228"     "hsa-miR-1229"     "hsa-miR-1231"     "hsa-miR-1233"    
  [25] "hsa-miR-1234"     "hsa-let-7a-2"     "hsa-miR-1238"     "hsa-miR-1243"     "hsa-miR-1244"     "hsa-miR-1245"    
  [31] "hsa-miR-1245b-3p" "hsa-miR-1246"     "hsa-miR-1247"     "hsa-miR-1248"     "hsa-miR-1249"     "hsa-miR-1250"    
  [37] "hsa-miR-1251"     "hsa-miR-1252"
Was it helpful?

Solution 2

You can also use grep with the ignore.case argument set to TRUE

> unlist(sapply(B, function(x){
      grep(x, ScanName, ignore.case = TRUE, value = TRUE)
      }, USE.NAMES = FALSE))
## [1] "hsa-let-7a-2" "hsa-miR-1238"

which gives the same result at

> ScanName[tolower(ScanName) %in% tolower(B)]
## [1] "hsa-let-7a-2" "hsa-miR-1238"

OTHER TIPS

you can use %in% and tolower

ScanName[tolower(ScanName) %in% tolower(B)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top