質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
R

R言語は、「S言語」をオープンソースとして実装なおした、統計解析向けのプログラミング言語です。 計算がとても速くグラフィックも充実しているため、数値計算に向いています。 文法的には、統計解析部分はS言語を参考にしており、データ処理部分はSchemeの影響を受けています。 世界中の専門家が開発に関わり、日々新しい手法やアルゴリズムが追加されています。

Q&A

解決済

1回答

823閲覧

あるはずの「csvファイルが存在しない」とエラーが出る。

wanwanko

総合スコア14

R

R言語は、「S言語」をオープンソースとして実装なおした、統計解析向けのプログラミング言語です。 計算がとても速くグラフィックも充実しているため、数値計算に向いています。 文法的には、統計解析部分はS言語を参考にしており、データ処理部分はSchemeの影響を受けています。 世界中の専門家が開発に関わり、日々新しい手法やアルゴリズムが追加されています。

0グッド

0クリップ

投稿2023/03/06 16:02

実現したいこと

①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

personsweightheightgender
person192.24459182.0007F
person279.88506199.0311F
person365.59031180.8477F
person2580.16016196.6961M
person2687.0112174.8087F

Output a summary file

Please follow the same format of the output1.csv, i.e., round number into two digitals

examples: output1.csv

setweightheight
input199.76199.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")

先生に質問禁止で、聞ける友達がいません。
厳しいご指摘をいただくことは承知の上での質問です。どうかご教授よろしくお願いします。

イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

melian

2023/03/06 16:12

> ディレクトリの中にexampleというファイルがあり、その中に とのことですので、 Rscript practice.R --input example/input1.csv --output example/output1.csv とするのではないでしょうか。
guest

回答1

0

自己解決

melianさんのおっしゃる通り、Rscript practice.R --input example/input1.csv --output example/output1.csv

としたら、できました。
コメントしてくださったmelianさんとxebmeさんにグットボタンとベストアンサーにすることがなぜかできないので自己解決にしました。泣

投稿2023/05/31 09:55

wanwanko

総合スコア14

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問