R - 按定义的间隔拆分列表

时间:2015-02-05 01:01:01

标签: r list subset intervals

我有一个文件列表,其长度始终是12的倍数。这是一个简化的示例:

files <- c("LC82210802013322LGN00_B1.TIF", "LC82210802013322LGN00_B10.TIF", 
"LC82210802013322LGN00_B11.TIF", "LC82210802013322LGN00_B2.TIF", 
"LC82210802013322LGN00_B3.TIF", "LC82210802013322LGN00_B4.TIF", 
"LC82210802013322LGN00_B5.TIF", "LC82210802013322LGN00_B6.TIF", 
"LC82210802013322LGN00_B7.TIF", "LC82210802013322LGN00_B8.TIF", 
"LC82210802013322LGN00_B9.TIF", "LC82210802013322LGN00_BQA.TIF", 
"LC82210802013354LGN00_B1.TIF", "LC82210802013354LGN00_B10.TIF", 
"LC82210802013354LGN00_B11.TIF", "LC82210802013354LGN00_B2.TIF", 
"LC82210802013354LGN00_B3.TIF", "LC82210802013354LGN00_B4.TIF", 
"LC82210802013354LGN00_B5.TIF", "LC82210802013354LGN00_B6.TIF", 
"LC82210802013354LGN00_B7.TIF", "LC82210802013354LGN00_B8.TIF", 
"LC82210802013354LGN00_B9.TIF", "LC82210802013354LGN00_BQA.TIF", 
"LC82210802014021LGN00_B1.TIF", "LC82210802014021LGN00_B10.TIF", 
"LC82210802014021LGN00_B11.TIF", "LC82210802014021LGN00_B2.TIF", 
"LC82210802014021LGN00_B3.TIF", "LC82210802014021LGN00_B4.TIF", 
"LC82210802014021LGN00_B5.TIF", "LC82210802014021LGN00_B6.TIF", 
"LC82210802014021LGN00_B7.TIF", "LC82210802014021LGN00_B8.TIF", 
"LC82210802014021LGN00_B9.TIF", "LC82210802014021LGN00_BQA.TIF", 
"LC82210802014037LGN00_B1.TIF", "LC82210802014037LGN00_B10.TIF", 
"LC82210802014037LGN00_B11.TIF", "LC82210802014037LGN00_B2.TIF", 
"LC82210802014037LGN00_B3.TIF", "LC82210802014037LGN00_B4.TIF", 
"LC82210802014037LGN00_B5.TIF", "LC82210802014037LGN00_B6.TIF", 
"LC82210802014037LGN00_B7.TIF", "LC82210802014037LGN00_B8.TIF", 
"LC82210802014037LGN00_B9.TIF", "LC82210802014037LGN00_BQA.TIF", 
"LC82210802014085LGN00_B1.TIF", "LC82210802014085LGN00_B10.TIF", 
"LC82210802014085LGN00_B11.TIF", "LC82210802014085LGN00_B2.TIF", 
"LC82210802014085LGN00_B3.TIF", "LC82210802014085LGN00_B4.TIF", 
"LC82210802014085LGN00_B5.TIF", "LC82210802014085LGN00_B6.TIF", 
"LC82210802014085LGN00_B7.TIF", "LC82210802014085LGN00_B8.TIF", 
"LC82210802014085LGN00_B9.TIF", "LC82210802014085LGN00_BQA.TIF"
)

这些文件是卫星图像。每个日期总共有12个文件(或带)。在这种情况下,有五个组(日期),每个组有12个文件,总共60个元素。

我需要做的是将此列表拆分为12个组,最好是创建一个新变量。使用上面提供的示例数据,新变量将有五个元素(对应于日期),每个元素包含12个文件:

new<-list()
length(new) <- length(files)/12

# CODE BELOW DOESN'T WORK. I JUST WANT TO SHOW WHAT I NEED TO DO
new[1] <- files[1:12]
new[2] <- files[13:24]
new[3] <- files[25:36]
new[4] <- files[37:48]
new[5] <- files[49:60]

如何找到此问题的通用解决方案?通用的意思是原始文件列表总是12的倍数,但并不总是有60个元素的长度 - 有时是72,有时是120。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

经过一番深入研究后,我提出了以下解决方案:

new <- split(files, ceiling(seq_along(files)/12))

哪作得好。还有更好的主意吗?

谢谢,

蒂亚戈。