سؤال

I have the following character list:

$`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"

From this, I want to create this matrix (nrow=7, ncol=2):

   1  1
   5  2
6826  1
9888  1
9890  2
9892  1
9895  3

For example, cells 9888, 9890 and 9895 should be read in file "TEMP_sim_zone615_cell_9888_9890_9895". 9888 is in position 1, 9890 in position 2 and 9895 in position 3 (this information should be taken from the position of the cells in the filename).

Can someone help me?

هل كانت مفيدة؟

المحلول

Here's another way, where temp is your list (simplified by @Thomas's advice)

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

نصائح أخرى

Here's how to get a named vector:

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 
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top