在循环中使用粘贴/获取

时间:2018-10-16 14:37:39

标签: r loops get paste

我的问题很琐碎,我想在这样的循环中粘贴一个向量

 Mysheetlandscap <- excel_sheets("C:/FAPSEP_Eucalyptus/FAPSEP/AHMED_GDAY/CloneParnew5_forCALIB2.xlsx")

for(j in 1:length(Mysheetlandscap)){

  ClonePar <- read_excel("C:/FAPSEP_Eucalyptus/FAPSEP/AHMED_GDAY/CloneParnew5_forCALIB2.xlsx", sheet = Mysheetlandscap[j])

  x <- ClonePar$,j,[!is.na(ClonePar$MIN_CL)]  #should return the vector ClonePar$j
  }

所以我尝试了

get(paste0("ClonePar$",j))

get(paste0(“ ClonePar $”,j))中的错误:   找不到对象'Cal_OCT_18_GLM $ C041H'

我缺少什么?

2 个答案:

答案 0 :(得分:0)

很难理解...但是也许正是您想要的:

ClonePar <- lapply(1:length(Mysheetlandscap), function(j)
read_excel("C:/FAPSEP_Eucalyptus/FAPSEP/AHMED_GDAY/CloneParnew5_forCALIB2.xlsx", sheet = j))

j<-10
x <- ClonePar[[10]]

答案 1 :(得分:0)

注意:我使用seq_along而不是1:length(),以便在数据框为空的情况下不会得到任何时髦的输出

如果您想使用for循环:

ClonePar <- list() #create empty list

for(j in seq_along(Mysheetlandscap)){

  ClonePar[j] <- read_excel("C:/FAPSEP_Eucalyptus/FAPSEP/AHMED_GDAY/CloneParnew5_forCALIB2.xlsx", sheet = Mysheetlandscap[j])

}

ClonePar[[1]] #look the first element (a data frame) in your newly filled list

如果您想使用apply系列(根据弗拉基米尔的回答):

ClonePar <- lapply(seq_along(Mysheetlandscap),
                   function(j) read_excel("C:/FAPSEP_Eucalyptus/FAPSEP/AHMED_GDAY/CloneParnew5_forCALIB2.xlsx", sheet = j))

在使用lapply的情况下,您不必在运行向量化操作之前创建一个空白的list