确定字符串是否包含数字?

时间:2020-04-10 13:53:10

标签: r

我一直试图用5分钟的时间来表达这个标题,以避免它成为一个类似短语的问题。没有运气,如果对此已经讨论过,那么抱歉。在此特定主题上找不到其他线程。

简单地说,我想确定类字符串中是否存在数字。如果为true,则应用其他功能。

这是一个狡猾的尝试。

x <- "900 years old"

if(str_detect(x, ">=0")) {

print("contains numbers")
}

因此,显然问题是我正在尝试在字符串中使用关系运算符。考虑到此类,我该如何识别数字字符?

2 个答案:

答案 0 :(得分:4)

[0-9]是用于数字0到9的正则表达式模式。您还可以使用特殊模式\d[:digit:](用于数字)。在R中,您必须在特殊模式中添加额外的转义符。所有这些都应该起作用:

str_detect(x, "[0-9]")
str_detect(x, "\\d")
str_detect(x, "[[:digit:]]")

答案 1 :(得分:1)

有了 map; markers: L.Layer[] = []; popupText = "Some popup text"; markerIcon = { icon: L.icon({ iconSize: [25, 41], iconAnchor: [10, 41], popupAnchor: [2, -40], // specify the path here iconUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-icon.png", shadowUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-shadow.png" }) }; options = { layers: [ L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", { maxZoom: 18, attribution: "" }) ], zoom: 10, center: L.latLng(46.879966, -121.726909) }; addMarker() { // const newMarker = L.marker( // [ // 46.879966 + 0.1 * (Math.random() - 0.5), // -121.726909 + 0.1 * (Math.random() - 0.5) // ], // this.markerIcon // ); const newMarker = L.marker([46.879966, -121.726909], this.markerIcon); this.markers.push(newMarker); newMarker.addTo(this.map); } onMapReady(map: L.Map) { this.map = map; // Do stuff with map } ,我们可以使用base R

grepl
相关问题