从单列文本文件读取R中的数据

从单列文本文件读取R中的数据,r,import,read.table,R,Import,Read.table,我的名为myfile.txt的文件设置如下: Column1 Column2 Column3 ... Column10 Row1 1 2 3 4 Row2 5 6 7 8 ... 行最终变为100,我在read.table命令中遇到了问题。我不是一个有经验的R用户,所以我只是需要弄清楚这一点,并完成它 我以为上校的名字会像这样: read.table("myfile.txt", col.names = 1:10) 但这不起作用里卡多给出了一个提示,这里有一个方法可以让它起作用: x <

我的名为myfile.txt的文件设置如下:

Column1
Column2
Column3
...
Column10
Row1
1
2
3
4
Row2
5
6
7
8
...
行最终变为100,我在read.table命令中遇到了问题。我不是一个有经验的R用户,所以我只是需要弄清楚这一点,并完成它

我以为上校的名字会像这样:

read.table("myfile.txt", col.names = 1:10)

但这不起作用

里卡多给出了一个提示,这里有一个方法可以让它起作用:

x <- read.table(text="Column1
Column2
Column3
Column10
Row1
1
2
3
4
Row2
5
6
7
8")

xRicardo给出了一个提示,这里有一个方法可以让它工作:

x <- read.table(text="Column1
Column2
Column3
Column10
Row1
1
2
3
4
Row2
5
6
7
8")

x示例
myfile.txt

Column1
Column2
Column3
Column4
Row1
1
2
3
4
Row2
5
6
7
8
读取文件并创建矩阵:

lin <- scan("myfile.txt", "") # read lines

lin2 <- grep("Column|Row", lin, value = TRUE, invert = TRUE) # values

matrix(as.numeric(lin2), ncol = sum(grepl("Column", lin)), byrow = TRUE)
  # create matrix

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8

示例
myfile.txt

Column1
Column2
Column3
Column4
Row1
1
2
3
4
Row2
5
6
7
8
读取文件并创建矩阵:

lin <- scan("myfile.txt", "") # read lines

lin2 <- grep("Column|Row", lin, value = TRUE, invert = TRUE) # values

matrix(as.numeric(lin2), ncol = sum(grepl("Column", lin)), byrow = TRUE)
  # create matrix

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
1.样本数据 4.向任何以这种格式向您发送数据的人发送电子邮件,并对其进行抨击

1。样本数据 4.向任何以这种格式向您发送数据的人发送电子邮件,并对其进行抨击


我会考虑更改创建此文件的内容。这是一种可怕的格式。不过有一种:将其全部折叠成一个字符串(使用“\n”或其他已知的伪字符串),然后在
行\\d+\\n
上拆分,我会考虑更改创建此文件的内容。这是一种可怕的格式。尽管如此:将其全部折叠成一个字符串(使用“\n”或其他已知的伪字符串),然后在
行\\d+\\n
上拆分。嗨,我喜欢这个答案,尽管结果不正确。第1行假定为1、2、3、4。不是1 3 57@user1083079哦,对不起!我忘记了
matrix
函数的参数
byrow=TRUE
。现在,它起作用了。看更新。嗨,我喜欢这个答案,虽然结果不正确。第1行假定为1、2、3、4。不是1 3 57@user1083079哦,对不起!我忘记了
matrix
函数的参数
byrow=TRUE
。现在,它起作用了。请参阅更新。
Column1
Column2
Column3
Column4
Row1
1
2
3
4
Row2
5
6
7
8
lin <- scan("myfile.txt", "") # read lines

lin2 <- grep("Column|Row", lin, value = TRUE, invert = TRUE) # values

matrix(as.numeric(lin2), ncol = sum(grepl("Column", lin)), byrow = TRUE)
  # create matrix

     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
lin <- scan("myfile.txt", "") # read lines

idx <- grep("^Row", lin) # index of lines containing 'Row'

lin2 <- lin[-(c(seq(1, idx[1] - 1), idx))] # actual values

matrix(as.numeric(lin2), nrow = length(idx), 
       dimnames = list(NULL, lin[seq(1, idx[1] - 1)]), byrow = TRUE)

      Column1 Column2 Column3 Column4
 [1,]       1       2       3       4
 [2,]       5       6       7       8
X <- read.table(text=
"Column1
Column2
Column3
Column10
Row1
1
2
3
4
Row2
5
6
7
8
Row99
1
2
3
4
Row100
5
6
7
8", stringsAsFactors=FALSE)
# Some string that does not appear naturally in your data
dummyCollapse <- "\n"  # eg:  "zQz5Nsddfdfjjj"
## Make sure to properly escape the escape char.
dummyFind <- "\\n"  

flat  <- paste(unlist(X), collapse=dummyCollapse)
splat <- strsplit(flat, paste0("Row\\d+", dummyFind))

## strsplit returns a list.  You likely want just the first element
splat <- splat[[1]]

## weed out colnames 
cnms <- splat[[1]]  # now, the first element is the coloumn names from the weird data structure
# split them, also on dummyFind
cnms <- strsplit(cnms, dummyFind) 
# again, only want the first element
cnms <- cnms[[1]]  

## Weed out the rows
rows <- tail(splat, -1)

# split on dummy find
rows <- strsplit(rows, dummyFind)
## NOTE: This time, do NOT take just the first element from strsplit. You want them all

## Combine the data into a matrix
MyData <- do.call(cbind, rows)
## Coerce to data.frame, if you'd like
MyData <- as.data.frame(MyData)
## Add in Column names
colnames(MyData) <- cnms
> MyData
  Column1 Column2 Column3 Column10
1       1       5       1        5
2       2       6       2        6
3       3       7       3        7
4       4       8       4        8