如何为 R 扫描 .txt 中的单个字符?据我了解,扫描使用空格作为分隔符,但如果我想使用空格作为在 R 中扫描的东西,我该怎么做?
即(我想扫描字符串“Hello World”)我如何得到 H,e,l,l,o, ,W,o,r,l,d ?
如何为 R 扫描 .txt 中的单个字符?据我了解,扫描使用空格作为分隔符,但如果我想使用空格作为在 R 中扫描的东西,我该怎么做?
即(我想扫描字符串“Hello World”)我如何得到 H,e,l,l,o, ,W,o,r,l,d ?
strsplit
也将成为您的朋友:
test <- readLines(textConnection("Hello world
Line two"))
strsplit(test,"")
> strsplit(test,"")
[[1]]
[1] "H" "e" "l" "l" "o" " " "w" "o" "r" "l" "d"
[[2]]
[1] "L" "i" "n" "e" " " "t" "w" "o"
并且按照@Thilo 的建议未列出...
> unlist(strsplit(test,""))
[1] "H" "e" "l" "l" "o" " " "w" "o" "r" "l" "d" "L" "i" "n" "e" " " "t" "w" "o"
我将采用两步方法:首先将文件作为纯文本读取readLines
,然后将单行拆分为字符向量:
lines <- readLines("test.txt")
characterlist <- lapply(a, function(x) substring(x, 1:nchar(x), 1:nchar(x)))
请注意,这种方法不会返回格式良好的矩阵或 data.frame,而是返回一个列表。
根据您要执行的操作,可能会有一些不同的修改:
unlist(characterlist)
为您提供一行中所有字符的向量。如果您的文本文件表现得非常好,以至于每行中的字符数完全相同,您可能只需添加并simplify=T
希望lapply
得到您的字符矩阵。