Big-data-analytics-r-introduction

提供:Dev Guides
移動先:案内検索

ビッグデータ分析-Rの概要

このセクションでは、ユーザーにRプログラミング言語を紹介します。 Rはhttps://cran.r-project.org/[cran website]からダウンロードできます。 Windowsユーザーの場合、https://cran.r-project.org/bin/windows/Rtools/[install rtools]およびhttps://www.rstudio.com/[rstudio IDE]が役立ちます。

*R* の背後にある一般的な概念は、C、C ++、Fortranなどのコンパイル言語で開発された他のソフトウェアへのインターフェイスとして機能し、データを分析するための対話型ツールをユーザーに提供することです。

ブックのzipファイル bda/part2/R_introduction のフォルダーに移動し、 R_introduction.Rproj ファイルを開きます。 これにより、RStudioセッションが開きます。 次に、01_vectors.Rファイルを開きます。 スクリプトを1行ずつ実行し、コード内のコメントに従ってください。 学習するための別の便利なオプションは、コードを入力するだけです。これは、R構文に慣れるのに役立ちます。 Rでは、コメントは#記号で記述されます。

ブックでRコードを実行した結果を表示するために、コードが評価された後、Rが返す結果にコメントが付けられます。 このようにして、コードをコピーして本に貼り付け、Rでそのセクションを直接試すことができます。

# Create a vector of numbers
numbers = c(1, 2, 3, 4, 5)
print(numbers)

# [1] 1 2 3 4 5
# Create a vector of letters
ltrs = c('a', 'b', 'c', 'd', 'e')
# [1] "a" "b" "c" "d" "e"

# Concatenate both
mixed_vec = c(numbers, ltrs)
print(mixed_vec)
# [1] "1" "2" "3" "4" "5" "a" "b" "c" "d" "e"

前のコードで何が起こったのか分析しましょう。 数字と文字でベクトルを作成できることがわかります。 事前にどのタイプのデータ型が必要かをRに伝える必要はありませんでした。 最後に、数字と文字の両方を含むベクターを作成できました。 ベクトルmixed_vecは数値を文字に強制しました。これは、引用符内で値がどのように印刷されるかを視覚化することで確認できます。

次のコードは、関数クラスによって返されるさまざまなベクトルのデータ型を示しています。 クラス関数を使用してオブジェクトに「問い合わせ」を行い、自分のクラスが何であるかを尋ねることが一般的です。

### Evaluate the data types using class

### One dimensional objects
# Integer vector
num = 1:10
class(num)
# [1] "integer"

# Numeric vector, it has a float, 10.5
num = c(1:10, 10.5)
class(num)
# [1] "numeric"

# Character vector
ltrs = letters[1:10]
class(ltrs)
# [1] "character"

# Factor vector
fac = as.factor(ltrs)
class(fac)
# [1] "factor"

Rは2次元オブジェクトもサポートします。 次のコードには、Rで使用される2つの最も一般的なデータ構造の例があります:matrixとdata.frameです。

# Matrix
M = matrix(1:12, ncol = 4)
#      [,1] [,2] [,3] [,4]
# [1,]    1    4    7   10
# [2,]    2    5    8   11
# [3,]    3    6    9   12
lM = matrix(letters[1:12], ncol = 4)
#     [,1] [,2] [,3] [,4]
# [1,] "a"  "d"  "g"  "j"
# [2,] "b"  "e"  "h"  "k"
# [3,] "c"  "f"  "i"  "l"

# Coerces the numbers to character
# cbind concatenates two matrices (or vectors) in one matrix
cbind(M, lM)
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
# [1,] "1"  "4"  "7"  "10" "a"  "d"  "g"  "j"
# [2,] "2"  "5"  "8"  "11" "b"  "e"  "h"  "k"
# [3,] "3"  "6"  "9"  "12" "c"  "f"  "i"  "l"

class(M)
# [1] "matrix"
class(lM)
# [1] "matrix"

# data.frame
# One of the main objects of R, handles different data types in the same object.
# It is possible to have numeric, character and factor vectors in the same data.frame

df = data.frame(n = 1:5, l = letters[1:5])
df
#   n l
# 1 1 a
# 2 2 b
# 3 3 c
# 4 4 d
# 5 5 e

前の例で示したように、同じオブジェクトで異なるデータ型を使用することが可能です。 一般に、これはデータがデータベースでどのように表示されるかであり、データのAPI部分はテキストまたは文字ベクトルと他の数値です。 Inは、割り当てる統計データ型を決定し、それに適切なRデータ型を使用するアナリストの仕事です。 統計では、通常、変数は次のタイプであると考えます-

  • 数値
  • 名義またはカテゴリー
  • 序数

Rでは、ベクトルは次のクラスになります-

  • 数値-整数
  • 因子
  • 順序付けられた因子

Rは、変数の統計タイプごとにデータタイプを提供します。 ただし、順序付けられた因子はめったに使用されませんが、関数因子によって作成されるか、順序付けられます。

次のセクションでは、インデックス付けの概念を扱います。 これは非常に一般的な操作であり、オブジェクトのセクションを選択して変換する問題を処理します。

# Let's create a data.frame
df = data.frame(numbers = 1:26, letters)
head(df)
#      numbers  letters
# 1       1       a
# 2       2       b
# 3       3       c
# 4       4       d
# 5       5       e
# 6       6       f

# str gives the structure of a data.frame, it’s a good summary to inspect an object
str(df)
#   'data.frame': 26 obs. of  2 variables:
#   $ numbers: int  1 2 3 4 5 6 7 8 9 10 ...
#   $ letters: Factor w/26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...

# The latter shows the letters character vector was coerced as a factor.
# This can be explained by the stringsAsFactors = TRUE argumnet in data.frame
# read ?data.frame for more information

class(df)
# [1] "data.frame"

### Indexing
# Get the first row
df[1, ]
#     numbers  letters
# 1       1       a

# Used for programming normally - returns the output as a list
df[1, , drop = TRUE]
# $numbers
# [1] 1
#
# $letters
# [1] a
# Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z

# Get several rows of the data.frame
df[5:7, ]
#      numbers  letters
# 5       5       e
# 6       6       f
# 7       7       g

### Add one column that mixes the numeric column with the factor column
df$mixed = paste(df$numbers, df$letters, sep = ’’)

str(df)
# 'data.frame': 26 obs. of  3 variables:
# $ numbers: int  1 2 3 4 5 6 7 8 9 10 ...
# $ letters: Factor w/26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
# $ mixed  : chr  "1a" "2b" "3c" "4d" ...

### Get columns
# Get the first column
df[, 1]
# It returns a one dimensional vector with that column

# Get two columns
df2 = df[, 1:2]
head(df2)

#      numbers  letters
# 1       1       a
# 2       2       b
# 3       3       c
# 4       4       d
# 5       5       e
# 6       6       f

# Get the first and third columns
df3 = df[, c(1, 3)]
df3[1:3, ]

#      numbers  mixed
# 1       1     1a
# 2       2     2b
# 3       3     3c

### Index columns from their names
names(df)
# [1] "numbers" "letters" "mixed"
# This is the best practice in programming, as many times indeces change, but
variable names don’t
# We create a variable with the names we want to subset
keep_vars = c("numbers", "mixed")
df4 = df[, keep_vars]

head(df4)
#      numbers  mixed
# 1       1     1a
# 2       2     2b
# 3       3     3c
# 4       4     4d
# 5       5     5e
# 6       6     6f

### subset rows and columns
# Keep the first five rows
df5 = df[1:5, keep_vars]
df5

#      numbers  mixed
# 1       1     1a
# 2       2     2b
# 3       3     3c
# 4       4     4d
# 5       5     5e

# subset rows using a logical condition
df6 = df[df$numbers < 10, keep_vars]
df6

#      numbers  mixed
# 1       1     1a
# 2       2     2b
# 3       3     3c
# 4       4     4d
# 5       5     5e
# 6       6     6f
# 7       7     7g
# 8       8     8h
# 9       9     9i