R character factor to numeric vector
I read in a csv file with "3:29" in one of the fields (without the quotation marks). This comes up as a factor. How can I convert this to a numeric vector e.g. c(3:29)? I tried as.vector() but this gives a string vector "3,4,5,6...29" (with the quotation marks, still character class).
EDIT Answer needs to be applicable to more general form, for exam开发者_如何学JAVAple, the column could contain 3:6,7,9:11, which needs to be converted to the equivalent c(3:6,7,9:11).
You can do:
> eval(parse(text='3:29'))
[1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
[26] 28 29
Split the string on :
and convert to a vector of numerics and generate the call to seq()
by hand:
> vars <- as.numeric(strsplit("3:29", ":")[[1]])
> seq(from = vars[1], to = vars[2], by = 1)
[1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
[26] 28 29
or slightly more elegantly by getting R to build the call to `:()`
directly:
> do.call(`:`, as.list(as.numeric(strsplit("3:29", ":")[[1]])))
[1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
[26] 28 29
[Updated in light of Edit to original Q]
In the spirit of:
> require(fortunes)
> fortune(106)
If the answer is parse() you should usually rethink the question.
-- Thomas Lumley
R-help (February 2005)
this is as close as I can get without using parse()
:
unlist(lapply(strsplit(strsplit(txt, ",")[[1]], ":"),
function(x) {
x <- as.numeric(x)
if(length(x) == 2) {
seq(x[1], x[2], by = 1) ## `:`(x[1], x[2])
} else {
x[1]
}
}))
yielding:
[1] 3 4 5 6 7 9 10 11
...but it makes me thing this might be one of those times when using parse()
might make sense ;-)
精彩评论