2

I have a table with several columns and in one of them I need to create a loop that flows down the column and if the same number is present twice another column is created (adding a "p" for example) and in which are present only once in the same column created earlier is added for example "-". Anyone?

the column have Barcodes TCGA-3M-AB47-01A-22R-A414-31 I need the AB47

for(code in tabela$Barcode){
     t=sapply(strsplit(as.character(code), "-"), function(x) x[[2]]) #to extract the AB47
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
kiliark
  • 21
  • 2

1 Answers1

0

This is essentially equivalent to the common find all duplicates question. (Which is, ironically, very widely duplicated.)

For a vector x like x = c("A", "A", "C", "B", "C", "D", "E", "F"), the most common answer is to use the duplicated function twice, once with fromLast = T to flag all duplicates. This will give a boolean vector indicating whether each value is duplicated. Adding 1 to the boolean converts it from TRUE/FALSE to 2/1, which we can use as a subsetting index of your desired markings:

y = c("-", "P")[(duplicated(x) | duplicated(x, fromLast = T)) + 1]
Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294