在R中的ID号列中的一个点之后提取数字
Extract number after a point in ID number column in R
所以这是我在 R 中的数据集中的一列:
ID
1.34789
1.90763
63107
84749
1.02195
预期输出
ID
34789
90763
63107
84749
02195
我只找到了关于字符串和句子的代码,但是 none 这种情况。
使用 stringr 你可以做:
id <- c(1.34789, 1.90763, 63107, 84749, 1.02195)
library(stringr)
str_remove(id, "^\d+\.")
#> [1] "34789" "90763" "63107" "84749" "02195"
#Or using base r `gsub`
gsub("^\d+\.", "", id)
由 reprex package (v2.0.0)
于 2021-04-29 创建
尝试将 gsub
与正则表达式一起使用 \b1.\b
df <- data.frame(ID=c(1.34789,1.90763,63107,84749,1.02195))
gsub(pattern = "\b1.\b",replacement = "",x = df$ID,perl = T)
[1] "34789" "90763" "63107" "84749" "02195"
所以这是我在 R 中的数据集中的一列:
ID |
---|
1.34789 |
1.90763 |
63107 |
84749 |
1.02195 |
预期输出
ID |
---|
34789 |
90763 |
63107 |
84749 |
02195 |
我只找到了关于字符串和句子的代码,但是 none 这种情况。
使用 stringr 你可以做:
id <- c(1.34789, 1.90763, 63107, 84749, 1.02195)
library(stringr)
str_remove(id, "^\d+\.")
#> [1] "34789" "90763" "63107" "84749" "02195"
#Or using base r `gsub`
gsub("^\d+\.", "", id)
由 reprex package (v2.0.0)
于 2021-04-29 创建尝试将 gsub
与正则表达式一起使用 \b1.\b
df <- data.frame(ID=c(1.34789,1.90763,63107,84749,1.02195))
gsub(pattern = "\b1.\b",replacement = "",x = df$ID,perl = T)
[1] "34789" "90763" "63107" "84749" "02195"