character <- "あいうえお"
character |> class()[1] "character"
character <- "あいうえお"
character |> class()[1] "character"
character2 <- "12345"
character2 |> class()[1] "character"
character2 |> as.numeric() # 数値型への変換[1] 12345
numeric <- 12345
numeric |> class()[1] "numeric"
logical <- TRUE
logical |> class()[1] "logical"
date <- lubridate::date("2020/04/01")
date |> class()[1] "Date"
vector <- c(1, 2, 3)
vector |> class()[1] "numeric"
sum(vector)[1] 6
vector * 2[1] 2 4 6
matrix <- matrix(c(1, 2, 3, 4, 5, 6), ncol = 2, byrow = TRUE)
matrix |> class()[1] "matrix" "array"
matrix [,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
list <- list(c(1, 2, 3), 4, "5", "A", TRUE)
list |> class()[1] "list"
list[[1]]
[1] 1 2 3
[[2]]
[1] 4
[[3]]
[1] "5"
[[4]]
[1] "A"
[[5]]
[1] TRUE
table <- runif(100) |> # 一様分布 (0-1の間の値から一様の確率で乱数を100個作成)
round() |> # 四捨五入し整数に
table() # table にする
table |> class()[1] "table"
table
0 1
50 50
data_frame <- data.frame(
ID = c(1, 2),
pref = c("東京都", "神奈川県"),
city = c("港区", "横浜市")
)
data_frame |> class()[1] "data.frame"
data_framecalc_rent <- \(annual = NULL, monthly = NULL){
if(!is.null(monthly)){
annual <- monthly * 12
}
rent_monthly_max <- 4.796e-01 + 9.784e-11 * (annual ^ 3) - 1.400e-06 * (annual ^ 2) +
2.036e-02 * annual
rent_monthly_ideal <- 3.997e-01 + 8.153e-11 * (annual ^ 3) - 1.167e-06 * (annual ^ 2) +
1.697e-02 * annual
print(
paste0("負担_理想 (.25): ", round(rent_monthly_ideal, digits = 1), "万円")
)
print(
paste0(
"負担_最大 (.3): ", round(rent_monthly_max, digits = 1), "万円")
)
}calc_rent(monthly = 30)[1] "負担_理想 (.25): 6.4万円"
[1] "負担_最大 (.3): 7.6万円"
calc_rent(annual = 500)[1] "負担_理想 (.25): 8.6万円"
[1] "負担_最大 (.3): 10.3万円"
variable <- 100 ## グローバル変数
print(variable)[1] 100
(\(){
variable <- 200 ## ローカル変数
print(variable)
})()[1] 200
print(variable) ## グローバル変数 (ローカル変数は反映されない!)[1] 100
variable <- 100 ## グローバル変数
print(variable)[1] 100
(\(){
variable <- 200 ## ローカル変数
print(variable)
assign("variable", variable, envir = globalenv()) ## ローカル変数の中身をグローバル変数に割り当てる
})()[1] 200
print(variable) ## グローバル変数[1] 200
for(i in 1:10){
print(paste0(i, "秒 経過"))
Sys.sleep(1)
}for(time_for in 10:0){
print(paste0("残り ", time_for, "秒"))
Sys.sleep(1)
}time_while <- 10
while(time_while >= 0){
print(paste0("残り ", time_while, "秒"))
time_while <- time_while - 1
Sys.sleep(1)
}wait_time <- \(time){
if(time > 0){
print(paste0("残り ", time, "秒"))
Sys.sleep(1)
wait_time(time - 1)
}
else{
print("お待たせしました!")
}
}wait_time(10)day <- lubridate::today() |>
lubridate::day()
if(day %% 2 == 0) {
print("偶数の日")
} else {
print("奇数の日")
}[1] "奇数の日"
day <- lubridate::today() |>
lubridate::day()
if(day %% 7 == 0) {
print("0")
} else if (day %% 7 == 1) {
print("1")
} else if (day %% 7 == 2) {
print("2")
} else if (day %% 7 == 3) {
print("3")
} else if (day %% 7 == 4) {
print("4")
} else if (day %% 7 == 5) {
print("5")
} else {
print("6")
}[1] "3"
day <- lubridate::today() |>
lubridate::day()
if(day %% 2 == 0) {
print("偶数")
if (day %% 3 == 0) {
print("6の倍数")
if (day %% 4 == 0) {
print("12の倍数")
}
}
}day <- lubridate::today() |>
lubridate::day()
## 今日の日が偶数なら今日の日付を、奇数なら翌日の日付を出力
ifelse(day %% 2 == 0, as.Date(lubridate::today()), lubridate::today() + 1)[1] 19556
day <- lubridate::today() |>
lubridate::day()
## 今日の日が偶数なら今日の日付を、奇数なら翌日の日付を出力
dplyr::if_else(day %% 2 == 0, as.Date(lubridate::today()), lubridate::today() + 1)[1] "2023-07-18"
day <- lubridate::today() |>
lubridate::day()
day <- day %% 2 |> as.character()
switch (
day,
"0" = "偶数の日",
"1" = "奇数の日"
)[1] "奇数の日"
day <- lubridate::today() |>
lubridate::day()
day <- day %% 2
dplyr::case_match(
day,
0 ~ "偶数の日",
1 ~ "奇数の日"
)[1] "奇数の日"
| 構文 | 意味 |
|---|---|
| a + b | aにbを足した結果の値 |
| a - b | aからbを引いた結果の値 |
| a * b | aにbを掛けた結果の値 |
| a / b | aをbで割った結果の値 |
| a %/% b | aをbで割った際の商(整数) |
| a %% b | aをbで割った際の余り(整数) |
| a ^ b | aのb乗の値 |
| 構文 | 意味 |
|---|---|
| a > b | aはbよりも大きいか? |
| a >= b | aはb以上か? |
| a < b | aはbよりも小さいか? |
| a <= b | aはb以下か? |
| a == b | aはbと等しいか? (文字列でも利用可) |
| a != b | aはbと等しくないか? (文字列でも利用可) |
| 構文 | 意味 |
|---|---|
| a & b | aかつb |
| a | b | aまたはb |
| !a | aではない |
| 構文 | 意味 |
|---|---|
| a %in% c(a, b, c) | aはc(a, b, c)のグループに含まれるか? |
| a %*% b | aとbの行列の積 |
| a %o% b | aとbの外積 |
パイプライン (パイプ演算子とも。R では |> ) の左辺が、右辺の第1引数になる
result <- lm(income ~ age, data = data) # resultが中間のオブジェクト
summary(result)↑ is equal to ↓
lm(income ~ age, data = data) |> # resultが不要に!
summary() # 第1引数はパイプラインの前の関数“|>” は Ctrl (or Command) +Shift+M で入力できるように設定できる
“Tools” -> “Global Options” -> “Code” -> “Use native pipe operator, |>” にチェックして”OK”
バージョン 4.1 以前 の R では,パイプラインを使いたい場合,tidyverseパッケージ群の一つである magrittr パッケージを用いるのが主流だった
バージョン 4.1 の R から、パイプ演算子 |> が標準で導入された
magrittr でのパイプ演算子: %>% (%で括る)
data %>%
select(row1, row2) %>%
lm() %>% summary() data %>%
select(row1) %>%
mutate(rowNew = as.numeric(.)) # .の部分に左辺が挿入される%$% 演算子 : 左辺内の変数にアクセス (= “<左辺>$”が実行時に補われる)
data %$% lm(row1, row2) %>% summary()data <- data %>% select(row1, row2)
data %<>% select(row1, row2) # 上行と同じ意味data %>%
select(row1, row2) %T>%
plot() %>% # plotが無事に出力される
lm() %>% summary() # 無事にsummaryも出力される