我有一个字符向量,想要从中获取整数和浮点数。
a <- c("Sam is 5 years old", "Ram is 3.7 years old" , "John is 17 years 2 months old")
输出应该是:
[1] 5 3.7 17.2
答案 0 :(得分:4)
我们可以使用parse_number
readr
readr::parse_number(a)
#[1] 5.0 3.7 17.0
基于OP的新例子
library(stringr)
sapply(str_extract_all(a, "[0-9]+\\s+(years|months)"), function(x) {
x1 <- readr::parse_number(x)
head(if(length(x1)==2) x1 + round(x1[2]/12, 1) else x1, 1)})
#[1] 5.0 7.0 17.2
如果我们不需要担心在几个月内除以12,则另一种选择是
as.numeric(sapply(regmatches(a, gregexpr('[0-9]+', a)), paste, collapse="."))
#[1] 5.0 3.7 17.2
答案 1 :(得分:3)
这是一个班轮,
Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified