извлечь разные строки после совпадения, используя R

Я предлагаю простую рекурсивную функцию -генератора :

// Generate all combinations of array elements:
function* cartesian(head, ...tail) {
  let remainder = tail.length ? cartesian(...tail) : [[]];
  for (let r of remainder) for (let h of head) yield [h, ...r];
}


// Example:
for (let c of cartesian([0,1], [0,1,2,3], [0,1,2])) {
  console.log(...c);
}

1
задан סטנלי גרונן 24 February 2019 в 10:25
поделиться

2 ответа

Как насчет удаления всего остального?

data <- c("Demand = 001 979", "Demand = -08 976 (154)", "Demand = -01 975 (359)")
data <- gsub("Demand = ", "", x = data)
data <- trimws(gsub("\\(.*\\)", "", x = data))

out <- list()

out[[1]] <- sapply(data, "[", 1)
out[[2]] <- sapply(data, "[", 2)
out

[[1]]
[1] "001" "-08" "-01"

[[2]]
[1] "979" "976" "975"
0
ответ дан Roman Luštrik 24 February 2019 в 10:25
поделиться

Возможность с str_extract_all() из stringr:

sapply(str_extract_all(x, "-?[0-9]+?[0-9]*"), function(x) x[1])

[1] "001" "-08" "-01"

sapply(str_extract_all(x, "-?[0-9]+?[0-9]*"), function(x) x[2])

[1] "979" "976" "975"

Или используя идею @Ruman Luštrik с strsplit():

sapply(strsplit(gsub("Demand = ", "", x), " "), function(x) x[1])

[1] "001" "-08" "-01"
0
ответ дан tmfmnk 24 February 2019 в 10:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: