[英]Reading formatted data into 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", ¶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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.