{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Hello World" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Hello World\"\n", "[1] \"日本語の入出力を確認します\"\n" ] } ], "source": [ "# 行頭に # をつけた行や行の途中での # 以降はコメントとして扱われます\n", "# 引用符(\" または ')で囲んだ文字は文字列として処理されます\n", "print(\"Hello World\")\n", "print(\"日本語の入出力を確認します\") # ここからはコメント" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [], "source": [ "## 四則演算" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "2" ], "text/latex": [ "2" ], "text/markdown": [ "2" ], "text/plain": [ "[1] 2" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "0" ], "text/latex": [ "0" ], "text/markdown": [ "0" ], "text/plain": [ "[1] 0" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "6" ], "text/latex": [ "6" ], "text/markdown": [ "6" ], "text/plain": [ "[1] 6" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "2" ], "text/latex": [ "2" ], "text/markdown": [ "2" ], "text/plain": [ "[1] 2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# 演算の優先順位は数学と同じ\n", "# 括弧内の演算 > かけ算・割り算 > 足し算・引き算\n", "1 + 1\n", "5 - 5\n", "2 * 3\n", "(2 + 4) / 3" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 論理演算" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "FALSE" ], "text/latex": [ "FALSE" ], "text/markdown": [ "FALSE" ], "text/plain": [ "[1] FALSE" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "'logical'" ], "text/latex": [ "'logical'" ], "text/markdown": [ "'logical'" ], "text/plain": [ "[1] \"logical\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Rでは論理値ベクトルとして TRUE(真)、FALSE(偽)、NAがあります\n", "9 > 8\n", "1.1 <= 1.0\n", "\"apple\" == \"apple\"\n", "\"apple\" != \"banana\"\n", "\n", "class(c(TRUE, FALSE, NA))" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 変数へのオブジェクトの代入" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "9" ], "text/latex": [ "9" ], "text/markdown": [ "9" ], "text/plain": [ "[1] 9" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "6" ], "text/latex": [ "6" ], "text/markdown": [ "6" ], "text/plain": [ "[1] 6" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "9" ], "text/latex": [ "9" ], "text/markdown": [ "9" ], "text/plain": [ "[1] 9" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "9" ], "text/latex": [ "9" ], "text/markdown": [ "9" ], "text/plain": [ "[1] 9" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Rでは <- (または ->、=)によってオブジェクトの内容を変数に割り当てます\n", "x <- 6\n", "y <- 3\n", "# 変数は環境中に保存され、変数名により参照できます\n", "z <- x + y\n", "z\n", "# 変数に保存された内容は上書きされるまで残ります\n", "x\n", "x <- 9\n", "x\n", "# xの値が変わっても変更前に計算された値には影響ありません\n", "z" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 関数" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "data": { "text/html": [ "1.4142135623731" ], "text/latex": [ "1.4142135623731" ], "text/markdown": [ "1.4142135623731" ], "text/plain": [ "[1] 1.414214" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "1.73205080756888" ], "text/latex": [ "1.73205080756888" ], "text/markdown": [ "1.73205080756888" ], "text/plain": [ "[1] 1.732051" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "2.30258509299405" ], "text/latex": [ "2.30258509299405" ], "text/markdown": [ "2.30258509299405" ], "text/plain": [ "[1] 2.302585" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "2" ], "text/latex": [ "2" ], "text/markdown": [ "2" ], "text/plain": [ "[1] 2" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "2.71828182845905" ], "text/latex": [ "2.71828182845905" ], "text/markdown": [ "2.71828182845905" ], "text/plain": [ "[1] 2.718282" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Rには一般的な数学関数が定義されています\n", "# 関数は 関数名(引数名 = 値) の形で実行します\n", "sqrt(x = 2) # 平方根\n", "sqrt(3)\n", "log(10) # 自然対数\n", "log10(100) # 常用対数(底が10の対数)\n", "exp(1) # 指数関数\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## ドキュメントの参照\n", "\n", "`help(\"関数名\")`関数や`?関数名`のように、参照する関数名を与えて実行します。\n", "\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "vscode": { "languageId": "r" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MathFun package:base R Documentation\n", "\n", "_\bM_\bi_\bs_\bc_\be_\bl_\bl_\ba_\bn_\be_\bo_\bu_\bs _\bM_\ba_\bt_\bh_\be_\bm_\ba_\bt_\bi_\bc_\ba_\bl _\bF_\bu_\bn_\bc_\bt_\bi_\bo_\bn_\bs\n", "\n", "_\bD_\be_\bs_\bc_\br_\bi_\bp_\bt_\bi_\bo_\bn:\n", "\n", " ‘abs(x)’ computes the absolute value of x, ‘sqrt(x)’ computes the\n", " (principal) square root of x, sqrt{x}.\n", "\n", " The naming follows the standard for computer languages such as C\n", " or Fortran.\n", "\n", "_\bU_\bs_\ba_\bg_\be:\n", "\n", " abs(x)\n", " sqrt(x)\n", " \n", "_\bA_\br_\bg_\bu_\bm_\be_\bn_\bt_\bs:\n", "\n", " x: a numeric or ‘complex’ vector or array.\n", "\n", "_\bD_\be_\bt_\ba_\bi_\bl_\bs:\n", "\n", " These are internal generic primitive functions: methods can be\n", " defined for them individually or via the ‘Math’ group generic.\n", " For complex arguments (and the default method), ‘z’, ‘abs(z) ==\n", " Mod(z)’ and ‘sqrt(z) == z^0.5’.\n", "\n", " ‘abs(x)’ returns an ‘integer’ vector when ‘x’ is ‘integer’ or\n", " ‘logical’.\n", "\n", "_\bS_\b4 _\bm_\be_\bt_\bh_\bo_\bd_\bs:\n", "\n", " Both are S4 generic and members of the ‘Math’ group generic.\n", "\n", "_\bR_\be_\bf_\be_\br_\be_\bn_\bc_\be_\bs:\n", "\n", " Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S\n", " Language_. Wadsworth & Brooks/Cole.\n", "\n", "_\bS_\be_\be _\bA_\bl_\bs_\bo:\n", "\n", " ‘Arithmetic’ for simple, ‘log’ for logarithmic, ‘sin’ for\n", " trigonometric, and ‘Special’ for special mathematical functions.\n", "\n", " ‘plotmath’ for the use of ‘sqrt’ in plot annotation.\n", "\n", "_\bE_\bx_\ba_\bm_\bp_\bl_\be_\bs:\n", "\n", " require(stats) # for spline\n", " require(graphics)\n", " xx <- -9:9\n", " plot(xx, sqrt(abs(xx)), col = \"red\")\n", " lines(spline(xx, sqrt(abs(xx)), n=101), col = \"pink\")\n", " " ] }, { "name": "stdout", "output_type": "stream", "text": [ "log package:IRkernel R Documentation\n", "\n", "_\bK_\be_\br_\bn_\be_\bl _\bl_\bo_\bg_\bg_\bi_\bn_\bg _\bf_\bu_\bn_\bc_\bt_\bi_\bo_\bn_\bs\n", "\n", "_\bD_\be_\bs_\bc_\br_\bi_\bp_\bt_\bi_\bo_\bn:\n", "\n", " A set of exported logging utilities that have the capability to be\n", " used in upstream projects. Log level and log file can be set via R\n", " package options e.g. ‘options(jupyter.log_level = 2L)’ or from the\n", " environment variables JUPYTER_LOG_LEVEL and JUPYTER_LOGFILE.\n", "\n", "_\bU_\bs_\ba_\bg_\be:\n", "\n", " log_debug(...)\n", " \n", " log_info(...)\n", " \n", " log_error(...)\n", " \n", "_\bA_\br_\bg_\bu_\bm_\be_\bn_\bt_\bs:\n", "\n", " ...: message to log\n" ] } ], "source": [ "# ここではコメントアウトで実行しないようにしています。コメントアウトを外して実行してみましょう。\n", "# help(\"sqrt\")\n", "# ?log" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.2.3" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }