将格式化数据读入R中

时间:2014-07-02 09:28:42

标签: r

我正在尝试使用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", &param1, &param2, &param3);

    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,我不知道。

1 个答案:

答案 0 :(得分:-1)

我对您的代码进行了三处小修改,然后就可以了:

  1. 使用nlines=...代替n=...
  2. 使用file=stdin()代替file="stdin"
  3. 使用what=integer()代替what=integer(0)
  4. 注意:我怀疑这里只有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