R 如何添加季节性虚拟变量?
我想在我的R 如何添加季节性虚拟变量?,r,data.table,dummy-variable,R,Data.table,Dummy Variable,我想在我的R数据中添加季节性假人。基于季度的表。我已经看过多个例子,但我还不能解决这个问题。我对R的了解有限,所以我想知道你是否能让我走上正轨 我的数据。表如下所示: Year_week artist_id number_of_events number_of_streams 1: 16/30 8296 1 957892 2: 16/33 8296 6 882282
R数据中添加季节性假人。基于季度的表
。我已经看过多个例子,但我还不能解决这个问题。我对R
的了解有限,所以我想知道你是否能让我走上正轨
我的数据。表如下所示:
Year_week artist_id number_of_events number_of_streams
1: 16/30 8296 1 957892
2: 16/33 8296 6 882282
3: 16/34 8296 5 926037
4: 16/35 8296 2 952704
5: 15/37 17879 1 89515
6: 16/22 22690 2 119653
Year_week artist_id number_of_events number_of_streams Q2 Q3 Q4
1: 16/50 8296 1 957892 0 0 1
我想要的是这样的格式:
Year_week artist_id number_of_events number_of_streams
1: 16/30 8296 1 957892
2: 16/33 8296 6 882282
3: 16/34 8296 5 926037
4: 16/35 8296 2 952704
5: 15/37 17879 1 89515
6: 16/22 22690 2 119653
Year_week artist_id number_of_events number_of_streams Q2 Q3 Q4
1: 16/50 8296 1 957892 0 0 1
将quarter
列添加到您的df
df$quarter <- as.factor(df$quarter)
df <- cbind(df, model.matrix(~quarter, df))
df$quarty我假设Year\u week
是我们可以提取条目日期的地方
library(data.table)
whichQuart <- function(x){
data.frame(+(x <= 13),
+(x >13 & x <= 26),
+(x > 26 & x <= 39),
+(x > 39 & x <= 52))
}
dt <- setDT(read.table(text="Year_week artist_id number_of_events number_of_streams
1: 16/30 8296 1 957892
2: 16/33 8296 6 882282
3: 16/34 8296 5 926037
4: 16/35 8296 2 952704
5: 15/37 17879 1 89515
6: 16/22 22690 2 119653", header=TRUE, stringsAsFactors=FALSE))
dt[, week := strsplit(Year_week, "/")[2]]
dt[, c("Q1", "Q2", "Q3", "Q4") := whichQuart(week)]
# Year_week artist_id number_of_events number_of_streams week Q1 Q2 Q3 Q4
#1: 16/30 8296 1 957892 16 0 1 0 0
#2: 16/33 8296 6 882282 33 0 0 1 0
#3: 16/34 8296 5 926037 16 0 1 0 0
#4: 16/35 8296 2 952704 33 0 0 1 0
#5: 15/37 17879 1 89515 16 0 1 0 0
#6: 16/22 22690 2 119653 33 0 0 1 0
库(data.table)
其中quart有两种方法:
1)使用dcast
、cut
和sub
:
dcast(DT[, Q := cut(as.integer(sub('.*/','',Year_week)),
breaks = c(0,13,26,39,53),
labels = paste0('Q',1:4))],
Year_week + artist_id + number_of_events + number_of_streams ~ Q,
value.var = 'Q',
drop = c(TRUE,FALSE),
fun = length)
给出:
Year_week artist_id number_of_events number_of_streams Q1 Q2 Q3 Q4
1: 15/37 17879 1 89515 0 0 1 0
2: 16/22 22690 2 119653 0 1 0 0
3: 16/30 8296 1 957892 0 0 1 0
4: 16/33 8296 6 882282 0 0 1 0
5: 16/34 8296 5 926037 0 0 1 0
6: 16/35 8296 2 952704 0 0 1 0
它的作用是:
as.integer(sub('./','',Year\u-week))
从Year\u-week
列中提取周数
- 使用
cut
将其用适当的标签分成四分之一(另请参见?cut
)
- 使用
dcast
可以使用聚合函数(length
)将四分之一列转换为宽格式。通过在dcast
函数中使用drop=c(真、假)
,您可以确保所有季度都包括在内
注:
Q
-列是一个有序因子,因此您也可以使用它来排列和过滤数据
- 根据虚拟列的使用情况:并不总是需要这些列。当您想将它们用作分组或筛选变量时,只需使用
Q
变量即可
- 然而,一些统计测试需要虚拟变量(这证明了
dcast
步骤的合理性)
2)使用cut
、sub
和lappy
:
DT[, Q := cut(as.integer(sub('.*/','',Year_week)),
breaks = c(0,13,26,39,53),
labels = paste0('Q',1:4))
][, paste0('Q',1:4) := lapply(paste0('Q',1:4), function(q) as.integer(q == Q))][]
这给出了类似的结果。您只需检查Q
-列中是否有一个季度标签,而不是使用dcast
进行转置
使用数据:
DT <- fread(' Year_week artist_id number_of_events number_of_streams
16/30 8296 1 957892
16/33 8296 6 882282
16/34 8296 5 926037
16/35 8296 2 952704
15/37 17879 1 89515
16/22 22690 2 119653')
DT您能上传您的代码季度是从哪里来的吗?这些是一些if语句,比如如果周数小于13,季度将是1。13-26,将是第二季度,依此类推到第四季度。我不想在这里实现它,我把它留给OP自己去做。这甚至不是该计划的主要目标。主要目标是创建一个虚拟变量,并将其绑定到数据帧,我解决了这个问题@Sotosand使用model.matrix是创建虚拟变量的更通用的方法。您需要添加完整的答案,以完全解决该问题。请记住,更多的人将访问此页面以获取有关此问题的帮助。完整的答案是必须的。首先,不要把这当成个人问题。这不是困扰我、你或其他任何人的原因。这就是为什么。为了使这个网站尽可能有价值,有一些指导方针。在这一点上,4票(!)的否决票是由于您拒绝通过创建quarter
变量来更新您的答案。另外,我不需要标记您的答案。或者,如果您想添加外部库,qdapTools::mtabulate(cut(as.integer(sub('./','',df$Year_-week))、c(0,13,26,39,52)、paste0('Q',1:4))
感谢您的解决方案!当我运行代码时,我得到以下消息:使用'Q'作为值列。使用“value.var”覆盖。这会导致任何问题吗?此外,代码还添加了一个名为NA的额外collumn。您知道为什么会发生这种情况吗?@JLMR17它给出的结果与使用value.var='Q'
明确指定它的结果相同(后者更好,以避免混淆)。我还更新了答案,以确保所有季度都包括在内。HTH@JLMR17我对NA值的第一个猜测可能是,有几周以53为数字。我已经更新了代码以包括这些案例。