简体   繁体   English

在 R 的嵌套表中,从具有天、月、年的单独列创建日期

[英]create date from separate columns with day, month year within a nested table in R

I'm working on hydrological data in nested tables.我正在处理嵌套表中的水文数据。 Besides streamflow (Q) I have dates split into 3 columns (one for days, second for months and third for year).除了流(Q)之外,我将日期分为 3 列(一列表示天,第二列表示月,第三列表示年)。

year_2008 <- list("910" = data.frame(Year = c(2008), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)),
             "950" = data.frame(Year = c(2008), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)),
             "1012" = data.frame(Year = c(2008), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)),
              "1087" = data.frame(Year = c(2008), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)))


year_2009 <- list("910" = data.frame(Year = c(2009), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)),
                  "950" = data.frame(Year = c(2009), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)),
                  "1012" = data.frame(Year = c(2009), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)),
                  "1087" = data.frame(Year = c(2009), Day=c(1:5), Month= c(1, 1, 1, 1 ,1)))

It's a nested table.这是一个嵌套表。

year_list <- list (year_2008, year_2009)
names(year_list) <- c("2008", "2009")

I'm trying to gather the information from those 3 columns into a single one which will show a date (in a new column called "date").我正在尝试将这 3 列中的信息收集到一个单独的列中,该列将显示一个日期(在一个名为“日期”的新列中)。 I've created a function to create a date column:我创建了一个 function 来创建一个日期列:

transform_date2 <- function(x) {x$date <- as.Date(with(x, paste(x$Day, x$Month, x$Year, sep="-")), "%d-%m-%Y")}

I tried applying that function to the nested table:我尝试将 function 应用于嵌套表:

result1 <- lapply(year_list, `[[`, transform_date2)

this results in an error: Error in FUN(X[[i]], ...): invalid subscript type 'closure'这会导致错误: FUN(X[[i]], ...) 中的错误:无效的下标类型“闭包”

I tried doing a loop:我试着做一个循环:

result2 <- for (i in 1:seq_along(year_list)) {transform_date2}

This results in: Warning message: In 1:seq_along(year_list): numerical expression has 2 elements: only the first used这将导致:警告消息:在 1:seq_along(year_list):数值表达式有 2 个元素:仅使用第一个元素

I feel that I'm having problems accessing the correct "level" of the nested table.我觉得我在访问嵌套表的正确“级别”时遇到问题。

My end goal is to keep the current format (nested table), remove the columns Day, Month, Year in each nested table, create the date column in each and keep the Q column.我的最终目标是保持当前格式(嵌套表),删除每个嵌套表中的 Day、Month、Year 列,在每个嵌套表中创建日期列并保留 Q 列。 Please help请帮忙

Since it's a nested list use nested lapply :因为它是一个嵌套列表,所以使用嵌套lapply

year_list <- lapply(year_list, function(x) lapply(x, transform_date2))

where transform_date2 is:其中transform_date2是:

transform_date2 <- function(x) {x$date <- as.Date(with(x, paste(x$Day, x$Month, x$Year, sep="-")), "%d-%m-%Y");x}

暂无
暂无

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

相关问题 将 3 列(日、月、年)转换为单个日期列 R - Converting 3 columns (day, month, year) into a single date column R 将R中的日期的月份和年份的两个单独列更改为一 - Change two separate columns of month and year into one for date in R 如何在 R 中使用日、月和年创建日期? - How to create a date using day, month and year in R? 从单独的列 R 获取最新的月份和年份 - get latest month and year from separate columns R 将日期拆分为年、月和日的不同列 - Split date into different columns for year, month and day 如何使用R保留日/月/日日期格式的月/年 - How to retain month/year from the day/month/year date format using R 如果我的月、日和年在不同的列中,我可以使用 R 包 lubridate 来解析日期吗? - Can I use the R package lubridate to parse dates if my month, day, and year are in separate columns? 创建一个新的日期变量,它与 r 中的原始日期变量位于同一周的同一天、同一个月和同一年 - Creating a new date variable that is on the same day of the week, within the same month, and year as original date variable in r 如何在 r 中将日期和时间列拆分为单独的日、月、小时、分钟、秒、星期几列? - How to split a date and time column into separate day, month, hour, minute, second, day of week columns in r? 从 R 中的 char 列中提取月份日期年份到新列中 - extract month date year into new columns from char column in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM