R 从文件名读取信息
我有以下字符列表:R 从文件名读取信息,r,filenames,R,Filenames,我有以下字符列表: $`1` [1] "TEMP_sim_zone1_cell_1_5_" $`5` [1] "TEMP_sim_zone1_cell_1_5_" $`6826` [1] "TEMP_sim_zone338_cell_6826_" $`9888` [1] "TEMP_sim_zone615_cell_9888_9890_9895" $`9890` [1] "TEMP_sim_zone615_cell_9888_9890_9895" $`9892` [1] "TEMP_s
$`1`
[1] "TEMP_sim_zone1_cell_1_5_"
$`5`
[1] "TEMP_sim_zone1_cell_1_5_"
$`6826`
[1] "TEMP_sim_zone338_cell_6826_"
$`9888`
[1] "TEMP_sim_zone615_cell_9888_9890_9895"
$`9890`
[1] "TEMP_sim_zone615_cell_9888_9890_9895"
$`9892`
[1] "TEMP_sim_zone614_cell_9892"
$`9895`
[1] "TEMP_sim_zone615_cell_9888_9890_9895"
由此,我想创建矩阵nrow=7,ncol=2:
1 1
5 2
6826 1
9888 1
9890 2
9892 1
9895 3
例如,应在文件TEMP_sim_zone615_cell_9888_9890_9895中读取单元格9888、9890和9895。9888位于位置1,9890位于位置2,9895位于位置3。此信息应取自文件名中单元格的位置
有人能帮我吗?以下是如何获得命名向量:
sapply(names(l), function(x) {
s <- sub('.+_cell_', '', l[[x]])
ids <- strsplit(s, '_', fixed = TRUE)[[1]]
which(ids == x)
})
# 1 5 6826 9888 9890 9892 9895
# 1 2 1 1 2 1 3
以下是如何获取命名向量:
sapply(names(l), function(x) {
s <- sub('.+_cell_', '', l[[x]])
ids <- strsplit(s, '_', fixed = TRUE)[[1]]
which(ids == x)
})
# 1 5 6826 9888 9890 9892 9895
# 1 2 1 1 2 1 3
另一种方法是,通过@Thomas的建议简化您的列表
temp2 <- mapply(match, names(temp), strsplit(gsub("^.*\\cell_", "", temp), "_"))
temp2 <- cbind(as.numeric(names(temp2)), as.numeric(temp2))
# [,1] [,2]
# [1,] 1 1
# [2,] 5 2
# [3,] 6826 1
# [4,] 9888 1
# [5,] 9890 2
# [6,] 9892 1
# [7,] 9895 3
另一种方法是,通过@Thomas的建议简化您的列表
temp2 <- mapply(match, names(temp), strsplit(gsub("^.*\\cell_", "", temp), "_"))
temp2 <- cbind(as.numeric(names(temp2)), as.numeric(temp2))
# [,1] [,2]
# [1,] 1 1
# [2,] 5 2
# [3,] 6826 1
# [4,] 9888 1
# [5,] 9890 2
# [6,] 9892 1
# [7,] 9895 3
+1,但若要获取OP请求的矩阵输出格式,请将返回值更改为cas.numericx,其值=x,然后将sapply封装在t.+1中,但若要获取OP请求的矩阵输出格式,请将返回值更改为cas.numericx,其值=x,然后将sapply封装在t中。这可以简化为mapplymatch、namestemp、,strsplitgsub^.*单元格,临时,无需额外重叠,因为gsub是矢量化的。这可以简化为mapplymatch,namestemp,strsplitgsub^.*单元格,临时,无需额外重叠,因为gsub是矢量化的。