将来自不同列表的参数传递给函数

时间:2014-03-04 11:00:13

标签: r for-loop apply

我试图创建一个函数,其中我从一个目录(例如subsample_dists / rep1 / matrices /)读取X量的距离矩阵(.dist)和从另一个目录读取相应的组文件(因子)(例如subsample_groups / REP1 /组)。因此,对于每个复制,该函数创建一个包含距离矩阵的.dist对象列表和相应的因子列表。

我想应用一个函数(betadisper function {vegan}),将这些列表的第i个和(对应的)第j项作为参数传递。这可以使用for loops直截了当,但是我可以在这里使用apply-like function吗?

很抱歉只提供虚拟代码而没有数据

# Code

myfunc <- function(rep){


# Read-in distance matrices from a replicate
____________________________________________

  setwd("~/subsample_dist/")

# Paths to each distance matrices within each replicate 

  rep_dir_dist <- list.files(path=rep, pattern="*.dist", full.names = TRUE, recursive = TRUE, include.dirs = FALSE)

# Apply matrice paths to read function. Returns list of .dist objects 
  dist_list <- sapply(rep_dir_dist, FUN=read_dist) #function for reading .dist class matrix


# Read-in grouping-files from a replicate
_________________________________________

  setwd("~/subsample_groups/")

# Paths to each groupfile within each replicate 

  rep_dir_group <- list.files(path=rep, pattern="*.csv", full.names = TRUE, recursive = TRUE, include.dirs = FALSE)

# Apply groupfile paths to read function. Returns a list of factors
  group_list <- sapply(rep_dir_group, FUN=read.csv)
___________________________________________________________________

# UPDATE (From comments)

# Applying betadisper; passing ith and jth item from the above lists as arguments
betadis <- mapply(betadisper, d = dist_list, group = group_list, SIMPLIFY=FALSE)

# Running {vegans} anova-like permutest function
perm <- lapply(betadis, function(x){permutest(x)})

# extracting the part containing info about the stats
s <- lapply(p, function(x) x$tab)

return(s)

}

非常感谢任何指示,谢谢!

0 个答案:

没有答案