我的数据如下:x = when,ch_d_mean,<,29.55 我想从x提取29.55。
x = 'when,ch_d_mean,< , 29.55'
我想要值为29.55
答案 0 :(得分:3)
这是基本的R选项:
x <- "when,ch_d_mean,< , 29.55"
regmatches(x, gregexpr("\\d+(?:\\.\\d+)?", x))[[1]]
[1] "29.55"
请注意,这种方法对于捕获输入字符串中的任意数量的数字都是可靠的。
答案 1 :(得分:2)
我们可以使用async function binanceTrailingSLOrder(symbol, orderId, quantity, oldPrice, percentage, active) {
const clean_trade = client.ws.trades([symbol], async trade => { //run websocket
var livePrice = parseFloat(binance_symbols[symbol]["close"]); //set new price to live price
if (active == true) {
try {
const order = await binanceCheckOrderStatus(symbol, orderId);
if (!order) {
throw new Error('order not found')
}
switch (order.status) {
case "PENDING":
if (livePrice >= (oldPrice * ((100 + percentage) / 100)) && active == true) {
active = false;
const r4 = await binanceCancelOrder(symbol, orderId);
if (r4) {
var newSL = livePrice * ((100 - percentage) / 100);
var r5 = binanceStopOrder(symbol, 'SELL', r4.origQty, newSL, newSL);
if (r5) {
orderId = r5.orderId; quantity = r5.origQty; oldPrice = r5.price;
active = true;
}
}
}
break;
default:
break;
}
}
catch(error) {
console.log('error found: ', error);
}
}
});
}
async function binanceCheckOrderStatus(symbol, orderId) {
if(!orderId){
console.log("order Id not found");
return false;
} else {
var client = loadBinanceKeys2();
return new Promise((resolve, reject) => {
client.getOrder({
symbol: symbol,
orderId: orderId,
recvWindow: 1000000
}).then((order) => {
resolve(order);
}).catch((err) => {
reject(err);
});
});
}
}
中的parse_number
readr
或来自`stringr
的library(readr)
parse_number(x)
#[1] 29.55
str_extract
或使用基数R中的as.numeric(str_extract(x, "[0-9.]+"))
#[1] 29.55
gsub
或使用基数R中的as.numeric(gsub("[^0-9.]+", "", x))
#[1] 29.55
substring
注意:最后一种方法基于位置,如果它位于字符串的末尾并占据最后5个位置
答案 2 :(得分:1)
另一种可能性。然后,您可以继续删除多余的空白。
x = 'when, ch_d_mean,>=, 29.55, &, ch_d_mean,< ,0.1090'
res<-unlist(strsplit(x,","))
res[grep("[0-9]",res)]
#[1] " 29.55" "0.1090"