stMind

about Tech, Computer vision and Machine learning

Rでタブ区切りのデータを読み込む

Rでタブ区切りのデータを取得する方法。

基本的には、テーブル形式のデータを読み込むfunctionを使用するだけ。
タブ区切りのデータの時には、read.delimが使える。

> ufo<-read.delim("data/ufo/ufo_awesome.tsv", sep='¥t', header=FALSE, stringsAsFactor=FALSE)

最初の引数はデータファイルの相対パス。ちなみに、Machine Learning for Hackersで紹介されてるUFOの目撃情報データ。
johnmyleswhite/ML_for_Hackers · GitHub

sepはデフォルトで'¥t'なので省略可能。あと、header=FALSEは一行目を
ヘッダ行として使用しないという指定、
stringsAsFactors=FALSEは文字列からfactorへの変換しないという設定。

headすると、時期とどれくらいの時間見られたのか?といったデータが表示されます。
(レイアウトが崩れてるけど、V1からV6の六列)

> head(ufo)
        V1       V2                    V3 V4      V5
1 19951009 19951009         Iowa City, IA           
2 19951010 19951011         Milwaukee, WI     2 min.
3 19950101 19950103           Shelton, WA           
4 19950510 19950510          Columbia, MO     2 min.
5 19950611 19950614           Seattle, WA           
6 19951025 19951024  Brunswick County, ND    30 min.
                                                                                                                                                                                       V6
1                                          Man repts. witnessing &quot;flash, followed by a classic UFO, w/ a tailfin at back.&quot; Red color on top half of tailfin. Became triangular.
2                                                       Man  on Hwy 43 SW of Milwaukee sees large, bright blue light streak by his car, descend, turn, cross road ahead, strobe. Bizarre!
3 Telephoned Report:CA woman visiting daughter witness discs and triangular ships over Squaxin Island in Puget Sound. Dramatic.  Written report, with illustrations, submitted to NUFORC.
4                                                    Man repts. son&apos;s bizarre sighting of small humanoid creature in back yard.  Reptd. in Acteon Journal, St. Louis UFO newsletter.
5                                                            Anonymous caller repts. sighting 4 ufo&apos;s in NNE sky, 45 deg. above horizon.  (No other facts reptd.  No return tel. #.)
6                                                      Sheriff&apos;s office calls to rept. that deputy, 20 mi. SSE of Wilmington,  is looking at peculiar, bright white, strobing light.

Rコンソールを使うときは、setwd()やgetwd()が必要になるので、覚えておくと良いかも。

> getwd()
[1] "/Users/satojkovic/projects/ML_for_Hackers/01-Introduction"
> dir()
> setwd('data/')
> dir()
[1] "census.csv" "ufo"

などなど。