1 Introduction

1.1 RとRStudioの関係

  • Rがプログラミング言語の核心部分
  • RStudioは、Rを包み込んで、プログラミング活動をしやすい「環境」を提供してくれる。

1.2 RStudioの使い方

1.2.2 RMarkdownを使った作業の基本的流れ

http://sugiura-ken.org/wiki/wiki.cgi/exp?page=RStudio#p7

1.2.4 ルーティン

  1. Projectの設定
  2. RMarkdownファイルの作成
  3. 最初の二行以外はサンプルを削除
  4. ヘッダー部分の記入
  5. 作業
  6. 説明文を書く
  7. コードを書いて実行、修正
  8. RMarkdownファイルの保存
  • 上の左のほうのフロッピーのアイコン
  1. Knitで作業結果を見やすく出力
  2. Knit DirectoryをCurrent Working Directory
  3. メニューのSessionで、Set Working Directory
  4. Knit to HTML

1.2.5 授業用の設定のおすすめ

  • 授業の専用フォルダーを決める
  • デスクトップにはしない(クラウドに保存状態だとトラブル起きやすい)

1.3 RMarkdown作成の練習

  • 以下をRpractice.Rmdというファイルで保存し、
  • 結果をRpractice.htmlに出力してみる
(注)#はコメント
+   # 足す
-   # 引く
*   # かける
/   # 割る
^   # べき乗
sqrt(x) #ルート (1/2乗)
log(x)  #xの自然対数を取る。底はe 
log2(x) #xの対数を取る。底は2 

1.3.1 計算例

1 + 1
## [1] 2
8 - 5
## [1] 3
2 * 2
## [1] 4
15 / 3
## [1] 5
2^3
## [1] 8
sqrt(9)
## [1] 3
log(2.7)
## [1] 0.9932518
log2(8)
## [1] 3

1.3.2 変数の作成と値の代入

  • 変数という入れ物に値を代入する。
  • 変数名は、英数字と_が使える。
    • 数字で始めてはいけない
    • Rのコマンドと重複しないように

1.3.2.1 変数

  • 数字と文字を区別する
  • 文字はダブルクオートに入れる
  • <- で入力
abc <- 3        # abcという変数を作り、3を代入
abc         # abcの中身を表示
## [1] 3
abc <- 300  # abcの値に300が上書きされる
abc         # abcの中身を表示
## [1] 300
efj <- 6

jke <- 2987

1.3.2.2 変数の一覧表示: ls()

ls()        # これまでに作った変数一覧を表示
## [1] "abc" "efj" "jke"

1.3.2.3 変数の削除: rm()

rm(jke)     #変数jkeを削除

ls()
## [1] "abc" "efj"

1.3.2.4 文字列は”ダブルクオート”に入れる

namae <- "sugiura"  # namaeにはsugiuraという文字列が入る

1.3.2.5 変数の種類(クラス)の確認:class()

class(abc)      # class(変数名)で変数の「クラス」を表示
## [1] "numeric"
class(namae)
## [1] "character"

1.3.3 ベクトル

  • Rでは「配列」のことを「ベクトル(vector)」と呼ぶ

1.3.3.1 ベクトル=入れ物が複数ならんでいる変数

1.3.3.2 ベクトルへの代入法

    <- c(  ,  ,  ,  ) で入れる
kazu <- c(2,4,6,8)  # kazuという配列を作り、2, 4, 6, 8 を代入

kazu
## [1] 2 4 6 8

1.3.3.3 文字列をベクトルに入れる

names <- c("I","you","he") # 配列の中身に文字列を代入

names
## [1] "I"   "you" "he"

1.3.4 要素がいくつ入っているか調べる: length()

length(kazu)
## [1] 4
length(names)
## [1] 3

1.4 学習者コーパスNICER 1.3.2

