如何从R数据框中的字符列中提取第一位数字?
如果我们在数据框中有一个字符列,其中包含字符串以及数字值,并且数字值的第一位具有可以帮助进行数据分析的含义,那么我们可以提取这些第一位。为此,我们可以使用stringi包中的stri_extract_first函数。
例1
考虑以下数据帧-
> x1<-1:20输出结果> y1<-sample(c("HT23L","HT14L","HT32L"),20,replace=TRUE)
> df1<-data.frame(x1,y1)
> df1
x1 y11 1 HT14L
2 2 HT14L
3 3 HT23L
4 4 HT14L
5 5 HT32L
6 6 HT32L
7 7 HT14L
8 8 HT32L
9 9 HT32L
10 10 HT32L
11 11 HT23L
12 12 HT32L
13 13 HT14L
14 14 HT23L
15 15 HT14L
16 16 HT23L
17 17 HT23L
18 18 HT23L
19 19 HT23L
20 20 HT23L
加载stringi包并提取y1列中的第一位-
> library(stringi)输出结果> stri_extract_first(df1$y1,regex="\\d")
[1] "1" "1" "2" "1" "3" "3" "1" "3" "3" "3" "2" "3" "1" "2" "1" "2" "2" "2" "2"[20] "2"
例2
> x2<-sample(c("India1RT1","UK5RT1","Egypt2PT4"),20,replace=TRUE)输出结果> y2<-rpois(20,5)
> df2<-data.frame(x2,y2)
> df2
x2 y21 India1RT1 2
2 India1RT1 8
3 India1RT1 7
4 India1RT1 6
5 UK5RT1 6
6 India1RT1 5
7 UK5RT1 6
8 India1RT1 6
9 India1RT1 7
10 UK5RT1 10
11 Egypt2PT4 8
12 Egypt2PT4 5
13 Egypt2PT4 7
14 India1RT1 2
15 UK5RT1 3
16 Egypt2PT4 5
17 UK5RT1 3
18 Egypt2PT4 6
19 Egypt2PT4 3
20 UK5RT1 5
提取列x2中的第一位数字-
> stri_extract_first(df2$x2,regex="\\d")输出结果
[1] "1" "1" "1" "1" "5" "1" "5" "1" "1" "5" "2" "2" "2" "1" "5" "2" "5" "2" "2"[20] "5"
范例3
> x3<-sample(c("abc123","dfe456"),20,replace=TRUE)输出结果> y3<-rnorm(20)
> df3<-data.frame(x3,y3)
> df3
x3 y31 abc123 0.1027005
2 dfe456 0.2297002
3 dfe456 -0.1441151
4 dfe456 1.0510760
5 abc123 0.8182656
6 dfe456 -0.5018968
7 dfe456 0.2957634
8 abc123 -0.4240910
9 dfe456 -1.0700713
10 dfe456 -0.3374661
11 dfe456 -0.4654241
12 dfe456 -0.4542710
13 abc123 0.6969808
14 dfe456 -0.6514574
15 abc123 0.2258769
16 dfe456 -0.5348958
17 abc123 0.6629195
18 dfe456 1.0998636
19 dfe456 -1.3147809
20 dfe456 -2.3015384
提取列x3中的第一位数字-
> stri_extract_first(df3$x3,regex="\\d")输出结果
[1] "1" "4" "4" "4" "1" "4" "4" "1" "4" "4" "4" "4" "1" "4" "1" "4" "1" "4" "4"[20] "4"
以上是 如何从R数据框中的字符列中提取第一位数字? 的全部内容, 来源链接: utcz.com/z/354815.html