On 23.01.2017 13:29, Abhinaba Roy wrote:
Hi,
How do I extract the first number after '*' in a vector?
The vector is given below
dput(out[1:10])
c(" 1 X[0,SMITH] * 0 0 1 ",
" 2 X[0,JOHNSON] * 0 0 1 ",
" 3 X[0,WILLIAMS]", " * 1 0
1 ",
" 4 X[0,JONES] * 0 0 1 ",
" 5 X[0,BROWN] * 0 0 1 ",
" 6 X[0,DAVIS] * 0 0 1 ",
" 7 X[0,MILLER] * 0 0 1 ",
" 8 X[0,WILSON] * 0 0 1 ",
" 9 X[0,MOORE] * 0 0 1 "
)
I want a vector with the first number after the asterisk.
So the output would give me, a vector (0,0,1,0,0,0,0,0,0,0)
How can I do it in R?
You know that your vector (called x below) contains an element without
an asterisk?
If that happened by accident, use
gsub(".+\\* *([[:digit:]]+).*", "\\1", x)
and if it could happen to have elements without an asterisk or number
that follows, you can set these results to NA in a seperate step.
Best,
Uwe Ligges
Best,
Abhinaba
[[alternative HTML version deleted]]
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.