1.4.1 NICERの概要

  • 日本語母語大学生・大学院生の英文エッセイ(381個)
  • 参照用に英語母語話者の英文エッセイ(71個)
  • 制限時間1時間(監督者あり)
  • 辞書など参考書なし
  • 三つのトピック(education, money, sports)から一つ選ぶ
  • ワープロソフト使用(スペルチェックと文書校正機能はオフ)
  • 書き終わった後、本人がスペルチェックのみ実行
  • データはCHATフォーマット形式に整形
  • 英語母語話者の添削文添付

1.4.2 ダウンロード

https://sugiura-ken.org/sgr/nicer/nicer-1-3-2/

  • NICER1_3_2.zip

  • マウス右ボタンクリックで、「開く」(すべて解凍)

1.4.3 内容

ファイル・フォルダ 説明
NICER1_3readme_2020-01-16.txt 概要の説明
Learner_Instructions.pdf 学習者用指示文
Learner_Profile_List.xls 学習者情報一覧
Learner_Questionnaire.pdf 学習者用質問事項
Native_Instructions.pdf 母語話者用指示文
Native_Profile_List.xls 母語話者情報一覧
Native_Questionnaire.pdf 母語話者向け質問事項
NICER_NNS/ 学習者コーパス・データ
NICER_NS/ 母語話者コーパス・データ

1.5 ファイルの読み込み

  • 学習者データ(NICER_NNS)を例に

1.5.1 準備:今どこにいるか?

"C:\Users\sugiura\ed\2023\2023後期\3. 木2 LC\2023年度テキスト\NICER1_3_2\2020-11-24NICER1_3_2\NICER_NNS"
  • メニュー「Session」> Set Working Directory > Choose Directory で目的のフォルダを開く
  • Knit ▼> Knit Diretory > Current Working Directory

1.5.2 場所の確認:getwd()

getwd()

1.5.3 場所の設定:setwd()

setwd("NICER1_3_2/2020-11-24NICER1_3_2/NICER_NNS")

1.5.4 ファイル一覧表示:list.files()

