R:如何创建字符数组数组

R:如何创建字符数组数组,r,R,我对R相当陌生,我必须了解以下情况。我得到一个文本文件,内容如下: [1] a b c d e f g [2] h i j k [3] l m n o [4] x y z 我想将属于同一标识符([x])的所有字符分组到一个数组中。因为我想访问所有标识符的所有字符,所以我需要一个二维数组(数组的数组)。 这就是我所尝试的: > bigvector <- vector() > bigvector <- append(bigvector, vector()) > big

我对R相当陌生,我必须了解以下情况。我得到一个文本文件,内容如下:

[1] a b c d e f g
[2] h i j k
[3] l m n o
[4] x y z
我想将属于同一标识符([x])的所有字符分组到一个数组中。因为我想访问所有标识符的所有字符,所以我需要一个二维数组(数组的数组)。 这就是我所尝试的:

> bigvector <- vector()
> bigvector <- append(bigvector, vector())
> bigvector[0][0] <- "Test"
> > bigvector[0][0]
logical(0)
>bigvector bigvector bigvector[0][0]>bigvector[0][0]
逻辑(0)
因此,不返回“测试”。我还尝试:

> tmpvector <- c("A", "B", "C", "D", "E", "F")
> bigvector <- vector()
> bigvector <- append(bigvector, tmpvector)
> bigvector[0][0]
character(0)
>tmpvector bigvector bigvector[0][0]
字符(0)

这应该是一项简单的任务,但我正在努力完成它。

我不确定您想做什么,以及是否真的需要
数组
对象

我建议使用列表。下面是一个示例,假设您的
[x]
只是行号

#read the data using readLines
tc <- textConnection("[1] a b c d e f g
[2] h i j k
[3] l m n o
[4] x y z")

dat <- readLines(tc)

#split at spaces
dat <- strsplit(dat,split=" ")

#remove identifier
rm1 <- function(x) x[-1] 
dat <- sapply(dat,rm1)

dat
#[[1]]
#[1] "a" "b" "c" "d" "e" "f" "g"
#
#[[2]]
#[1] "h" "i" "j" "k"
#
#[[3]]
#[1] "l" "m" "n" "o"
#
#[[4]]
#[1] "x" "y" "z"

dat[[3]][3]
#[1] "n"

索引是基于1的,元素0将始终是空集Hey Roland,谢谢你的提示。这适用于多行内容。但是,当我必须处理一行内容时,这些代码不起作用。当我将真实数据集应用于此算法(而不是合成数据集)时,它会破坏整个数据结构……抱歉,无法再编辑上述注释……因此,我的回答是:当我将真实数据集应用于此算法(而不是合成数据集)时它破坏了数据的整体结构。数据集:结果:。。。[2339,] "0" "0" "0" [2340,] "0" "1" "1" [2341,] "1" "1" "1" [2342,] "1" "1" "1" [2343,] "1" "1" "1" [2344,] "1" "1" "1" [2345,] "0" "0" "0" [2346,] "0" "1" "1" [2347,] "0" "0" "0" [2348,] "0" "0" "0" [2349,] "0" "0" "0" [2350,] "0" "0" "0" ....
dat <- readLines(file('http://pastebin.com/raw.php?i=tJW8H6K1'))

#split at spaces
dat <- strsplit(dat,split=" ")

#remove identifier
rm1 <- function(x) x[-1] 
dat <- lapply(dat,rm1)

#first five characters of the first line
dat[[1]][1:5]
#[1] "1" "1" "0" "1" "0"