简体   繁体   English

循环遍历 3-d 数组并在 R 中创建子集向量

[英]Looping through a 3-d array and creating subset vectors in R

I have the following MWE:我有以下 MWE:

N <- 84 #Number of datasets to pull data from
dates <- c("2010.01", "2010.02", "2010.03", "2010.04", "2010.05", "2010.06", "2010.07", "2010.08",
 "2010.09", "2010.10", "2010.11", "2010.12", "2011.01", "2011.02", "2011.03", "2011.04", "2011.05",
 "2011.06", "2011.07", "2011.08", "2011.09", "2011.10", "2011.11", "2011.12", "2012.01", "2012.02",
 "2012.03", "2012.04", "2012.05", "2012.06", "2012.07", "2012.08", "2012.09", "2012.10", "2012.11",
 "2012.12", "2013.01", "2013.02", "2013.03", "2013.04", "2013.05", "2013.06", "2013.07", "2013.08",
 "2013.09", "2013.10", "2013.11", "2013.12", "2014.01", "2014.02", "2014.03", "2014.04", "2014.05",
 "2014.06", "2014.07", "2014.08", "2014.09", "2014.10", "2014.11", "2014.12", "2015.01", "2015.02",
 "2015.03", "2015.04", "2015.05", "2015.06", "2015.07", "2015.08", "2015.09", "2015.10", "2015.11",
 "2015.12", "2016.01", "2016.02", "2016.03", "2016.04", "2016.05", "2016.06", "2016.07", "2016.08",
 "2016.09", "2016.10", "2016.11", "2016.12") #list of all dates to loop through

A <- list()
for (k in seq_along(dates)) {
  A[[k]] <- read_excel(paste0("~/R/data.", dates[k], ".xlsx"), range = "B3:EO94")
}
data <- array(unlist(A), dim=c(91,144,84)) #3-dimensional array

####Problem here####
countries <- c("Can", "US")
coord <- c("15,34", "23,28")

long <- c("34", "28")
lat <- c("15", "23")

for (c in seq_along(countries)) {
for (i in seq_along(lat)) {
for (j in seq_along(long)) {
  nam <- sprintf("ci%d%d", c, i)
  assign(nam, data[i,j,])
}}}

I am basically trying to create two vectors that I could write individually as Can <- data[15,34,] and US <- data[23,28,] .我基本上是在尝试创建两个向量,我可以将它们分别写为Can <- data[15,34,]US <- data[23,28,] My problem lies in me wanting to try and write data[[x,y],] to recognize the the comma within the looped value.我的问题在于我想尝试写入 data[[x,y],] 来识别循环值中的逗号。 I was wondering if there was a way around this?我想知道是否有办法解决这个问题? I don't think having a separate i and j to loop through would work because it would make 4 variables no?我不认为有一个单独的ij循环会起作用,因为它会产生 4 个变量吗?

I could do them individually but I have nearly 100 countries to do this for, as such, I look towards loops.我可以单独完成它们,但我有近 100 个国家/地区可以这样做,因此,我期待循环。

We can modify the loop to我们可以将循环修改为

for (c in seq_along(countries)) {
   for (i in seq_along(lat)) {
    for (j in seq_along(long)) {
     #nam <- sprintf("s%s%s", countries[c], lat[i], long[j])
     #Or with `paste`
     nam <- paste0(countries[c], lat[i], long[j])
     assign(nam, data[as.integer(lat[i]),as.integer(long[j]),])
  }}}

If we do "[c]" in paste , it won't do any interpolation.如果我们在paste"[c]" ,它不会做任何插值。 It would be just the literal character "[c]".它只是文字字符“[c]”。 We could do interpolation with sprintf我们可以用sprintf进行插值

 ..
  nam <- sprintf("ci%d%d", c, i)
 ..

Or with glue::glue或者用glue::glue

..
 nam <- as.character(glue::glue("ci{c}{i}"))
..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM