mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 18:24:39 +02:00
still on a middle way
This commit is contained in:
@@ -221,26 +221,25 @@ class(-Inf) # "numeric"
|
|||||||
# 不正な計算は "not-a-number"になる
|
# 不正な計算は "not-a-number"になる
|
||||||
0 / 0 # NaN
|
0 / 0 # NaN
|
||||||
class(NaN) # "numeric"
|
class(NaN) # "numeric"
|
||||||
# You can do arithmetic on two vectors with length greater than 1,
|
# 長さが1より大きなベクター同士で計算ができます
|
||||||
# so long as the larger vector's length is an integer multiple of the smaller
|
# どちらかが長い場合、短い方は何度も繰り返して使われます
|
||||||
c(1,2,3) + c(1,2,3) # 2 4 6
|
c(1,2,3) + c(1,2,3) # 2 4 6
|
||||||
|
|
||||||
|
# 文字
|
||||||
# CHARACTERS
|
# Rでは、文字列と文字に区別がありません
|
||||||
# There's no difference between strings and characters in R
|
|
||||||
"Horatio" # "Horatio"
|
"Horatio" # "Horatio"
|
||||||
class("Horatio") # "character"
|
class("Horatio") # "character"
|
||||||
class('H') # "character"
|
class('H') # "character"
|
||||||
# Those were both character vectors of length 1
|
# 上記は両方とも、長さ1のベクターです
|
||||||
# Here is a longer one:
|
# 以下は、より長いものです
|
||||||
c('alef', 'bet', 'gimmel', 'dalet', 'he')
|
c('alef', 'bet', 'gimmel', 'dalet', 'he')
|
||||||
# =>
|
# =>
|
||||||
# "alef" "bet" "gimmel" "dalet" "he"
|
# "alef" "bet" "gimmel" "dalet" "he"
|
||||||
length(c("Call","me","Ishmael")) # 3
|
length(c("Call","me","Ishmael")) # 3
|
||||||
# You can do regex operations on character vectors:
|
# 正規表現処理を文字ベクターに使えます
|
||||||
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
|
substr("Fortuna multis dat nimis, nulli satis.", 9, 15) # "multis "
|
||||||
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
|
gsub('u', 'ø', "Fortuna multis dat nimis, nulli satis.") # "Fortøna møltis dat nimis, nølli satis."
|
||||||
# R has several built-in character vectors:
|
# Rはいくつかの文字ベクターを組み込みで持っています
|
||||||
letters
|
letters
|
||||||
# =>
|
# =>
|
||||||
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
|
# [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
|
||||||
@@ -248,40 +247,40 @@ letters
|
|||||||
month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
|
month.abb # "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
|
||||||
|
|
||||||
|
|
||||||
# LOGICALS
|
# 論理
|
||||||
# In R, a "logical" is a boolean
|
# Rでは、Booleanは論理(logical)型です
|
||||||
class(TRUE) # "logical"
|
class(TRUE) # "logical"
|
||||||
class(FALSE) # "logical"
|
class(FALSE) # "logical"
|
||||||
# Their behavior is normal
|
# 以下は正しい動きです
|
||||||
TRUE == TRUE # TRUE
|
TRUE == TRUE # TRUE
|
||||||
TRUE == FALSE # FALSE
|
TRUE == FALSE # FALSE
|
||||||
FALSE != FALSE # FALSE
|
FALSE != FALSE # FALSE
|
||||||
FALSE != TRUE # TRUE
|
FALSE != TRUE # TRUE
|
||||||
# Missing data (NA) is logical, too
|
# 無いデータ (NA) も論理型です
|
||||||
class(NA) # "logical"
|
class(NA) # "logical"
|
||||||
# Here we get a logical vector with many elements:
|
# 以下のようにすると、複数の要素を持つ、論理型ベクターが返ります
|
||||||
c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE
|
c('Z', 'o', 'r', 'r', 'o') == "Zorro" # FALSE FALSE FALSE FALSE FALSE
|
||||||
c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE
|
c('Z', 'o', 'r', 'r', 'o') == "Z" # TRUE FALSE FALSE FALSE FALSE
|
||||||
|
|
||||||
|
|
||||||
# FACTORS
|
# ファクター
|
||||||
# The factor class is for categorical data
|
# ファクタークラスは、カテゴリカルデータようのクラスです
|
||||||
# Factors can be ordered (like childrens' grade levels) or unordered (like gender)
|
# ファクターは、子供の学年のように順序がつけられるものか、性別のように順序がないものがあります
|
||||||
factor(c("female", "female", "male", "NA", "female"))
|
factor(c("female", "female", "male", "NA", "female"))
|
||||||
# female female male NA female
|
# female female male NA female
|
||||||
# Levels: female male NA
|
# Levels: female male NA
|
||||||
# The "levels" are the values the categorical data can take
|
# "levels" は、カテゴリカルデータがとりうる値を返します
|
||||||
levels(factor(c("male", "male", "female", "NA", "female"))) # "female" "male" "NA"
|
levels(factor(c("male", "male", "female", "NA", "female"))) # "female" "male" "NA"
|
||||||
# If a factor vector has length 1, its levels will have length 1, too
|
# ファクターベクターの長さが1ならば、そのlevelも1です
|
||||||
length(factor("male")) # 1
|
length(factor("male")) # 1
|
||||||
length(levels(factor("male"))) # 1
|
length(levels(factor("male"))) # 1
|
||||||
# Factors are commonly seen in data frames, a data structure we will cover later
|
# ファクターは、この後で紹介するデータフレーム(というデータ型)内で、よくみられます
|
||||||
data(infert) # "Infertility after Spontaneous and Induced Abortion"
|
data(infert) # "Infertility after Spontaneous and Induced Abortion"
|
||||||
levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs"
|
levels(infert$education) # "0-5yrs" "6-11yrs" "12+ yrs"
|
||||||
|
|
||||||
|
|
||||||
# NULL
|
# NULL
|
||||||
# "NULL" is a weird one; use it to "blank out" a vector
|
# "NULL" は変わった型です。ベクターを空にするときに使います
|
||||||
class(NULL) # NULL
|
class(NULL) # NULL
|
||||||
parakeet
|
parakeet
|
||||||
# =>
|
# =>
|
||||||
@@ -292,11 +291,11 @@ parakeet
|
|||||||
# NULL
|
# NULL
|
||||||
|
|
||||||
|
|
||||||
# TYPE COERCION
|
# 型の強制
|
||||||
# Type-coercion is when you force a value to take on a different type
|
# 型の強制は、ある値を、強制的にある型として利用する事です
|
||||||
as.character(c(6, 8)) # "6" "8"
|
as.character(c(6, 8)) # "6" "8"
|
||||||
as.logical(c(1,0,1,1)) # TRUE FALSE TRUE TRUE
|
as.logical(c(1,0,1,1)) # TRUE FALSE TRUE TRUE
|
||||||
# If you put elements of different types into a vector, weird coercions happen:
|
# さまざまな要素が入っているベクターに対して型の強制を行うと、おかしなことになります
|
||||||
c(TRUE, 4) # 1 4
|
c(TRUE, 4) # 1 4
|
||||||
c("dog", TRUE, 4) # "dog" "TRUE" "4"
|
c("dog", TRUE, 4) # "dog" "TRUE" "4"
|
||||||
as.numeric("Bilbo")
|
as.numeric("Bilbo")
|
||||||
@@ -306,8 +305,8 @@ as.numeric("Bilbo")
|
|||||||
# NAs introduced by coercion
|
# NAs introduced by coercion
|
||||||
|
|
||||||
|
|
||||||
# Also note: those were just the basic data types
|
# 追記: ここで紹介したのは、基本的な型だけです
|
||||||
# There are many more data types, such as for dates, time series, etc.
|
# 実際には、日付(dates)や時系列(time series)など、いろいろな型があります
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -315,40 +314,40 @@ as.numeric("Bilbo")
|
|||||||
|
|
||||||
|
|
||||||
##################################################
|
##################################################
|
||||||
# Variables, loops, if/else
|
# 変数、ループ、もし/ほかに(if/else)
|
||||||
##################################################
|
##################################################
|
||||||
|
|
||||||
|
|
||||||
# A variable is like a box you store a value in for later use.
|
# 変数は、ある値を後で使うために入れておく、箱のようなものです
|
||||||
# We call this "assigning" the value to the variable.
|
# 箱に入れることを、変数に値を代入する、といいます
|
||||||
# Having variables lets us write loops, functions, and if/else statements
|
# 変数を使うと、ループや関数、if/else 分岐を利用できます
|
||||||
|
|
||||||
|
|
||||||
# VARIABLES
|
# 変数
|
||||||
# Lots of way to assign stuff:
|
# 代入する方法はいろいろあります
|
||||||
x = 5 # this is possible
|
x = 5 # これはできます
|
||||||
y <- "1" # this is preferred
|
y <- "1" # これがおすすめです
|
||||||
TRUE -> z # this works but is weird
|
TRUE -> z # これも使えますが、変です
|
||||||
|
|
||||||
|
|
||||||
# LOOPS
|
# ループ
|
||||||
# We've got for loops
|
# forでループできます
|
||||||
for (i in 1:4) {
|
for (i in 1:4) {
|
||||||
print(i)
|
print(i)
|
||||||
}
|
}
|
||||||
# We've got while loops
|
# whileでループできます
|
||||||
a <- 10
|
a <- 10
|
||||||
while (a > 4) {
|
while (a > 4) {
|
||||||
cat(a, "...", sep = "")
|
cat(a, "...", sep = "")
|
||||||
a <- a - 1
|
a <- a - 1
|
||||||
}
|
}
|
||||||
# Keep in mind that for and while loops run slowly in R
|
# Rでは、forやwhileは遅いことを覚えておいてください
|
||||||
# Operations on entire vectors (i.e. a whole row, a whole column)
|
# 処理を行う場合は、ベクター丸ごと処理する(つまり、行全体や、列全体)を指定して行うか、
|
||||||
# or apply()-type functions (we'll discuss later) are preferred
|
# 後述する、apply()系の関数を使うのがお勧めです
|
||||||
|
|
||||||
|
|
||||||
# IF/ELSE
|
# IF/ELSE
|
||||||
# Again, pretty standard
|
# ごく普通のif文です
|
||||||
if (4 > 3) {
|
if (4 > 3) {
|
||||||
print("4 is greater than 3")
|
print("4 is greater than 3")
|
||||||
} else {
|
} else {
|
||||||
@@ -358,14 +357,14 @@ if (4 > 3) {
|
|||||||
# [1] "4 is greater than 3"
|
# [1] "4 is greater than 3"
|
||||||
|
|
||||||
|
|
||||||
# FUNCTIONS
|
# 関数
|
||||||
# Defined like so:
|
# 以下のように定義します
|
||||||
jiggle <- function(x) {
|
jiggle <- function(x) {
|
||||||
x = x + rnorm(1, sd=.1) #add in a bit of (controlled) noise
|
x = x + rnorm(1, sd=.1) #すこしだけ(制御された)ノイズを入れます
|
||||||
return(x)
|
return(x)
|
||||||
}
|
}
|
||||||
# Called like any other R function:
|
# 他のR関数と同じように呼びます
|
||||||
jiggle(5) # 5±ε. After set.seed(2716057), jiggle(5)==5.005043
|
jiggle(5) # 5±ε. set.seed(2716057)をすると、jiggle(5)==5.005043
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -373,26 +372,26 @@ jiggle(5) # 5±ε. After set.seed(2716057), jiggle(5)==5.005043
|
|||||||
|
|
||||||
|
|
||||||
###########################################################################
|
###########################################################################
|
||||||
# Data structures: Vectors, matrices, data frames, and arrays
|
# データ構造: ベクター、行列、データフレーム、配列
|
||||||
###########################################################################
|
###########################################################################
|
||||||
|
|
||||||
|
|
||||||
# ONE-DIMENSIONAL
|
# 1次元
|
||||||
|
|
||||||
|
|
||||||
# Let's start from the very beginning, and with something you already know: vectors.
|
# まずは基本からです。すでにご存じのベクターからです
|
||||||
vec <- c(8, 9, 10, 11)
|
vec <- c(8, 9, 10, 11)
|
||||||
vec # 8 9 10 11
|
vec # 8 9 10 11
|
||||||
# We ask for specific elements by subsetting with square brackets
|
# 特定の要素を、[角括弧]による指定で取り出せます
|
||||||
# (Note that R starts counting from 1)
|
# (Rでは、最初の要素は1番目と数えます)
|
||||||
vec[1] # 8
|
vec[1] # 8
|
||||||
letters[18] # "r"
|
letters[18] # "r"
|
||||||
LETTERS[13] # "M"
|
LETTERS[13] # "M"
|
||||||
month.name[9] # "September"
|
month.name[9] # "September"
|
||||||
c(6, 8, 7, 5, 3, 0, 9)[3] # 7
|
c(6, 8, 7, 5, 3, 0, 9)[3] # 7
|
||||||
# We can also search for the indices of specific components,
|
# 特定のルールに当てはまる要素を見つけることもできます
|
||||||
which(vec %% 2 == 0) # 1 3
|
which(vec %% 2 == 0) # 1 3
|
||||||
# grab just the first or last few entries in the vector,
|
# 最初か最後の数個を取り出すこともできます
|
||||||
head(vec, 1) # 8
|
head(vec, 1) # 8
|
||||||
tail(vec, 2) # 10 11
|
tail(vec, 2) # 10 11
|
||||||
# or figure out if a certain value is in the vector
|
# or figure out if a certain value is in the vector
|
||||||
|
Reference in New Issue
Block a user