根据列表中数据帧的文件名在大列表中分组数据

问题描述 投票:0回答:1

我正在处理包含450个数据帧的大列表。我将举例说明数据帧的名称

ALL_SM51_SE1_hourly, ALL_SM201_SE1_hourly, ALL_SM501_SE1_hourly
ALL_SM51_SE2_hourly, ALL_SM201_SE2_hourly, ALL_SM501_SE2_hourly
...................................................................
ALL_SM51_SE150_hourly, ALL_SM201_SE150_hourly, ALL_SM501_SE150_hourly

这些数据帧包含在不同深度(5cm,20cm,50cm,在文件名中由“ SM51,SM201,SM501”表示]的测量土壤湿度数据),并且有150个传感器文件名中的“ SE1,SE2,SE3,...”),这就是为什么我将450个数据帧存储在列表中的原因。

我想做的事情:我想为每个包含3个元素的传感器创建一个新列表(做一个子集)。所以我想列出一个SE1,SE2,SE3,...,SE150以及相应的测量深度。

我已经搜索了问题的合适答案,但是我只找到了通过特定值表示子集数据的答案,但是我想通过文件名来表示子集

有人知道该怎么做吗?

r list subset filenames
1个回答
0
投票

使用正则表达式,您可以识别可以从un.sepaste的唯一传感器new.names。>>

un.se <- gsub(".*SE(\\d+).*", "\\1", names(lst))
new.names <- paste0("SE", unique(un.se))
tmp <- setNames(split(lst, un.se), paste0("SE", unique(un.se)))
res <- lapply(tmp, function(x) {
  nm <- gsub(".*SM(\\d+).*", "\\1", names(x))
  setNames(lapply(x[order(nm)], data.frame), paste0("d", gsub("1$", "", nm)))
  })

结果

res
# $SE1
# $SE1$d5
#   x1 x2 x3
# 1  1  2  3
# 
# $SE1$d20
#   x1 x2 x3
# 1  1  2  3
# 
# $SE1$d50
#   x1 x2 x3
# 1  1  2  3
# 
# 
# $SE2
# $SE2$d5
#   x1 x2 x3
# 1  1  2  3
# 
# $SE2$d20
#   x1 x2 x3
# 1  1  2  3
# 
# $SE2$d50
#   x1 x2 x3
# 1  1  2  3

玩具数据

lst <- list(ALL_SM51_SE1_hourly = 1, ALL_SM201_SE1_hourly = 1, ALL_SM501_SE1_hourly = 1, 
    ALL_SM51_SE2_hourly = 1, ALL_SM201_SE2_hourly = 1, ALL_SM501_SE2_hourly = 1)
© www.soinside.com 2019 - 2024. All rights reserved.