2012-08-08 23 views
1

我正在構建一個旨在多次查詢數據庫的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嵌套三次,每一個載體,但我甚至不出去的第一級的!

任何想法,我能做些什麼?

+0

它看起來更像是你想要使用的東西就像'mlply'。 – joran 2012-08-08 14:44:30

回答

3

我認爲,如果你使用mdply(或等價只是mapply),這是相當簡單:

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(tags, times, timesteps) { 

    sql <- paste("select tag, time, timestep, value from mydb where 
      tag = '",tags,"' and time = '",times,"' and timestep = '",timesteps,"'", sep="") 
    # pretend the line below is actually querying a database and returning a DF with one row 
    data.frame(tag = tags, time = times, timestep = timesteps, value = rnorm(1)) 

} 

dat <- expand.grid(tags, times, timesteps) 
colnames(dat) <- c('tags','times','timesteps') 

mdply(dat,query) 

注意變量名很輕微的變化,使他們都在數據和函數參數同意。

+0

很好的答案!謝謝。我想知道'expand.grid'是不知何故的答案,但不記得它的名字,也不知道如何處理結果! – 2012-08-08 22:51:10

1

這將讓工作完成但它只使用apply。首先,我創建一個包含感興趣組合然後I的對象重寫查詢以從該對象中取出一行而不是3個輸入。

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") 

# Use expand.grid to create an object with all the combinations 
dat <- expand.grid(tags, times, timesteps) 

# Rewrite query to take in a row of dat 
query <- function(row) { 
    # extract the pieces of interest 
    tag <- row[1] 
    time <- row[2] 
    timestep <- row[3] 

    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 on a single row 
query(dat[1,]) 

# apply the function to each row 
j <- apply(dat, 1, query) 
# bind all the output together 
do.call(rbind, j) 
+0

謝謝你的回答,Dason。我真的應該更多地瞭解這些問題的基本功能。 – 2012-08-08 22:52:15

+0

@ TommyO'Dell喬蘭的答案肯定更好。我不太習慣應用類型的函數,所以我通常將問題簡化爲像我這樣做的單一維度。 – Dason 2012-08-08 23:07:21