Reputation: 361
for every row I would like all numbers before the first 5 to be deleted e.g. second row: 1 5 5 5 --> 5 5 5 but first row should stay the same as it starts with a 5. I have tried with gsub but it only gives me empty strings.
gsub(".*5", "",xy.list)
Any help is appreciated!
structure(list(data_rel1 = c("5 5 5 5", "1 5 5 5", "1 5 5 5",
"1 5 5 5", "1 5 5 5", "5 5 5 5", "1 5 5 5", "1 5 5 5", "5 5 5 5",
"1 5 5 5", "1 5 5 5", "2 5 5 5", "5 5 5 5", "5 5 5 5", "1 5 5 5",
"1 5 5 5", "1 5 5 5", "5 5 5 5", "3 5 5 5", "5 5 5 5", "5 5 5 5",
"5 5 5 5", "1 5 5 5", "1 5 5 5", "4 5 5 5", "1 5 5 5", "5 5 5 5",
"3 5 5 5", "1 5 5 5", "5 5 5 5", "5 5 5 5", "5 5 5 5", "5 5 5 5",
"5 5 5 5", "5 5 5 5", "5 5 5 5", "5 5 5 5", "1 5 5 5", "5 5 5 5",
"1 5 5 5", "5 5 5 5", "1 5 5 5", "5 5 5 5", "5 5 5 5", "1 5 5 5",
"1 5 5 5", "1 5 5 5", "5 5 5 5", "5 5 5 5", "5 5 5 5")), row.names = c(NA,
-50L), class = c("tbl_df", "tbl", "data.frame"))
Upvotes: 1
Views: 33
Reputation: 887971
Or can use str_remove
to match characters that are not 5 ([^5]+
) from the start (^
) of the string
library(dplyr)
library(stringr)
df %>%
mutate(data_rel1 = str_remove(data_rel1, '^[^5]+'))
Upvotes: 0
Reputation: 102920
You can try sub
like this
transform(
df,
data_rel1 = sub(".*?(5.*)", "\\1", data_rel1)
)
which gives
data_rel1
1 5 5 5 5
2 5 5 5
3 5 5 5
4 5 5 5
5 5 5 5
6 5 5 5 5
7 5 5 5
8 5 5 5
9 5 5 5 5
10 5 5 5
11 5 5 5
12 5 5 5
13 5 5 5 5
14 5 5 5 5
15 5 5 5
16 5 5 5
17 5 5 5
18 5 5 5 5
19 5 5 5
20 5 5 5 5
21 5 5 5 5
22 5 5 5 5
23 5 5 5
24 5 5 5
25 5 5 5
26 5 5 5
27 5 5 5 5
28 5 5 5
29 5 5 5
30 5 5 5 5
31 5 5 5 5
32 5 5 5 5
33 5 5 5 5
34 5 5 5 5
35 5 5 5 5
36 5 5 5 5
37 5 5 5 5
38 5 5 5
39 5 5 5 5
40 5 5 5
41 5 5 5 5
42 5 5 5
43 5 5 5 5
44 5 5 5 5
45 5 5 5
46 5 5 5
47 5 5 5
48 5 5 5 5
49 5 5 5 5
50 5 5 5 5
Upvotes: 0