R foreach数据返回

时间:2016-07-22 14:31:28

标签: r

假设我有一组实验和控制数据,并希望获取每个组的标准偏差并返回两个结果列表,以下代码可用

experimental <- replicate(3,rnorm(10))
control      <- replicate(3,rnorm(10))

# experimental and control data look like
#             [,1]        [,2]       [,3]
# [1,] -1.98749717 -1.37039284 -2.1681745
# [2,]  0.51401787  0.22997560  1.3334176
# ...ten rows total

rcomb  <- function(...) { mapply('rbind',...,SIMPLIFY=FALSE) } #for combining results

result <- foreach(i=iter(experimental,by='row'),j=iter(control,by='row'),
               .combine='rcomb',.multicombine=TRUE) %dopar% {

                c(sd(i),sd(j))

                }

result[[1]] #experimental results (what I want)

#           [,1]
# [1,] 0.9527986
# [2,] 0.3180453
# [3,] 0.8088746
# ...ten rows total; likewise for the control; what I want

实际上我从使用group concat的sql查询中选择数据,但无法获得结果以返回我想要的方式。我必须忽视一些简单的事情。

data <- dbGetQuery(database_connection, "select group_concat from ...

# data looks like
#
#    experimental  control
# 1      14,14,14 14,14,14
# 2      15,15,15 15,15,15
# 3         9,9,9    9,9,9
#
# ...many rows

attach(data)

result <- foreach(i=iter(data$experimental,by="row"),j=iter(data$control,by="row",
               .combine='rcomb',.multicombine=TRUE)) %dopar% {

                c(sd(as.numeric(unlist(strsplit(i,',')))),
                  sd(as.numeric(unlist(strsplit(j,',')))))

                }

result[[1]] # instead of what I want it only gives the first pair

# [1] 0 0

虽然我是R的新手,但我仔细研究了所有数据类型,我缺少什么?

0 个答案:

没有答案