我对 R 很陌生,我想知道如何从这种类型的字符串中提取距离并输入:“刚刚用 @RunKeeper 完成了 0.56 英里的步行”。所以我想将“0.56”、“mi”和“walk”存储到三个单独的变量中。我该怎么做?
谢谢!杰罗恩。
我试过这个:
can.be <- function(object, class="numeric")
suppressWarnings(!is.na(as(object, class)))
str.vec <- c(text)
str.vec <- strsplit(str.vec, " ")
strsplit(str.vec, " ") 中的错误:非字符参数
pos <- sapply(str.vec, function(x) which(sapply(x, can.be)))
[[1]]
0.56 4
[[2]] 命名整数(0)
... mapply( [[
, str.vec, pos) mapply( [[
, str.vec, pos+1) mapply( [[
, str.vec, pos+2)
但现在我得到这个错误:
> mapply(`[[`, str.vec, pos)
Error in .Primitive("[[")(dots[[1L]][[2L]], dots[[2L]][[2L]]) :
attempt to select less than one element
> mapply(`[[`, str.vec, pos+1)
Error in pos + 1 : non-numeric argument to binary operator
> mapply(`[[`, str.vec, pos+2)
Error in pos + 2 : non-numeric argument to binary operator
示例数据(文本):
Just completed a 0.56 mi walk with @RunKeeper. Check it out! http://t.co/lCyzzFeSwq #RunKeeper
Just completed a run in 0:00 with @RunKeeper. Check it out! http://t.co/dJB9DBwF4o #RunKeeper
Just completed a 1.83 km run with @RunKeeper. Check it out! http://t.co/f0S2aKnWXz #RunKeeper
Just completed a 6.03 km run - Gettin' it done! http://t.co/uQ7rBn6M #RunKeeper
Just completed a 1.81 mi walk with @RunKeeper. Check it out! http://t.co/R70fvkLDES #RunKeeper