我正在構建一個旨在多次查詢數據庫的R腳本(每個從3個向量的元素中排列一個,但我很難搞清楚如何使用ldply
來實現此目的)如何在幾個向量的排列上使用ldply?
tags <- c("tag1","tag2","tag3")
times <- c("2012-08-01 13:00:00","2012-08-07 21:00:00")
timesteps <- c("2m", "10m","60m", "90m")
query <- function(tag, time, timestep) {
sql <- paste("select tag, time, timestep, value from mydb where tag = '",tag,"' and time = '",time,"' and timestep = '",timestep,"'", sep="")
# pretend the line below is actually querying a database and returning a DF with one row
data.frame(tag = tag, time = time, timestep = timestep, value = rnorm(1))
}
# function works correctly!
query(time = times[1], tag = tags[1], timestep = timesteps[1])
# causes an error! (Error in FUN(X[[1L]], ...) : unused argument(s) (X[[1]]))
ldply(times, query, time = times, tag = tags, timestep = timesteps)
我想我可以用ldply嵌套三次,每一個載體,但我甚至不出去的第一級的!
任何想法,我能做些什麼?
它看起來更像是你想要使用的東西就像'mlply'。 – joran 2012-08-08 14:44:30