簡體   English   中英

循環以創建偽變量R

[英]looping in order to create dummy variables R

我想制作一個帶有虛擬變量的數據幀,當事件的時間在時間​​范圍(小時)之間時,該變量顯示1。 那就是我所做的:

library(chron)
library(stringr)

var <- c("A", "B", "C", "D", "E", "F")
times <- c("22:40:20", "10:36:29", "09:23:27", "12:33:27", "12:22:22", "00:17:44")

df <- data.frame(var, times)
hours <- print(paste(stringr::str_pad(c(0:23), 2, side="left", pad=0),
                     "00", "00", sep=":"))

hours <- chron(times=hours)
df$times <- chron(times=df$times)

df$zero <- 0
df$zero[df$times>=hours[1] & df$times<hours[2]] <- 1
df$one <- 0
df$one[(df$times>=hours[2] & df$times<hours[3])] <- 1

依此類推,直到:

df$twentytree <- 0
df$twentytree[(df$times>=hours[24] ] <- 1

  var    times zero one
1   A 22:40:20    0   0
2   B 10:36:29    0   0
3   C 09:23:27    1   0 # in column zero is one because the time event is between 00:00:00 and 01:00:00
4   D 12:33:27    0   0
5   E 12:22:22    0   0
6   F 00:17:44    1   0

但是我很確定那一定是其他一些自動化的想法嗎?

Thx高級。

pd:我已經嘗試過這種方法,但我無法實現。

df <- cbind(df, sapply(hours, function(x) as.integer(c4$times >=x & c4$times<x) ) )

我相信這是使用lubridate和model.matrix的解決方案。 您將要重命名列。

> library(lubridate)
> 
> var <- c("A", "B", "C", "D", "E", "F")
> times <- c("22:40:20", "10:36:29", "09:23:27", "12:33:27", "12:22:22", "00:17:44")
> 
> df <- data.frame(var, times)
> 
> df$times <- hms(df$times)
> df$hour <- hour(df$times)
> indicators <- model.matrix(~ -1 + factor(df$hour))
> df <- cbind(df, indicators)
> df
  var       times hour factor(df$hour)0 factor(df$hour)9 factor(df$hour)10 factor(df$hour)12 factor(df$hour)22
1   A 22H 40M 20S   22                0                0                 0                 0                 1
2   B 10H 36M 29S   10                0                0                 1                 0                 0
3   C  9H 23M 27S    9                0                1                 0                 0                 0
4   D 12H 33M 27S   12                0                0                 0                 1                 0
5   E 12H 22M 22S   12                0                0                 0                 1                 0
6   F     17M 44S    0                1                0                 0                 0                 0

最后,我什至不需要轉換為日期。 我是這樣的:

library(stringr)

var <- c("A", "B", "C", "D", "E", "F")
times <- c("22:40:20", "10:36:29", "09:23:27", "12:33:27", "12:22:22", "00:17:44")

df <- data.frame(var, times)

hours <- stringr::str_pad(c(0:23), 2, side="left", pad=0)
df$hour <- substr(df$times,1,2)

df <- cbind(df, sapply(hours, function(x) as.integer(df$hour==x) ) )

結果:

 var    times hour 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21
1   A 22:40:20   22  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
2   B 10:36:29   10  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0
3   C 09:23:27   09  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0
4   D 12:33:27   12  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0
5   E 12:22:22   12  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0
6   F 00:17:44   00  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM