我试图弄清楚如何从列表中删除几个字符(不是一个一个):
-首先,我将{。{1}}字符列表A
中的data.frame(单列)转换为
-然后我将其转换为A2[]<-lapply(A,as.character)
,生成一个向量,其中包含A3<-unlist(A2, recursive = TRUE, use.names = TRUE)
中出现的所有原子成分。
最后,在这种情况下,我使用A1
删除了这些点。见下文:
str_remove(A3, ".")
问题在于,将向量的 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" "." "."
的其他元素应用为“ *”会丢失。正常吗?
str_remove()
有更好的方法吗?只是删除“。”该列的
[1] "" "" "" "" "" "" "" "" "" ""
[11] "" "1-" "C:" "" "" "" "" "" "" ""
[21] "" "" "7b" "" "" "" "" "" "" ""
获得:
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 .
答案 0 :(得分:3)
句点是一个元字符(请参见here),因此请尝试将其转义-
str_remove(A3, “\\.”)
编辑:
您可以使用str_remove_all
从任意长度的向量中删除多个字符,这将从供应字符向量中删除所有匹配项。您还可以提供str_remove_all
多个模式进行一次匹配,从而仅用一行代码即可执行多个操作。
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 = "|"))
这将返回:
"hello this is " "this is also " "here is even more "