list.files()
##   [1] "JPN501.txt" "JPN502.txt" "JPN503.txt" "JPN504.txt" "JPN505.txt"
##   [6] "JPN506.txt" "JPN507.txt" "JPN508.txt" "JPN509.txt" "JPN510.txt"
##  [11] "JPN511.txt" "JPN512.txt" "JPN513.txt" "JPN514.txt" "JPN515.txt"
##  [16] "JPN516.txt" "JPN517.txt" "JPN518.txt" "JPN519.txt" "JPN520.txt"
##  [21] "JPN521.txt" "JPN522.txt" "JPN523.txt" "JPN524.txt" "JPN525.txt"
##  [26] "JPN526.txt" "JPN527.txt" "JPN528.txt" "JPN529.txt" "JPN530.txt"
##  [31] "JPN531.txt" "JPN532.txt" "JPN533.txt" "JPN534.txt" "JPN535.txt"
##  [36] "JPN536.txt" "JPN537.txt" "JPN538.txt" "JPN539.txt" "JPN540.txt"
##  [41] "JPN541.txt" "JPN542.txt" "JPN543.txt" "JPN544.txt" "JPN545.txt"
##  [46] "JPN546.txt" "JPN547.txt" "JPN548.txt" "JPN549.txt" "JPN550.txt"
##  [51] "JPN551.txt" "JPN552.txt" "JPN553.txt" "JPN554.txt" "JPN555.txt"
##  [56] "JPN556.txt" "JPN557.txt" "JPN558.txt" "JPN559.txt" "JPN560.txt"
##  [61] "JPN561.txt" "JPN562.txt" "JPN563.txt" "JPN564.txt" "JPN565.txt"
##  [66] "JPN566.txt" "JPN567.txt" "JPN568.txt" "JPN569.txt" "JPN570.txt"
##  [71] "JPN571.txt" "JPN572.txt" "JPN573.txt" "JPN574.txt" "JPN575.txt"
##  [76] "JPN576.txt" "JPN577.txt" "JPN578.txt" "JPN579.txt" "JPN580.txt"
##  [81] "JPN581.txt" "JPN582.txt" "JPN583.txt" "JPN584.txt" "JPN585.txt"
##  [86] "JPN586.txt" "JPN587.txt" "JPN588.txt" "JPN589.txt" "JPN590.txt"
##  [91] "JPN591.txt" "JPN592.txt" "JPN593.txt" "JPN594.txt" "JPN595.txt"
##  [96] "JPN596.txt" "JPN597.txt" "JPN598.txt" "JPN599.txt" "JPN600.txt"
## [101] "JPN601.txt" "JPN602.txt" "JPN603.txt" "JPN604.txt" "JPN605.txt"
## [106] "JPN606.txt" "JPN607.txt" "JPN608.txt" "JPN609.txt" "JPN610.txt"
## [111] "JPN611.txt" "JPN612.txt" "JPN613.txt" "JPN614.txt" "JPN615.txt"
## [116] "JPN616.txt" "JPN617.txt" "JPN618.txt" "JPN619.txt" "JPN620.txt"
## [121] "JPN621.txt" "JPN622.txt" "JPN623.txt" "JPN624.txt" "JPN625.txt"
## [126] "JPN626.txt" "JPN627.txt" "JPN628.txt" "JPN629.txt" "JPN630.txt"
## [131] "JPN631.txt" "JPN632.txt" "JPN633.txt" "JPN634.txt" "JPN635.txt"
## [136] "JPN636.txt" "JPN637.txt" "JPN638.txt" "JPN639.txt" "JPN640.txt"
## [141] "JPN641.txt" "JPN642.txt" "JPN643.txt" "JPN644.txt" "JPN645.txt"
## [146] "JPN646.txt" "JPN647.txt" "JPN648.txt" "JPN649.txt" "JPN650.txt"
## [151] "JPN651.txt" "JPN652.txt" "JPN653.txt" "JPN654.txt" "JPN655.txt"
## [156] "JPN656.txt" "JPN657.txt" "JPN658.txt" "JPN659.txt" "JPN660.txt"
## [161] "JPN661.txt" "JPN662.txt" "JPN663.txt" "JPN664.txt" "JPN665.txt"
## [166] "JPN666.txt" "JPN667.txt" "JPN668.txt" "JPN669.txt" "JPN670.txt"
## [171] "JPN671.txt" "JPN672.txt" "JPN673.txt" "JPN674.txt" "JPN675.txt"
## [176] "JPN676.txt" "JPN677.txt" "JPN678.txt" "JPN679.txt" "JPN680.txt"
## [181] "JPN681.txt" "JPN682.txt" "JPN683.txt" "JPN684.txt" "JPN685.txt"
## [186] "JPN686.txt" "JPN687.txt" "JPN688.txt" "JPN689.txt" "JPN690.txt"
## [191] "JPN691.txt" "JPN692.txt" "JPN693.txt" "JPN694.txt" "JPN695.txt"
## [196] "JPN696.txt" "JPN697.txt" "JPN698.txt" "JPN699.txt" "JPN700.txt"
## [201] "JPN701.txt" "JPN702.txt" "JPN703.txt" "JPN704.txt" "JPN705.txt"
## [206] "JPN706.txt" "JPN707.txt" "JPN708.txt" "JPN709.txt" "JPN710.txt"
## [211] "JPN711.txt" "JPN712.txt" "JPN713.txt" "JPN714.txt" "JPN715.txt"
## [216] "JPN716.txt" "JPN717.txt" "JPN718.txt" "JPN719.txt" "JPN720.txt"
## [221] "JPN721.txt" "JPN722.txt" "JPN723.txt" "JPN724.txt" "JPN725.txt"
## [226] "JPN726.txt" "JPN727.txt" "JPN728.txt" "JPN729.txt" "JPN730.txt"
## [231] "JPN731.txt" "JPN732.txt" "JPN733.txt" "JPN734.txt" "JPN735.txt"
## [236] "JPN736.txt" "JPN737.txt" "JPN738.txt" "JPN739.txt" "JPN740.txt"
## [241] "JPN741.txt" "JPN742.txt" "JPN743.txt" "JPN744.txt" "JPN745.txt"
## [246] "JPN746.txt" "JPN747.txt" "JPN748.txt" "JPN749.txt" "JPN750.txt"
## [251] "JPN751.txt" "JPN752.txt" "JPN753.txt" "JPN754.txt" "JPN755.txt"
## [256] "JPN756.txt" "JPN757.txt" "JPN758.txt" "JPN759.txt" "JPN760.txt"
## [261] "JPN761.txt" "JPN762.txt" "JPN763.txt" "JPN764.txt" "JPN765.txt"
## [266] "JPN766.txt" "JPN767.txt" "JPN768.txt" "JPN769.txt" "JPN770.txt"
## [271] "JPN771.txt" "JPN772.txt" "JPN773.txt" "JPN774.txt" "JPN775.txt"
## [276] "JPN776.txt" "JPN777.txt" "JPN778.txt" "JPN779.txt" "JPN780.txt"
## [281] "JPN781.txt" "JPN782.txt" "JPN783.txt" "JPN784.txt" "JPN785.txt"
## [286] "JPN786.txt" "JPN787.txt" "JPN788.txt" "JPN789.txt" "JPN790.txt"
## [291] "JPN791.txt" "JPN792.txt" "JPN793.txt" "JPN794.txt" "JPN795.txt"
## [296] "JPN796.txt" "JPN797.txt" "JPN798.txt" "JPN799.txt" "JPN800.txt"
## [301] "JPN801.txt" "JPN802.txt" "JPN803.txt" "JPN804.txt" "JPN805.txt"
## [306] "JPN806.txt" "JPN807.txt" "JPN808.txt" "JPN809.txt" "JPN810.txt"
## [311] "JPN811.txt" "JPN812.txt" "JPN813.txt" "JPN814.txt" "JPN815.txt"
## [316] "JPN816.txt" "JPN817.txt" "JPN818.txt" "JPN819.txt" "JPN820.txt"
## [321] "JPN821.txt" "JPN822.txt" "JPN823.txt" "JPN824.txt" "JPN825.txt"
## [326] "JPN826.txt" "JPN827.txt" "JPN828.txt" "JPN829.txt" "JPN830.txt"
## [331] "JPN831.txt" "JPN832.txt" "JPN833.txt" "JPN834.txt" "JPN835.txt"
## [336] "JPN836.txt" "JPN837.txt" "JPN838.txt" "JPN839.txt" "JPN840.txt"
## [341] "JPN841.txt" "JPN842.txt" "JPN843.txt" "JPN844.txt" "JPN845.txt"
## [346] "JPN846.txt" "JPN847.txt" "JPN848.txt" "JPN849.txt" "JPN850.txt"
## [351] "JPN851.txt" "JPN852.txt" "JPN853.txt" "JPN854.txt" "JPN855.txt"
## [356] "JPN856.txt" "JPN857.txt" "JPN858.txt" "JPN859.txt" "JPN860.txt"
## [361] "JPN861.txt" "JPN862.txt" "JPN863.txt" "JPN864.txt" "JPN865.txt"
## [366] "JPN866.txt" "JPN867.txt" "JPN868.txt" "JPN869.txt" "JPN870.txt"
## [371] "JPN871.txt" "JPN872.txt" "JPN873.txt" "JPN874.txt" "JPN875.txt"
## [376] "JPN876.txt" "JPN877.txt" "JPN878.txt" "JPN879.txt" "JPN880.txt"
## [381] "JPN881.txt"

