我正在尝试使用 stdin
将数据读入 R。我想以以下格式读取数据:
5 #rows matrix A
7 #cols matrix A
0 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 32 33 34
3 #rows matrix B
4 #cols matrix B
0 1 2 3
4 5 6 7
8 9 10 11
100 #param1
-7 #param2
9 #param3
以下 c 代码最能说明我希望如何读取数据的示例:
#include <stdio.h>
#include <stdlib.h>
int* readMatrix(int rows, int cols) {
int* matrix = (int*) malloc(rows*cols*sizeof(int));
for (int i = 0; i < rows*cols; ++i) {
scanf("%d", &matrix[i]);
}
return matrix;
}
int main() {
int a_rows, a_cols;
int b_rows, b_cols;
int *a, *b;
int param1, param2, param3;
scanf("%d %d", &a_rows, &a_cols);
a = readMatrix(a_rows, a_cols);
scanf("%d %d", &b_rows, &b_cols);
b = readMatrix(b_rows, b_cols);
scanf("%d %d %d", ¶m1, ¶m2, ¶m3);
return 0;
}
我的“等效”R 代码是这样的:
a_rows <- scan(file="stdin", what=integer(0), n=1);
a_cols <- scan(file="stdin", what=integer(0), n=1);
A <- matrix(scan(file="stdin", n = a_rows*a_cols), a_rows, a_cols, byrow = TRUE)
b_rows <- scan(file="stdin", what=integer(0), n=1);
b_cols <- scan(file="stdin", what=integer(0), n=1);
B <- matrix(scan(file="stdin", n = b_rows*b_cols), b_rows, b_cols, byrow = TRUE)
param1 <- scan(file="stdin", what=integer(0), n=1);
param2 <- scan(file="stdin", what=integer(0), n=1);
param3 <- scan(file="stdin", what=integer(0), n=1);
当然,这不起作用,当我运行 R 脚本时,我得到以下输出:
Read 1 item
Read 0 items
Read 0 items
Error in matrix(scan(file = "stdin", n = a_rows * a_cols), a_rows, a_cols, :
invalid 'ncol' value (too large or NA)
Execution halted
事实上,如果我尝试只读取前两个值:
a_rows <- scan(file="stdin", what=integer(0), n=1);
a_cols <- scan(file="stdin", what=integer(0), n=1);
我得到以下输出:
Read 1 item
Read 0 items
有没有人对这个问题有很好的解决方案?我一直在努力寻找有关
scan
的更多信息功能,但找不到任何关于我为什么会出现这种行为的信息。编辑 1
我正在运行 R 版本 3.1.0
R version 3.1.0 (2014-04-10) -- "Spring Dance"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-unknown-linux-gnu (64-bit)
编辑 2
我找到了一个解决方案,R代码如下:
sin <- file("stdin");
open(sin);
a_rows <- scan(sin, what=integer(0), n=1);
a_cols <- scan(sin, what=integer(0), n=1);
A <- matrix(scan(sin, n = a_rows*a_cols), a_rows, a_cols, byrow = TRUE)
b_rows <- scan(sin, what=integer(0), n=1);
b_cols <- scan(sin, what=integer(0), n=1);
B <- matrix(scan(sin, n = b_rows*b_cols), b_rows, b_cols, byrow = TRUE)
param1 <- scan(sin, what=integer(0), n=1);
param2 <- scan(sin, what=integer(0), n=1);
param3 <- scan(sin, what=integer(0), n=1);
为什么我必须打开
stdin
, 我不知道。
最佳答案
我对你的代码做了三个小改动,然后它就可以工作了:
nlines=...
而不是 n=...
file=stdin()
而不是 file="stdin"
what=integer()
而不是 what=integer(0)
注意:我怀疑只有
nlines=...
在这里很重要。其他更改可能不是必需的。尝试这个:
a_rows <- scan(file=stdin(), what=integer(), nlines=1);
a_cols <- scan(file=stdin(), what=integer(), nlines=1);
A <- matrix(scan(file=stdin(), nlines = a_rows), a_rows, a_cols, byrow = TRUE)
print(A)
然后我获取文件:
> source('~/.active-rstudio-document')
1: 2
Read 1 item
1: 3
Read 1 item
1: 1
2: 2
Read 2 items
> source('~/.active-rstudio-document')
1: 2
Read 1 item
1: 3
Read 1 item
1: 1 2 3
4: 4 5 6
Read 6 items
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
关于将格式化数据读入 R,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24527499/