我正试图为一个数据帧创建一个向量,它包含所有的点状和符号,如:点状: R中的类,有什么方法可以打印出类的内容,而不是将所有字符串在一起?似乎您必须转义每个字符,并将它们全部手工粘贴到字符串中,这似乎非常乏味。
这些都是符号:
! " # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~.
#code so far
symbols <- c(' ! " # $ % & ’ ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~. ')任何帮助都将不胜感激。谢谢。
发布于 2016-09-01 22:53:16
您可以从raw转换为字符,然后对预定义的类进行grep:
(rch <- as.raw(0:255))
# [1] 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 ...
(ch <- rawToChar(rch, TRUE))
# [1] "" "\001" "\002" "\003" "\004" "\005" "\006" "\a" "\b" "\t" "\n" "\v" "\f" ...
## change locale to avoid warnings
Sys.setlocale('LC_ALL','C')
dput(grep('[[:punct:]]', ch, value = TRUE))
# c("!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",",
# "-", ".", "/", ":", ";", "<", "=", ">", "?", "@", "[", "\\",
# "]", "^", "_", "`", "{", "|", "}", "~")?regex描述了这些类:
[:alnum:]字母数字字符:[:alpha:]和[:digit:]。[:alpha:]字母字符:[:lower:]和[:upper:]。[:blank:]空白字符:空格和制表符,可能还有其他与区域设置相关的字符,如非破缺空间.[:cntrl:]控制字符。在ASCII中,这些字符的八进制代码从000到037,以及177 (DEL)。在另一个字符集中,这些字符(如果有的话)是等效的。[:digit:]数字:0 1 2 3 4 5 6 7 8 9.[:graph:]图形字符::alnum:和:punct:。 当前区域设置中的[:lower:]小写字母。[:print:]可打印字符:[:alnum:]、[:punct:]和空格。{ | } ~.:space:Space characters: tab, newline, vertical tab, form feed, carriage return, space and possibly other locale-dependent characters.:upper:Upper-case letters in the current locale.:xdigit:`标点符号:!#$$%&‘()*+,-/:;<=>?@\^_{ | } ~.:space:Space characters: tab, newline, vertical tab, form feed, carriage return, space and possibly other locale-dependent characters.:upper:Upper-case letters in the current locale.:xdigit:`十六进制数字:0 1 2 3 4 5 6 7 9 A,B,C,D,F a b c d e f.
因此,我们可以重复上面的任何一个
dput(grep('[[:space:]]', ch, value = TRUE))
# c("\t", "\n", "\v", "\f", "\r", " ")
dput(grep('[[:alnum:]]', ch, value = TRUE))
# c("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B",
# "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
# "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b",
# "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
# "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")您也可以使用PCRE。
dput(grep('\\s', ch, value = TRUE))
# c("\t", "\n", "\v", "\f", "\r", " ")
dput(grep('\\v|\\h', ch, value = TRUE, perl = TRUE))
# c("\t", "\n", "\v", "\f", "\r", " ", "\205", "\240")
dput(grep('\\p{P}', ch, value = TRUE, perl = TRUE))
# c("!", "\"", "#", "%", "&", "'", "(", ")", "*", ",", "-", ".",
# "/", ":", ";", "?", "@", "[", "\\", "]", "_", "{", "}", "\241",
# "\247", "\253", "\266", "\267", "\273", "\277")或定义你自己的等
dput(grep('[\x20-\x7E]', ch, value = TRUE))
dput(grep('[A-c]', ch, value = TRUE))https://stackoverflow.com/questions/39281185
复制相似问题