May be you want to create different dataframe
objects based on the unique combination of date
and time
. I would recommend using split
and then do the rest of the calculation/analysis in the list
using lapply
rather than creating individual data.frames
in the global environment.
lst <- split(df["Price"], list(df$Date, df$Time), drop=TRUE)
You can do most of the operations in the lst
. For example:
sapply(lst, function(x) mean(x$Price, na.rm=TRUE))
#02-Jan-96.03:20 02-Jan-96.03:25 02-Jan-96.03:45 02-Jan-96.04:20
# 366.1500 337.1500 346.4333 353.4833
But, if you need to create individual data.frames
nm1 <- gsub("[[:punct:]]", "", paste("Var", names(lst),sep="_"))
nm1
#[1] "Var02Jan960320" "Var02Jan960325" "Var02Jan960345" "Var02Jan960420"
list2env(setNames(lst, nm1), envir=.GlobalEnv)
Var02Jan960320
# Price
#1 387.15
#4 345.15
data
df <- structure(list(Date = c("02-Jan-96", "02-Jan-96", "02-Jan-96",
"02-Jan-96", "02-Jan-96", "02-Jan-96", "02-Jan-96", "02-Jan-96",
"02-Jan-96"), Time = c("03:20", "03:45", "04:20", "03:20", "03:45",
"04:20", "03:25", "03:45", "04:20"), Price = c(387.15, 387.1,
387.15, 345.15, 325.1, 335.15, 337.15, 327.1, 338.15)), .Names = c("Date",
"Time", "Price"), class = "data.frame", row.names = c("1", "2",
"3", "4", "5", "6", "7", "8", "9"))