1.5.5 ファイルの読み込み: readLines()

  • ファイル名は文字列なので、ダブルクオートで囲む
  • 読み込んだものを変数に入れる
    • 例: JPN501.txtのファイルを読み込んで、jpn501という変数に入れる
jpn501 <- readLines("JPN501.txt")
  • 全体がベクトル(一文一行が一要素)

1.5.6 変数の中身を見てみる

jpn501
##   [1] "@Begin"                                                                                                                                    
##   [2] "@Participants:\tJPN501"                                                                                                                    
##   [3] "@PID:\tPIDJP501"                                                                                                                           
##   [4] "@Age:\t21"                                                                                                                                 
##   [5] "@Sex:\tF"                                                                                                                                  
##   [6] "@YearInSchool:\tU2"                                                                                                                        
##   [7] "@Major:\tagriculture"                                                                                                                      
##   [8] "@StudyHistory:\t8"                                                                                                                         
##   [9] "@OtherLanguage:\tChinese=1.0;none="                                                                                                        
##  [10] "@Qualification:\tTOEIC=590(2013);none=;none="                                                                                              
##  [11] "@Abroad:\tnone=;none="                                                                                                                     
##  [12] "@Reading:\t3"                                                                                                                              
##  [13] "@Writing:\t2"                                                                                                                              
##  [14] "@Listening:\t2"                                                                                                                            
##  [15] "@Speaking:\t1"                                                                                                                             
##  [16] "@JapaneseEssay:\t4"                                                                                                                        
##  [17] "@EnglishEssayEx:\t3"                                                                                                                       
##  [18] "@EnglishEssay:\t2"                                                                                                                         
##  [19] "@Difficulty:\t"                                                                                                                            
##  [20] "@EssayTraining:\t3"                                                                                                                        
##  [21] "@SelfEval:\t2"                                                                                                                             
##  [22] "@TopicEase:\t4"                                                                                                                            
##  [23] "@Topic:\tsports"                                                                                                                           
##  [24] "@Criterion:\t4"                                                                                                                            
##  [25] "@Proctor:\t1"                                                                                                                              
##  [26] "@Comments:\t"                                                                                                                              
##  [27] "@Date:\t2013-12-17"                                                                                                                        
##  [28] "@Version:\t1.3.1"                                                                                                                          
##  [29] "*JPN501:\tWhat kind of sports do you like?"                                                                                                
##  [30] "%NTV:\tOK"                                                                                                                                 
##  [31] "%COM:\t"                                                                                                                                   
##  [32] "*JPN501:\tDo you like soccer, base ball or swimming?"                                                                                      
##  [33] "%NTV:\tDo you like soccer, baseball, or swimming?"                                                                                         
##  [34] "%COM:\t\"Baseball\" is one word. In lists with three or more items, put a comma between each item, including one before the final \"and\"."
##  [35] "*JPN501:\tThere are many and variety sports around the world."                                                                             
##  [36] "%NTV:\tThere are many varieties of sports around the world."                                                                               
##  [37] "%COM:\t"                                                                                                                                   
##  [38] "*JPN501:\tA country has some traditional sports."                                                                                          
##  [39] "%NTV:\tMost countries have some traditional sports."                                                                                       
##  [40] "%COM:\t"                                                                                                                                   
##  [41] "*JPN501:\tOf course, there are some traditional sports in Japan."                                                                          
##  [42] "%NTV:\tOK"                                                                                                                                 
##  [43] "%COM:\t"                                                                                                                                   
##  [44] "*JPN501:\tThey are called \"BUDO\"."                                                                                                       
##  [45] "%NTV:\tThey are called budo."                                                                                                              
##  [46] "%COM:\tThis word does not require capitalization."                                                                                         
##  [47] "*JPN501:\tBUDO are JYUDO, KENDO, KYUDO and so on."                                                                                         
##  [48] "%NTV:\tBudo include judo, kendo, kyudo, and so on."                                                                                        
##  [49] "%COM:\tThese words do not require capitalization."                                                                                         
##  [50] "*JPN501:\tIf you play BUDO, there is an important thing that you must remember."                                                           
##  [51] "%NTV:\tIf you play budo, there is one important thing you must remember. "                                                                 
##  [52] "%COM:\t"                                                                                                                                   
##  [53] "*JPN501:\tIt is \"REI\"."                                                                                                                  
##  [54] "%NTV:\tIt is rei."                                                                                                                         
##  [55] "%COM:\t"                                                                                                                                   
##  [56] "%par:"                                                                                                                                     
##  [57] "*JPN501:\tFirst,  \"REI\" is the feeling that you thank people who are involved in the game."                                              
##  [58] "%NTV:\tFirst, rei is the custom of thanking people who are involved in the game."                                                          
##  [59] "%COM:\tIt seems that \"rei\" is describing an action here, so it would be a \"custom\" rather than a \"feeling\"."                         
##  [60] "*JPN501:\tFor example, when the game will start, players bow each other."                                                                  
##  [61] "%NTV:\tFor example, when the game starts, players bow to each other."                                                                      
##  [62] "%COM:\t"                                                                                                                                   
##  [63] "*JPN501:\tand when the game ended, players bow each other, too."                                                                           
##  [64] "%NTV:\tWhen the game ends, players bow to each other, too."                                                                                
##  [65] "%COM:\t"                                                                                                                                   
##  [66] "*JPN501:\tPlayers cannot play a game without an enemy."                                                                                    
##  [67] "%NTV:\tOK"                                                                                                                                 
##  [68] "%COM:\t"                                                                                                                                   
##  [69] "*JPN501:\tThis is the expression that players thank each other for playing the game."                                                      
##  [70] "%NTV:\tThis is the expression by which players thank each other for playing the game."                                                     
##  [71] "%COM:\t"                                                                                                                                   
##  [72] "%par:"                                                                                                                                     
##  [73] "*JPN501:\tSecondly,  \"REI\" is the feeling that you thank the place where you play the game."                                             
##  [74] "%NTV:\tSecond, rei is the custom of thanking the place where you play the game."                                                           
##  [75] "%COM:\tThe \"-ly\" ending is old-fashioned -- just say \"first\", \"second\", etc."                                                        
##  [76] "*JPN501:\tBUDO players must bow when they enter the BUDOJYO (the playing place) and when they leave there."                                
##  [77] "%NTV:\tBudo players must bow when they enter the budojyo (playing place) and when they leave."                                             
##  [78] "%COM:\t"                                                                                                                                   
##  [79] "*JPN501:\tAnd you must clean the BUDOJYO because you thank the BUDOJYO for playing there."                                                 
##  [80] "%NTV:\tPlayers must also clean the budojyo as thanks for playing there."                                                                   
##  [81] "%COM:\tAvoid starting sentences with coordinating conjunctions (\"and\" here)."                                                            
##  [82] "%par:"                                                                                                                                     
##  [83] "*JPN501:\tFinally, \"REI\" is the feeling that you thank yourself."                                                                        
##  [84] "%NTV:\tFinally, rei is the custom of thanking yourself."                                                                                   
##  [85] "%COM:\t"                                                                                                                                   
##  [86] "*JPN501:\tIn BUDO, you think about yourself deeply."                                                                                       
##  [87] "%NTV:\tIn budo, you think about yourself deeply."                                                                                          
##  [88] "%COM:\t"                                                                                                                                   
##  [89] "*JPN501:\tFor example, in soccer, players have many team mates."                                                                           
##  [90] "%NTV:\tFor example, in soccer, players have many teammates."                                                                               
##  [91] "%COM:\t"                                                                                                                                   
##  [92] "*JPN501:\tBut many BUDO have no team mate, you must play with yourself."                                                                   
##  [93] "%NTV:\tHowever, many budo have no teammate; instead, you must play against yourself."                                                      
##  [94] "%COM:\tAvoid starting sentences with coordinating conjunctions. You can change \"but\" to \"however\"."                                    
##  [95] "*JPN501:\tSo you must fight not only the enemy but also yourself who feel awful, tired, sad, pain and so on in the BUDO game."             
##  [96] "%NTV:\tThis means, in budo, you must fight not only the enemy but also yourself, even if you feel awful, tired, sad, in pain, and so on."  
##  [97] "%COM:\tAvoid starting sentences with coordinating conjunctions. You can change \"so\" to \"this means\"."                                  
##  [98] "*JPN501:\tYou must beat weak yourself."                                                                                                    
##  [99] "%NTV:\tNG"                                                                                                                                 
## [100] "%COM:\t"                                                                                                                                   
## [101] "*JPN501:\tIf you can beat him, you would feel how much you could grow."                                                                    
## [102] "%NTV:\tIf you can beat the enemy, you can feel how much you can grow."                                                                     
## [103] "%COM:\tIt's not clear who \"him\" refers to."                                                                                              
## [104] "*JPN501:\tThen, you can thank yourself for your efforts."                                                                                  
## [105] "%NTV:\tOK"                                                                                                                                 
## [106] "%COM:\t"                                                                                                                                   
## [107] "%par:"                                                                                                                                     
## [108] "*JPN501:\tBUDO can make your body strong."                                                                                                 
## [109] "%NTV:\tBudo can make your body strong."                                                                                                    
## [110] "%COM:\t"                                                                                                                                   
## [111] "*JPN501:\tThis is the same good point which other sports give."                                                                            
## [112] "%NTV:\tThis is the same good point that other sports give."                                                                                
## [113] "%COM:\tUse \"that\", not \"which\", with restrictive clauses."                                                                             
## [114] "*JPN501:\tBut BUDO must give you many other good points, for example, mental strong, taught  from \"REI\"."                                
## [115] "%NTV:\tHowever, because of rei, budo provides many additional good points, such as mental strength."                                       
## [116] "%COM:\t"                                                                                                                                   
## [117] "*JPN501:\tSo, today, many people plays and loves BUDO around the world."                                                                   
## [118] "%NTV:\tThis is why, today, many people play and love budo around the world."                                                               
## [119] "%COM:\t"                                                                                                                                   
## [120] "*JPN501:\tWe Japanese should be proud of and teach more many people around the world about this traditional sports \"BUDO\"."              
## [121] "%NTV:\tWe Japanese should be proud of and teach many more people around the world about our traditional sports, budo."                     
## [122] "%COM:\t"                                                                                                                                   
## [123] "@End"

1.6 複数ファイルの読み込み

1.6.1 学習者のデータを10ファイル読み込む

jpn501 <- readLines("JPN501.txt")
## Warning in readLines("JPN501.txt"): 'JPN501.txt'
## で不完全な最終行が見つかりました
jpn502 <- readLines("JPN502.txt")
jpn503 <- readLines("JPN503.txt")
## Warning in readLines("JPN503.txt"): 'JPN503.txt'
## で不完全な最終行が見つかりました
jpn504 <- readLines("JPN504.txt")
## Warning in readLines("JPN504.txt"): 'JPN504.txt'
## で不完全な最終行が見つかりました
jpn505 <- readLines("JPN505.txt")
## Warning in readLines("JPN505.txt"): 'JPN505.txt'
## で不完全な最終行が見つかりました
jpn506 <- readLines("JPN506.txt")
## Warning in readLines("JPN506.txt"): 'JPN506.txt'
## で不完全な最終行が見つかりました
jpn507 <- readLines("JPN507.txt")
## Warning in readLines("JPN507.txt"): 'JPN507.txt'
## で不完全な最終行が見つかりました
jpn508 <- readLines("JPN508.txt")
## Warning in readLines("JPN508.txt"): 'JPN508.txt'
## で不完全な最終行が見つかりました
jpn509 <- readLines("JPN509.txt")
## Warning in readLines("JPN509.txt"): 'JPN509.txt'
## で不完全な最終行が見つかりました
jpn510 <- readLines("JPN510.txt")
## Warning in readLines("JPN510.txt"): 'JPN510.txt'
## で不完全な最終行が見つかりました

