一个字字符串到R中的向量

一个字字符串到R中的向量,r,string,vector,R,String,Vector,像绳子一样的 "xzyyzyzyxyyyxxxyzzyzzzxzyxxzzzyzyxyxxxxyxxyxxyxyyzxyzzyzzxyxyyzxzzxyxxyxxzyxyzyxzzyzxxyzyzyyyzxxxzxx" 如何将其转换为向量,如: "x","z","y","y","z","y","z","y","x","y","y","y","x","x","x","y","z","z","y","z","z","z","x","z","y","x","x","z","z","z","y","z

像绳子一样的

"xzyyzyzyxyyyxxxyzzyzzzxzyxxzzzyzyxyxxxxyxxyxxyxyyzxyzzyzzxyxyyzxzzxyxxyxxzyxyzyxzzyzxxyzyzyyyzxxxzxx"
如何将其转换为向量,如:

"x","z","y","y","z","y","z","y","x","y","y","y","x","x","x","y","z","z","y","z","z","z","x","z","y","x","x","z","z","z","y","z","y","x",","y",","x","x","x","x",","y",","x","x",","y",","x","x",","y",","x","y","y","z","x","y","z","z","y","z","z","x",","y",","x","y","y","z","x","z","z","x",","y",","x","x",","y",","x","x","z","y","x","y","z","y","x","z","z","y","z","x","x","y","z","y","z","y","y","y","z","x","x","x","z","x","x"
我做不到

unlist(strsplit("thestring", split=""))
你可以试试:

strsplit("xzyyzyzyxyyyxxxyzzyzzzxzyxxzzzyzyxyxxxxyxxyxxyxyyzxyzzyzzxyxyyzxzzxyxxyxxzyxyzyxzzyzxxyzyzyyyzxxxzxx", "")
[[1]]
[1] "x" "z" "y" "y" "z" "y" "z" "y" "x" "y" "y" "y" "x" "x" "x" "y" "z" "z" "y" "z" "z" "z" "x" "z" "y" "x" "x" "z" "z" "z" "y" "z" "y" "x" "y" "x" "x" "x" "x"
[40] "y" "x" "x" "y" "x" "x" "y" "x" "y" "y" "z" "x" "y" "z" "z" "y" "z" "z" "x" "y" "x" "y" "y" "z" "x" "z" "z" "x" "y" "x" "x" "y" "x" "x" "z" "y" "x" "y" "z"
[79] "y" "x" "z" "z" "y" "z" "x" "x" "y" "z" "y" "z" "y" "y" "y" "z" "x" "x" "x" "z" "x" "x"
要指定给新向量,请执行以下操作:

x <- strsplit("xzyyzyzyxyyyxxxyzzyzzzxzyxxzzzyzyxyxxxxyxxyxxyxyyzxyzzyzzxyxyyzxzzxyxxyxxzyxyzyxzzyzxxyzyzyyyzxxxzxx", "")
str(x)
#List of 1
# $ : chr [1:100] "x" "z" "y" "y" ...
is.vector(x)
#[1] TRUE
请注意,您还可以将此输出另存为另一个对象:

thestring2 <- strsplit(thestring, "")
#You can 'rename' the same object too, bear in mind you would have to assign the actual "xzyy..." to `thestring` first:
thestring <- strsplit(thestring, "")

如何将其分配给向量i已使用更多信息编辑了答案@cMinorWhy您不能这样做?您可能输入错误。试一试strsplittstring,split=
thestring2 <- strsplit(thestring, "")
#You can 'rename' the same object too, bear in mind you would have to assign the actual "xzyy..." to `thestring` first:
thestring <- strsplit(thestring, "")