首页 文章

将函数写入大型列表中的Lapply

提问于
浏览
0

基本上我有几天的大型时间序列数据帧 . 我编写了一些代码,这些代码在数据框中一次可以运行一天,但现在我想调整它以便它可以运行一整天 . 对于我的数据框中的每一天,都有一个列包含当天的日出时间和一个包含日落的列 . 我想利用这些时间将每一天分成白天和夜晚 . 我的日出日落列看起来如此,每天都不同:

Sunrise              Sunset
2010-01-19 08:55:12 2010-01-19 17:26:34

我使用split来按日期划分数据框,以提供包含10个元素(天)的大型列表

# Splits data frame by date 
sepdays<- split(df, df$Date)

# Function to split each day into day and night hours
daynight <- function(){
rise <- as.character(df$Sunrise[1])
rise <- substr(rise, 12,19)
set <- as.character(df$Sunset[1])
set <- substr(set, 12,19)
day <- df[df$Time>rise & df$Time<set, ]
df.night1<-df[df$Time<rise,]
df.night2<-df[df$Time>set,]
night <- merge.data.frame(df.night1,df.night2, sort = TRUE, all.x = TRUE, all.y=TRUE)
return(table(day$Activity))
}

# Apply function over list of days
lapply(sepdays,daynight)

当我运行lapply时,我得到未使用的参数错误:

FUN中的错误(X [[1L]],...):未使用的参数(X [[1]])

我也不确定这是否是获得每个匹配日的特定日出/设定时间的最佳方式 . 我意识到我的功能没有任何争议,但我是一个R新手,所以不太确定我在做什么 .

这是我的数据的样子 .

Date       Time      Activity  Sunrise              Sunset
2010-01-19 23:58:00  1         2010-01-19 08:55:12  2010-01-19 17:26:34
2010-01-19 23:59:00  1         2010-01-19 08:55:12  2010-01-19 17:26:34
2010-01-19 00:00:00  0         2010-01-20 08:54:13  2010-01-20 17:28:11
2010-01-19 00:01:00  0         2010-01-20 08:54:13  2010-01-20 17:28:11
2010-01-20 00:02:00  1         2010-01-20 08:54:13  2010-01-20 17:28:11
2010-01-20 00:03:00  0         2010-01-20 08:54:13  2010-01-20 17:28:11
2010-01-20 00:04:00  1         2010-01-20 08:54:13  2010-01-20 17:28:11

我希望我的输出能够为每个日期包含一个活动表格,例如:

2010-01-19
1  0
2  0

2010-01-20
1  0
2  3

1 回答

  • 1

    我不太确定,因为你的问题很模糊,但我认为你可以这样做:

    DF <- read.table(text="Date,       Time,      Activity,  Sunrise,              Sunset
    2010-01-19, 23:58:00,  1,         2010-01-19 08:55:12,  2010-01-19 17:26:34
    2010-01-19, 23:59:00,  1,         2010-01-19 08:55:12,  2010-01-19 17:26:34
    2010-01-19, 00:00:00,  0,         2010-01-19 08:55:12,  2010-01-19 17:26:34
    2010-01-19, 00:01:00,  0,         2010-01-19 08:55:12,  2010-01-19 17:26:34
    2010-01-19, 09:01:00,  0,         2010-01-19 08:55:12,  2010-01-19 17:26:34
    2010-01-20, 00:02:00,  1,         2010-01-20 08:54:13,  2010-01-20 17:28:11
    2010-01-20, 00:03:00,  0,         2010-01-20 08:54:13,  2010-01-20 17:28:11
    2010-01-20, 00:04:00,  1,         2010-01-20 08:54:13,  2010-01-20 17:28:11", header=TRUE, sep=",")
    
    DF$datetime <- as.POSIXct(paste(DF$Date, DF$Time), "%Y-%m-%d %H:%M:%S", tz="GMT")
    DF$date <- as.Date(DF$datetime)
    DF$Sunrise <- as.POSIXct(DF$Sunrise, "%Y-%m-%d %H:%M:%S", tz="GMT")
    DF$Sunset <- as.POSIXct(DF$Sunset, "%Y-%m-%d %H:%M:%S", tz="GMT")
    
    
    DF$day <- (DF$datetime > DF$Sunrise) & (DF$datetime < DF$Sunset)
    
    #        Date      Time Activity             Sunrise              Sunset            datetime   day       date
    #1 2010-01-19  23:58:00        1 2010-01-19 08:55:12 2010-01-19 17:26:34 2010-01-19 23:58:00 FALSE 2010-01-19
    #2 2010-01-19  23:59:00        1 2010-01-19 08:55:12 2010-01-19 17:26:34 2010-01-19 23:59:00 FALSE 2010-01-19
    #3 2010-01-19  00:00:00        0 2010-01-19 08:55:12 2010-01-19 17:26:34 2010-01-19 00:00:00 FALSE 2010-01-19
    #4 2010-01-19  00:01:00        0 2010-01-19 08:55:12 2010-01-19 17:26:34 2010-01-19 00:01:00 FALSE 2010-01-19
    #5 2010-01-19  09:01:00        0 2010-01-19 08:55:12 2010-01-19 17:26:34 2010-01-19 09:01:00  TRUE 2010-01-19
    #6 2010-01-20  00:02:00        1 2010-01-20 08:54:13 2010-01-20 17:28:11 2010-01-20 00:02:00 FALSE 2010-01-20
    #7 2010-01-20  00:03:00        0 2010-01-20 08:54:13 2010-01-20 17:28:11 2010-01-20 00:03:00 FALSE 2010-01-20
    #8 2010-01-20  00:04:00        1 2010-01-20 08:54:13 2010-01-20 17:28:11 2010-01-20 00:04:00 FALSE 2010-01-20
    
    table(DF[,c("date", "Activity", "day")])
    
    #, , day = FALSE
    #
    #            Activity
    #date         0 1
    #  2010-01-19 2 2
    #  2010-01-20 1 2
    #
    #, , day = TRUE
    #
    #            Activity
    #date         0 1
    #  2010-01-19 1 0
    #  2010-01-20 0 0
    

    这更容易阅读,效率更高 .

相关问题