I was trying to figure out how to remove several characters from a list (not one per one):
-First I converted my data.frame (single column) in a list A
of characters with A2[]<-lapply(A,as.character)
-then I converted it to A3<-unlist(A2, recursive = TRUE, use.names = TRUE)
producing a vector which contains all the atomic components which occur in A1
.
Finally I use str_remove(A3, ".")
to remove, in this case, the points. See below:
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10
"*" "*" "*" "*" "*" "*" "*" "*" "*" "*"
A11 A12 A13 A14 A15 A16 A17 A18 A19 A20
"*" "=1-" "*C:" "I" "." "." "." "." "." "."
A21 A22 A23 A24 A25 A26 A27 A28 A29 A30
"." "2" "V7b" "." "." "." "." "I" "." "."
The problem is that applying str_remove()
other elements of the vector as "*" get lost. Is that normal?
[1] "" "" "" "" "" "" "" "" "" ""
[11] "" "1-" "C:" "" "" "" "" "" "" ""
[21] "" "" "7b" "" "" "" "" "" "" ""
Is there a better way to do that? It is simply to remove "." of that column:
12 =1-
13 *C:
14 I
15 .
16 .
17 .
18 .
19 .
20 .
21 .
22 2
23 V7b
24 .
25 .
26 .
27 .
28 I
29 .
30 .
31 .
to get:
=1-
*C:
I
2
V7b
I
The period is a meta character (see here ), so try escaping it like so-
str_remove(A3, “\\.”)
Edit:
You can remove multiple characters from a vector of arbitrary length with str_remove_all
, which removes every match from the supply character vector. You can also supply str_remove_all
multiple patterns to match at once, allowing you to do multiple operations with only one line of code.
library(stringr)
a <- c("hello. this. is. text","this. is. also. text","here. is. even. more. text")
remove_these <- c("\\.","text")
str_remove_all(a, paste(remove_these, collapse = "|"))
This returns:
"hello this is " "this is also " "here is even more "
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.