我想知道R
中是否存在一个已存在的简单命令,并允许将char文本文件(.txt)导入char矢量。
该文件可能是带有字符串的英文文本
像"Hello my name is Fagui Curtain"
并且R中的输出将是char矢量A.
这样A[1]<-"H"
,A[2]<-"e"
,A[3]<-"l"
等......
我尝试过扫描功能,但会返回单词
A[1]<-"Hello"
,A[2]<-"my"
....
我用谷歌搜索了我的问题,但找不到任何有用的东西。
由于
答案 0 :(得分:8)
使用strsplit
gsub
A <- strsplit(gsub('\\s+', '', lines),'')[[1]]
A
#[1] "H" "e" "l" "l" "o" "m" "y" "n" "a" "m" "e" "i" "s" "F" "a" "g" "u" "i" "C"
#[20] "u" "r" "t" "a" "i" "n"
或者
library(stringi)
stri_extract_all_regex(lines, '\\w')[[1]]
#[1] "H" "e" "l" "l" "o" "m" "y" "n" "a" "m" "e" "i" "s" "F" "a" "g" "u" "i" "C"
#[20] "u" "r" "t" "a" "i" "n"
或者,如果您使用的是Linux,scan
并使用awk
scan(pipe("awk 'BEGIN{FS=\"\";OFS=\" \"}{$1=$1}1' file.txt"),
what='', quiet=TRUE)
#[1] "H" "e" "l" "l" "o" "m" "y" "n" "a" "m" "e" "i" "s" "F" "a" "g" "u" "i" "C"
#[20] "u" "r" "t" "a" "i" "n"
lines <- readLines('file.txt')
答案 1 :(得分:1)
使用stringr
包的替代解决方案(我喜欢它,因为它产生非常易读的语法)。
<强> sample_text 强>
Hello my name is Fagui Curtain
文件阅读
require(stringr)
testVector <- str_split(readLines("sample_text.txt"), pattern = " ")