-1

I have a df like below

df <- structure(list(V1 = structure(c(3L,), 
                          class = "factor")), 
                    .Names = c("V1", "V2", "V3"), class = "data.frame", 
                     row.names = c(NA, -7L))

I want to remove part of string in each cell after the first |

so the output looks like this

out<- structure(list(V1 = structure(c(3L, 2L, 4L, 1L, 6L, 5L, 7L), .Label = c("O15111", 
"P41250", "P62805", "Q13547", "Q16555", "Q8NBS9", "Q9H6T3"), class = "factor"), ta.frame", row.names = c(NA, -7L))
nik
  • 2,500
  • 5
  • 21
  • 48

1 Answers1

2

We can loop through every column in df and substitute everything after first | with a blank space

df[] <- lapply(df, function(x) sub("\\|.*", "", as.character(x)))

df
#   V1       V2        V3
#1 P62805   Q71DI3       
#2 P41250   P12081    P34896
#3 Q13547   P62805       
#4 O15111   Q13748       
#5 Q8NBS9   Q12792       
#6 Q16555   Q14195-2  P21359
#7 Q9H6T3   Q9Y230    Q9Y265

EDIT

As per the update in the comments, to paste the columns together and remove the values which are empty

apply(df, 1, function(x) paste0(sub("\\|.*", "", as.character(x[x!=""])), collapse = ","))

#[1] "P62805,Q71DI3"  "P41250,P12081,P34896"   "Q13547,P62805" "O15111,Q13748"         
#[5] "Q8NBS9,Q12792"  "Q16555,Q14195-2,P21359" "Q9H6T3,Q9Y230,Q9Y265"  
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • 2
    Don't need to loop really, just `df[] <- sub("\\|.*", "", unlist(df))` should do – David Arenburg Dec 15 '16 at 10:28
  • @Ronak Shah how can you put each line together with a , seperated ? – nik Dec 15 '16 at 11:14
  • @nik Do you need this ? `apply(df, 1, function(x) paste0(sub("\\|.*", "", as.character(x)), collapse = ","))` – Ronak Shah Dec 15 '16 at 12:10
  • @Ronak Shah yes but if I use that then it makes it like P62805,Q71DI3,,,,,,,,,,,,,,,,,,,,,, for example the first line. I want it to be P62805,Q71DI3 – nik Dec 15 '16 at 13:07
  • @nik ok, we need to exclude the empty strings then . How about `apply(df, 1, function(x) paste0(sub("\\|.*", "", as.character(x[x!=""])), collapse = ","))` ? – Ronak Shah Dec 15 '16 at 13:14
  • 1
    @Ronak Shah thanks I accepted and liked your answer – nik Dec 15 '16 at 13:54