1.6.2 母語話者ファイルも10ファイル読み込む

  • データの入っているフォルダーが違うので、注意。(setwd()で働く場所をセットする)
  • 隣のフォルダー(NICER_NS)を一時的に使うには、隣のフォルダー名の前に ../ を付けて表記する
setwd("../NICER_NS")

ns501 <- readLines("NS501.txt")
ns502 <- readLines("NS502.txt")
ns503 <- readLines("NS503.txt")
ns504 <- readLines("NS504.txt")
ns505 <- readLines("NS505.txt")
ns506 <- readLines("NS506.txt")
ns507 <- readLines("NS507.txt")
ns508 <- readLines("NS508.txt")
ns509 <- readLines("NS509.txt")
ns510 <- readLines("NS510.txt")

1.6.3 できたかどうか確認:ls()

ls()
##  [1] "abc"    "efj"    "jpn501" "jpn502" "jpn503" "jpn504" "jpn505" "jpn506"
##  [9] "jpn507" "jpn508" "jpn509" "jpn510" "kazu"   "namae"  "names"  "ns501" 
## [17] "ns502"  "ns503"  "ns504"  "ns505"  "ns506"  "ns507"  "ns508"  "ns509" 
## [25] "ns510"

1.7 文字列の検索:grep(“検索文字列”, 変数名)

