将格式化数据读入 R

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

最佳答案

我对你的代码做了三个小改动,然后它就可以工作了:

  • 使用 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/

    相关文章:

    r - 对于一系列不规则形状的点和一组空间多边形,如何检测哪些点位于哪个多边形内?

    R Shiny变量有序或无序列表

    R:k最近邻分类

    用向量中的下划线替换两个单词之间的空格

    r - 在ggplot2中添加手动渐变图例

    r - 在R中,如何按降序对多列数据进行分组,然后为每个组指定从1开始的索引?

    r - 在 R 中创建一个指示共享唯一集群 ID 的列

    r - 使用 ddply 进行汇总统计

    r - ggplot2 - 当点图不在同一坐标处着色点时,如何使用图例应用手动渐变

    r - 指定条形图 R 上条形的位置