nik
nik

Reputation: 2584

how to remove part of string based on a comma for all rows

df <- structure(list(V = structure(c(4L, 5L, 3L, 7L, 6L, 2L, 1L), .Label = c("132 B26,172 B27,107 B57,104 B59,137 B60,133 B61,103 B62,134 B63,177 B100,123 B133,184 B168,109 B197,103 B198,173 B202,157 B203,143 B266,62 B342,62 B354,92 B355,195 B368,164 B370,52 B468,74 B469,71 B484,98 B494,66 B502,63 B601,133 B622", 
"135A,510A,511A,60 B23,67 B24,70 B25,95 B26,122 B27,123 B27,109 B60", 
"25A,28 B55,31 B56,45 B57,43 B58,5 B59,47 B59,6 B60,69 B60,66 B61", 
"267 B361,786 B363,543 B392", "563 B202,983 B360", "8 B1,12 B35,10 B71,9 B154,51 B179", 
"91 B26,117 B27,117 B28,102 B29,47 B31,96 B63,78 B64,133 B65,117 B66,121 B66,112 B67,127 B100"
), class = "factor")), .Names = "V", class = "data.frame", row.names = c(NA, 
-7L))

I want to have an output like this

V
    361, 363, 392
    202,360
    55,56,57,58,59,59,60,60,61
    26,27,28,29,31,63,64,65,66,66,67,100
    1,35,71,154,179
    23,24,25,26,27,27,60
    26,27,57,59,60,61,62,63,100,133,168,197,198,202,203,266,342,354,355,368,370,468,469,484,494,502,601,622

I have tried for one string which works

s = "267 B361"
s1 = unlist(strsplit(s, split='B', fixed=TRUE))[2]

but I don't know how to apply it on all strings which are separated by a comma in each row

Upvotes: 1

Views: 40

Answers (1)

akrun
akrun

Reputation: 887203

We can use str_extract_all to get the numbers that follow a non-numeric character. The output will be a list, so loop over the list with sapply and paste the elements in the list together (toString is a wrapper for paste(., collapse=', ')).

library(stringr)
sapply(str_extract_all(df$V, "(?<=[A-Z])\\d+"), toString)
#[1] "361, 363, 392"                                                                                                                     
#[2] "202, 360"                                                                                                                          
#[3] "55, 56, 57, 58, 59, 59, 60, 60, 61"                                                                                                
#[4] "26, 27, 28, 29, 31, 63, 64, 65, 66, 66, 67, 100"                                                                                   
#[5] "1, 35, 71, 154, 179"                                                                                                               
#[6] "23, 24, 25, 26, 27, 27, 60"                                                                                                        
#[7] "26, 27, 57, 59, 60, 61, 62, 63, 100, 133, 168, 197, 198, 202, 203, 266, 342, 354, 355, 368, 370, 468, 469, 484, 494, 502, 601, 622"

Upvotes: 1

Related Questions