grep("However", ns501)
## [1] 45 54 84
  • 要素番号(=行番号)が表示される。

1.7.1 番号でなく中の値を出力するオプション: value=T

1.7.2 “However”

grep("However", ns501, value=T)
## [1] "*NS501:\tHowever in the French educational system instead of a head or a body there is a thesis and an anti-thesis or point and counter point in which the writer must oppose his or her original statements."
## [2] "*NS501:\tHowever what the French lose in logical flow they gain in critical thinking."                                                                                                                        
## [3] "*NS501:\tHowever, sadly with the continuous failings of the American educational system, these lofty dreams yet remain dreams for a generation of potential Newtons and Einsteins."

1.7.3 “however”

grep("however", ns501, value=T)   # 小文字 h
## [1] "*NS501:\tThis makes the facts easy to access, however, it does not force the writer to challenge his or her own logic in the process, leaving the ideas themselves rigid."

1.8 grepで正規表現を使う

grep("[hH]owever", ns501, value=T)
## [1] "*NS501:\tHowever in the French educational system instead of a head or a body there is a thesis and an anti-thesis or point and counter point in which the writer must oppose his or her original statements."
## [2] "*NS501:\tThis makes the facts easy to access, however, it does not force the writer to challenge his or her own logic in the process, leaving the ideas themselves rigid."                                    
## [3] "*NS501:\tHowever what the French lose in logical flow they gain in critical thinking."                                                                                                                        
## [4] "*NS501:\tHowever, sadly with the continuous failings of the American educational system, these lofty dreams yet remain dreams for a generation of potential Newtons and Einsteins."

1.8.1 もしくは、grepの大文字小文字区別しないオプションを付ける: ignore.case=T

grep("however", ns501, value=T, ignore.case=T)
## [1] "*NS501:\tHowever in the French educational system instead of a head or a body there is a thesis and an anti-thesis or point and counter point in which the writer must oppose his or her original statements."
## [2] "*NS501:\tThis makes the facts easy to access, however, it does not force the writer to challenge his or her own logic in the process, leaving the ideas themselves rigid."                                    
## [3] "*NS501:\tHowever what the French lose in logical flow they gain in critical thinking."                                                                                                                        
## [4] "*NS501:\tHowever, sadly with the continuous failings of the American educational system, these lofty dreams yet remain dreams for a generation of potential Newtons and Einsteins."

1.9 補足:ウィンドウを開いてファイルを選ぶ方法:choose.files()

  • ファイル名を指定するかわりに。
  • MacOSの場合は、file.choose()
  • 別のウィンドウが開く。RStudioのウィンドウの裏に開かれるので注意。
  • ↓ 例:ウィンドウを開いて JPN502.txtを読み込んでみる。
jpn502 <- readLines(choose.files())

1.10 ■今日の課題■

* 学習者と母語話者が使っている表現を検索して比べてみよう。