简体   繁体   English

从动态 dataframe 转换为动态日期列作为因子

[英]Convert a dynamic date column from a dynamic dataframe as a factor

I am creating dynamically 3 dataframes through a loop to calculate the variable Nb_Active_Accounts on 3 levels of granularity: daily/weekly/monthly;我正在通过一个循环动态创建 3 个数据帧来计算 3 个粒度级别的变量Nb_Active_Accounts :每天/每周/每月; and I am trying to convert the dynamic date column as factor in a dynamic way.我正在尝试以动态方式将动态日期列转换为因素。
If I had only a simple weekly level dataframe, the dataframe would be named Active_Accounts_Weekly and would look like below:如果我只有一个简单的每周级别 dataframe,则 dataframe 将被命名为Active_Accounts_Weekly ,如下所示:
在此处输入图像描述

and to convert the Week_Date from a date to a factor I would only have to do this:并将 Week_Date 从日期转换为一个因素,我只需要这样做:

Active_Accounts_Weekly$Week_Date <- as.factor(Active_Accounts_Weekly$Week_Date)

But since I am creating dynamically 3 different dataframes by using functions like get() and assign() in a loop, I am trying to convert respectively the column Day_Date/Week_Date/Month_Date to a factor.但是由于我通过在循环中使用get()assign()等函数动态创建 3 个不同的数据帧,我试图分别将 Day_Date/Week_Date/Month_Date 列转换为一个因子。
The dynamic dataframe representation looks like this: get( paste0("Active_Accounts_", date_granularity$var_name[i] ) ) where var_name is respectively equal to "Daily" / "Weekly" / "Monthly"动态 dataframe 表示如下: get( paste0("Active_Accounts_", date_granularity$var_name[i] ) )其中var_name分别等于"Daily" / "Weekly" / "Monthly"

What is the appropriate way to do this?这样做的合适方法是什么?

We could use mget to get all the data into a list and then loop over the list with lapply and transform and keep the data in a list我们可以使用mget将所有数据放入list ,然后使用lapply listtransform并将数据保存在list

lst1 <- lapply(mget(paste0("Active_Accounts_", date_granularity$var_name)), 
       transform, Week_Date = factor(Week_Date))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM