R; 用for循环遍历向量填充矩阵

弗格

我对R并不是很有经验,并且一直在努力重复几天的代码来填充数据矩阵。我的本能是创建一个for循环。

我是一名生物学专业的学生,​​致力于利用R包的色差来处理图像集之间的色差。相关数据已作为8x4矩阵列表加载到R中(每个矩阵描述一个图像中的颜色)。五张图像组成一组,总共有100套。每个集合都由一个数字标识(不是1-100,它是一个中断的序列,但是我已经将数字序列存储在称为“数字列表”的向量中)。我已经编写了代码,以正确的格式为第一组提取所需的数据,如下所示;

## extract the list of matrices belonging to the first set (A3) from the the full list
A3<-histlist[grep('^3',names(histlist))] 
## create a colour distance matrix (cdm), ie a pairwise comparison of "similarity" between the five matrices stored in A3
cdm3<-colordistance::getColorDistanceMatrix(A3, method="emd", plotting=FALSE)
## convert to data frame to fix row names
cdm3df<-as.data.frame(cdm3) 
## remove column names
names(cdm3df)<-NULL 
## return elements in the first row and column 2-5 only (retains row names).
cdm3filtered<-cdm3df[1,2:5]

现在,我想用“数字列表”中的每个数字替换上面代码中的“ 3”(不确定它们应为as.factor还是as.numeric)。我尝试了很多尝试,for (i in numberlist) {...}但没有成功输出。对我来说,将循环的输出存储在存储矩阵中是有意义的。matrix(nrow=100,ncol=4)但是我非常困窘,无法通过迭代上面的代码来逐行填充存储矩阵...

任何帮助将不胜感激!

更新

我希望循环的输出看起来像什么(+附加在存储矩阵中);

> cdm17filtered                                
17clr 0.09246918 0.1176651 0.1220622 0.1323586

这是我的尝试:

for (i in numberlist$X) {
  A[i] <- histlist[grep(paste0('^',i),names(histlist))]
  cdm[i] <- colordistance::getColorDistanceMatrix(A[i], method="emd", plotting=FALSE)
  cdm[i]df <- as.data.frame(cdm[i])
  cdm[i]filtered <- cdm[i]df[1,2:5]
  print(A[i]) # *insert in n'th column of storage matrix
}

上面的方法不起作用,并且我缺少在存储矩阵中存储循环输出所需的最后一位。(建议我不要使用rbind来填充存储矩阵,因为它很慢。)

完善

在尝试中,您使用了无效的R名称,其中的非字母数字字符未转义,cdm[i]dfcdm[i]filtered看来您打算从较大的容器(例如对象列表)中进行索引。

要正确地概括编号列表中所有项目的流程,请调整^3设置。具体来说,构建空列表并循环地通过index分配[i]

# INITIALIZE LISTS (SAME LENGTH AS numberlist)
A <- vector(mode="list", length = length(numberlist))
cdm_matrices <- vector(mode="list", length = length(numberlist))
cdm_dfs <- vector(mode="list", length = length(numberlist))
cdm_filtered_dfs  <- vector(mode="list", length = length(numberlist))

# POPULATE LISTS
for (i in numberlist$X) {
  ## extract the list of matrices belonging to the first set
  A[i] <- histlist[grep(paste0('^', i), names(histlist))] 

  ## create a colour distance matrix (cdm)
  cdm_matrices[i] <- colordistance::getColorDistanceMatrix(A[i], method="emd", plotting=FALSE)

  ## convert to data frame to fix row names and remove column names
  cdm_dfs[i] <- setNames(as.data.frame(cdm_matrices[i]), NULL)

  ## return elements in the first row and column 2-5 only (retains row names).
  cdm_filtered_dfs[i] <- cdm_dfs[i][1,2:5]
}

或者,如果仅需要cdm_filtered_df返回的最后一个对象,则lapply在不需要使用列表或对列表进行索引的情况下使用,并且所有对象在功能范围内都是本地的(即,从未保存在全局环境中):

cdm_build <- function(i) {
  A <- histlist[grep(paste0('^', i), names(histlist))]    
  cdm <- colordistance::getColorDistanceMatrix(A, method="emd", plotting=FALSE)    
  cdm_df <- setNames(as.data.frame(cdm), NULL)    
  cdm_filtered_df <- cdm_df[1,2:5]

  return(cdm_filtered_df)    # REDUNDANT AS LAST LINE IS RETURNED BY DEFAULT
}

# LIST OF FILTERED CDM DATA FRAMES
cdm_filtered_dfs <- lapply(numberlist, cdm_build)

最后,使用上述两种解决方案时,如果要构建单个数据帧,请在rbind运行do.call()

cdm_final_df <- do.call(rbind, cdm_filtered_dfs)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章