【R】時系列データのサンプル作成

こんてんつ

時系列データのサンプルを簡易的に用意する方法を記述する。今回は、timevalueの値を持つサンプルを作成する。

  • 乱数生成を使った作戦
  • data.frameでベタ打ち作戦
  • csvファイルから読み込む作戦
  • (補足)mm:ss形式の行を追加する方法

乱数生成を使った作成

コード

dat <- 
  data.frame(
    time = runif(30, min = 0, max = 300),
    value = rnorm(30)
    ) %>%
  arrange(time) %>%
  mutate(time = round(time, digits = 2),
         value = round(value, digits = 2)+time*0.05)

出力

time value
24.73   1.2765
35.25   2.8025
40.4    1.85
41.61   1.3005
76.63   4.2915
85.84   1.632
117.06  5.493
134.09  5.9745
137.32  6.436
138.69  6.2945
142.5   6.515
154.26  7.923
155.73  7.4765
168.1   8.905
192.52  10.946
197.1   11.065
211.52  12.476
215.73  10.5265
220.98  9.269
249.13  10.0165
250.8   11.17
271.21  11.8405
271.72  14.346
274.44  14.362
280.4   12.26
281.12  13.776
282 14.56
284 11.79
293.47  15.3735
296.67  13.9835

解説

runif(30, min = 0, max = 300)は0から300の範囲で30個のランダムな値を生成する。rnorm(30)正規分布から30個のランダムな値を生成する。data.frameは一般的には、次の形で利用する。

# データフレームの作成
my_data <- data.frame(
  Name = c("Alice", "Bob", "Charlie"),
  Age = c(25, 30, 22),
  Score = c(95, 89, 75)
)

# 作成したデータフレームの表示
print(my_data)

#出力
    Name Age Score
1  Alice  25    95
2    Bob  30    89
3 Charlie  22    75

その他、arrange(time)timeの昇順にソートを行っている。また、mutateround()で四捨五入をして丸めている。

contents-open.hatenablog.com

data.frameでベタ打ち作戦

コード

dat <- data.frame(
  time = c(24.73, 35.25, 40.4, 41.61, 76.63, 85.84, 117.06, 134.09, 137.32, 138.69, 
           142.5, 154.26, 155.73, 168.1, 192.52, 197.1, 211.52, 215.73, 220.98, 249.13, 
           250.8, 271.21, 271.72, 274.44, 280.4, 281.12, 282, 284, 293.47, 296.67),
  value = c(1.2765, 2.8025, 1.85, 1.3005, 4.2915, 1.632, 5.493, 5.9745, 6.436, 6.2945,
            6.515, 7.923, 7.4765, 8.905, 10.946, 11.065, 12.476, 10.5265, 9.269, 10.0165,
            11.17, 11.8405, 14.346, 14.362, 12.26, 13.776, 14.56, 11.79, 15.3735, 13.9835)
)

csvファイルから読み込む作戦

コード

dat <- read.csv("mydata.csv")

(補足)mm:ss形式の行を追加する方法

dat %>% 
  mutate(time_mmss = sprintf("%02d:%02d", time %/% 60, round(time %% 60))) %>%
  select(time, time_mmss, value)

【R】秒単位で整理された時系列データをmm:ss形式でグラフプロットする

こんてんつ

時系列データをプロットしたいとき、データは秒で整列されているが、グラフの横軸はmm:ssの表示の方が見やすい場合がある。その時に使える方法を紹介する。

チートシート

コード

dat %>%
  ggplot(aes(x = time, y = value)) +
  geom_line() +
  scale_x_continuous(labels = function(x) sprintf("%02d:%02d", as.integer(floor(x / 60)), as.integer(x %% 60))) +
  labs(x = "Time (mm:ss)",y = "Value")

出力

サンプルデータ

time value
24.73   0.5346
35.25   1.745
40.4    0.978
41.61   1.6122
76.63   1.9926
85.84   4.3768
117.06  2.7012
134.09  3.4118
137.32  3.1764
138.69  3.4138
142.5   3.46
154.26  3.2952
155.73  3.4246
168.1   3.862
192.52  5.1704
197.1   5.152
211.52  6.1304
215.73  4.5746
220.98  6.1996
249.13  7.4226
250.8   6.386
271.21  7.1442
271.72  6.1944
274.44  6.1288
280.4   7.368
281.12  5.9024
282 6.1
284 8.09
293.47  6.5694
296.67  6.7834

【R】データフレームの特定の列を四捨五入する方法(mutate、round)

こんてんつ

データフレームの列を指定して四捨五入したい場面がある。その時につかえるコマンドを紹介する。

チートシート

コード

dat %>% 
  mutate(time = round(time, digits = 2))

解説

roundで、指定した列(time)を小数第二位まで(digits = 2)で丸めている。mutateと組みあわせることで置換することができる。

サンプルデータ

#四捨五入前
time    value
1.092821988 0.908130569
4.295000131 -0.668832741
5.590229016 0.049779323
6.653013057 1.188054982
16.35518947 -0.322950312
38.88655601 0.219548011
47.25476343 -0.609888171
50.61970835 0.201942437
53.95446466 0.370294421
57.29383249 -1.113040194
66.27735144 -0.090865006
103.8869736 -0.194863607
108.6246822 -0.012996542
117.4398822 -1.246182062
148.4241006 0.42119744
148.4321235 -1.032527137
149.7821984 1.016773978
161.2059699 -0.764005911
186.8323629 -0.509764512
190.914034  0.279108394
#四捨五入後
time    value
1.09    0.908130569
4.3 -0.668832741
5.59    0.049779323
6.65    1.188054982
16.36   -0.322950312
38.89   0.219548011
47.25   -0.609888171
50.62   0.201942437
53.95   0.370294421
57.29   -1.113040194
66.28   -0.090865006
103.89  -0.194863607
108.62  -0.012996542
117.44  -1.246182062
148.42  0.42119744
148.43  -1.032527137
149.78  1.016773978
161.21  -0.764005911
186.83  -0.509764512
190.91  0.279108394

