Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 当字符串中的数字超过1个时,从该字符串中提取最小值_R_Regex - Fatal编程技术网

R 当字符串中的数字超过1个时,从该字符串中提取最小值

R 当字符串中的数字超过1个时,从该字符串中提取最小值,r,regex,R,Regex,我有一系列包含数字的字符串。其中一些字符串包含如下2个数字: library(tidyverse) df1 <- data.frame(x ="Want to extract both values 7 + 8", y = "var") 以下是我提取数字的方法: df1$comp_num = regmatches(df1$x,gregexpr('[0-9]+',df1$x)) 但最后我得到了一个列表列。以下是我迄今为止所尝试的: df1$unnest

我有一系列包含数字的字符串。其中一些字符串包含如下2个数字:

library(tidyverse)
df1 <- data.frame(x ="Want to extract both values 7 + 8",
                  y = "var")
以下是我提取数字的方法:

df1$comp_num = regmatches(df1$x,gregexpr('[0-9]+',df1$x))
但最后我得到了一个列表列。以下是我迄今为止所尝试的:

df1$unnestval <- tidyr::unnest(df1$comp_num)

df1$separ <- tidyr::separate(df1$comp_num)

df1$unlistval <- unlist(df1$comp_num)

df1$unnestval如果您愿意切换到
data.table
,这会有所帮助

library(data.table)
DT <- data.table(C1=replicate(5, paste0(sample(LETTERS, 2), sample(1:9,2), collapse = "")))
DT
     C1
1: Y7J6
2: J8O5
3: M4G6
4: I5Q9
5: T3M1

## Extracting Digits
DT[ , C2:=lapply(C1, function(x){ gsub("[^\\d]", "", x, perl = T) }), by=C1]

## Extracting Min Value
DT[, C3:=lapply(C2, function(x){min(as.integer(unlist(strsplit(x, ""))))}), by=C1]

## Extracting Max Value
DT[, C4:=lapply(C2, function(x){max(as.integer(unlist(strsplit(x, ""))))}), by=C1]
DT
     C1 C2 C3 C4
1: Y7J6 76  6  7
2: J8O5 85  5  8
3: M4G6 46  4  6
4: I5Q9 59  5  9
5: T3M1 31  1  3
库(data.table)

DT
df1$comp_num=min(未列出(regmatches(df1$x,gregexpr('[0-9]+',df1$x)))
Fwiw,使用unnest是错误的。它需要两个参数:数据集和列,类似于
tidyr::unnest(df1,comp_num)
library(data.table)
DT <- data.table(C1=replicate(5, paste0(sample(LETTERS, 2), sample(1:9,2), collapse = "")))
DT
     C1
1: Y7J6
2: J8O5
3: M4G6
4: I5Q9
5: T3M1

## Extracting Digits
DT[ , C2:=lapply(C1, function(x){ gsub("[^\\d]", "", x, perl = T) }), by=C1]

## Extracting Min Value
DT[, C3:=lapply(C2, function(x){min(as.integer(unlist(strsplit(x, ""))))}), by=C1]

## Extracting Max Value
DT[, C4:=lapply(C2, function(x){max(as.integer(unlist(strsplit(x, ""))))}), by=C1]
DT
     C1 C2 C3 C4
1: Y7J6 76  6  7
2: J8O5 85  5  8
3: M4G6 46  4  6
4: I5Q9 59  5  9
5: T3M1 31  1  3