実現したいこと
①githubからcloneしたディレクトリに、R言語のコードファイルを追加する。(完了)
②cloneしたディレクトリに既に存在するcsvファイルを使用して、①のR言語を実行。(エラーが出る)
前提
R言語で、csvファイルを使用してcsvファイルの中で一番重い体重、一番高い身長をoutputしたいです。
ファイルは存在するのですが、どうしても実装すると以下のエラーが発生してしまいます。
ディレクトリの中にexampleというファイルがあり、その中に、input1.csv,input2.csv,input3.csv,output1.csv,output2.csv,output3.csvのファイルがあります。
発生している問題・エラーメッセージ
エラー: input1.csv doesn't exit 実行が停止されました
該当のソースコード
R
1# Simple example of extracting input args 2args = commandArgs(trailingOnly=TRUE) 3if (length(args)==0) { 4 stop("USAGE: Rscript practice.R --input input1.csv --output output1.csv", call.=FALSE) 5} 6 7# get input&output path 8f_in <- NA 9f_out <- NA 10 11#parse input & output 12for (i in args){ 13 if(i=='--input'){ 14 f_in <- args[which(args==i)+1] 15 } 16 if(i=='--output'){ 17 f_out <- args[which(args==i)+1] 18 } 19} 20 21#check whether there is input or output 22if (is.na(f_in)){ 23 stop("Unable to identify input file, please use --input input.csv", call.=FALSE) 24} 25 26if (is.na(f_out)) { 27 # Generate output file name 28 f_out <- gsub(".csv", "_output.csv", f_in) 29 message("Output file name not provided, generated output file name: ", f_out) 30} 31 32if (!file.exists(f_in)) { 33 stop((sprintf("%s doesn't exit", f_in)), call.=FALSE) 34} 35 36message("input file = ", f_in) 37message("output file = ", f_out) 38 39# read data & check columns 40df <- read.csv(f_in) 41if(!any(match(names(df),"weight"))) { 42 stop("Input data does not contain a 'weight' column.", call.=FALSE) 43} 44 45print(f_in)
試したこと
ファイル名を書き間違えているのかと思い確認しましたが、打ち間違えはしていないかと思います。
もし、コード自体が間違っている場合があれば、ご指摘しただける助かります。
補足情報(FW/ツールのバージョンなど)
以下はこの問題の説明文です。
Description
- Your R code should output and round the set name with maximum value of weight and height.
- You should implement the command parser by yourself without using any library.
cmd
R
1Rscript practice.R --input input1.csv --output output1.csv 2 3Rscript practice.R --output output1.csv --input input1.csv
Read an input file
Input data will have other numeric & category columns besides weight and height.
examples: input1.csv
persons | weight | height | gender |
---|---|---|---|
person1 | 92.24459 | 182.0007 | F |
person2 | 79.88506 | 199.0311 | F |
person3 | 65.59031 | 180.8477 | F |
… | … | … | … |
person25 | 80.16016 | 196.6961 | M |
person26 | 87.0112 | 174.8087 | F |
Output a summary file
Please follow the same format of the output1.csv, i.e., round number into two digitals
examples: output1.csv
set | weight | height |
---|---|---|
input1 | 99.76 | 199.03 |
Code for reference
R
1# Simple example of extracting input args 2args = commandArgs(trailingOnly=TRUE) 3if (length(args)==0) { 4 stop("USAGE: Rscript hw1_exam.R input", call.=FALSE) 5} else if (length(args)==1) { 6 i_f <- args[1] 7} 8print(i_f)
Score
10 testing data (90%)
R
1Rscript hw1_5566.R --input hw1/data/test.1.csv --output hw1/eval/test1/hw1_001.csv 2Rscript hw1_5566.R --output hw1/eval/test2/hw1_002.csv --input hw1/data/test.2.csv
Correct answer gets 9 points of each testing data.
Bonus (10%)
- Output format without “: 3 points
- Concise file name without path: 3 points
- Concise file name without .csv extension: 4 points
Penalty: -2 points of each problem
- Can not detect missing --input/--ouptut flag
- Arguments order cannot change
- Wrong file name
- Wrong column name
- Not round number to 2 digitals
Note
- Please use R version above 4
- Please do not set working directory(setwd) in a fixed folder. For example,
R
1d <- read.csv("D://DataScience/hw1/example/output1.csv")
先生に質問禁止で、聞ける友達がいません。
厳しいご指摘をいただくことは承知の上での質問です。どうかご教授よろしくお願いします。

回答1件
あなたの回答
tips
プレビュー