【R】分:秒(mm:ss)の様な入力を、秒に変換する関数

こんてんつ

10:10の様なmm:ssの入力を、610秒の様な秒に変換する関数を紹介する。

チートシート

関数

to_seconds <- function(input_time) {
  
  time_parts <- strsplit(input_time, ":")[[1]]
  
  minutes <- as.integer(time_parts[1])
  seconds <- as.integer(time_parts[2])
  
  total_seconds <- minutes * 60 + seconds
  
  return(total_seconds)
}

使用例

print(to_seconds("10:10"))

解説

strsplit(time, ":")[[1]][[1]]の意味について解説する。strsplit(time, ":")[[1]]"10:10"の様な入力を、c("10", "10")に変換する。strsplit(time, ":")はリストを返し、[[1]]を付けることで、リストの最初の要素の値を返すことになる。具体的には、

> strsplit("10:10", ":")
[[1]]
[1] "10" "10"

> strsplit("10:10", ":")[[1]]
[1] "10" "10" 

【R】データフレームの列名を列挙する方法(colnames())

こんてんつ

データフレームの列名だけを取り出したいときに使えるコマンドを紹介する。

チートシート

列名の取り出し

iris %>% colnames()

条件を指定した列名の取り出し

#特定の文字列から始まる列名の取り出し
iris %>% 
  select(starts_with("Sepal")) %>%
  colnames()

#特定の文字列で終わる列名の取り出し
iris %>% 
  select(ends_with("Length")) %>%
  colnames()

#特定の文字列を含む列名の取り出し
iris %>% 
  select(contains("al")) %>%
  colnames()

【英文法】really, too...to, so...thatのニュアンスの違い

こんてんつ

次の3つの強調される部分がどこなのかを、例文をもとに説明する。

  • really
  • too...to
  • so...that

reallyについては、一般的に一緒に取り上げられないが、比較の仕方の一例として取り上げた。

例題

3つの比較

  • She was really drunk, and I didn’t understand what she was saying.
  • She was too drunk for me to understand what she was saying.
  • She was so drunk that I didn’t understand what she was saying.

really

  • She was really drunk, and I didn’t understand what she was saying.

「really」は「drunk」を強調しており、彼女が非常に酔っぱらっていたことを示している。

その他にも、次の様な例文が考えられる。

  • He was really tired and couldn’t finish his work.
  • She was really excited and couldn’t stop talking about her trip.
  • The pizza was really good, and I ate three slices.

too...to

  • She was too drunk for me to understand what she was saying.

「too」が「drunk」を強調しており、彼女が非常に酔っぱらっていたために、私が彼女の言っていることを理解できなかったことをしめしている。一方で次の文章の場合は、

  • She was too drunk to understand what she was saying.

「彼女」自身が自分の言っていることを理解できない状態になっているという表現になる。

その他にも、次の様な例文が考えられる。

  • The music was too loud for me to hear my phone ring.
  • The soup was too hot for me to drink.
  • The room was too dark for me to find my keys.

so...that

  • She was so drunk that I didn’t understand what she was saying.

「so」に続く「drunk」が非常に強いために、彼女の言っていることを理解できなかったことを強調している。

その他にも、次の様な例文が考えられる。

  • The cake was so delicious that I ate the whole thing.
  • The book was so interesting that I couldn’t put it down.
  • The concert was so loud that I couldn’t hear my friend talking.

まとめ

  • 「really」は後ろに続く形容詞や副詞を強調する
  • 「too…to」は「too」に続く状況を強調し、それが別の結果(to以下)を引き起こすことを示す
  • 「so…that」は「so」に続く状況が非常に強いために、別の結果(that以下)が生じることを強調する

【TOEIC】SVOCの「to不定詞」と「使役・知覚」の例文集

こんてんつ

SVOCの例文を幾つか並べました。

解説

  • SはOがCするのをVする」のCはto不定で表す
  • しかし、SVOCのVが使役動詞 make / let / have知覚動詞などの時のCはtoなし不定になる
  • また、SVOCのVがhelpのときのCはto不定toなし不定のどちらも可能
  • かならず、Cがto不定詞や原型不定詞になるのではなく目的格補語の取り扱いに注意。1つの文を2文に分けた時に、be動詞が伴うことに気を付ける。

例文

to不定

  • I want you to be more careful.
  • I would you to stay here.
  • My mother told me to eat more vegetables.
  • My mother allowed me to study abroad.
  • I believe him to be a genius.

make / let / have

  • This medicine will make you feel much better.
  • We'll let you watch TV tonight.
  • He had the doctor look at his leg.

ちなみに、make以外は受動態で用いることが出来ない。

知覚動詞

  • I saw the boy fall down.
  • I watched my boyfriend go out of the house.
  • He noticed a big difference.
  • I heard someone shout in the distance.
  • I felt my house shake.

両方取るhelp

  • Tom helped me (to) do my homework.

OがCする以外の目的格補語の取り扱い

  • I heard someone call my name.「OがCする」
  • I heard someone calling my name.「OがCしている」
  • I heard my name called.「OがCされる」
  • I heard my name being called.「OがCされている」