Dear R users: I want to convert some character vectors into numeric vectors.> head(price)[1] "15450 EUR" "7900 EUR" "13800 EUR" "3990 EUR" "4500 EUR" [6] "4250 EUR">head(mileage)[1] "21000?km" "119000?km" "36600?km" "92000?km" "140200?km" [6] "90000?km" in the first example I can use: price <- sub(" EUR", "", price) to get "15450" "7900" "13800" "3990" "4500" "4250" but in the second example it doesn't work with mileage <- sub(" km, "", mileage) I found a solution online: sub("[[:blank:]]+$", "", x) so I can solve it with two steps (eliminate first "km" then blanks) but I'm wondering where the difference in my two examples is why does it work with the first vector and why not with the second -- View this message in context: http://r.789695.n4.nabble.com/problem-with-sub-tp4632946.html Sent from the R help mailing list archive at Nabble.com.
Hello, Works with me. x <- c("21000 km", "119000 km", "36600 km", "92000 km", "140200 km", "90000 km") sub("[[:blank:]]*km", "", x) as.numeric(sub("km", "", x)) # for numeric, don't worry with blanks Hope this helps, Rui Barradas Em 10-06-2012 19:44, t_o_b_y escreveu:> Dear R users: > > > > I want to convert some character vectors into numeric vectors. > >> head(price) > [1] "15450 EUR" "7900 EUR" "13800 EUR" "3990 EUR" "4500 EUR" > [6] "4250 EUR" > >> head(mileage) > [1] "21000 km" "119000 km" "36600 km" "92000 km" "140200 km" > [6] "90000 km" > > in the first example I can use: > > price <- sub(" EUR", "", price) > > to get > > "15450" "7900" "13800" "3990" "4500" "4250" > > but in the second example it doesn't work with > > mileage <- sub(" km, "", mileage) > > > I found a solution online: > sub("[[:blank:]]+$", "", x) > > so I can solve it with two steps (eliminate first "km" then blanks) > > but I'm wondering where the difference in my two examples is > why does it work with the first vector and why not with the second > > -- > View this message in context: http://r.789695.n4.nabble.com/problem-with-sub-tp4632946.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
On Sun, Jun 10, 2012 at 11:44:33AM -0700, t_o_b_y wrote:> Dear R users: > > > > I want to convert some character vectors into numeric vectors. > > > head(price) > [1] "15450 EUR" "7900 EUR" "13800 EUR" "3990 EUR" "4500 EUR" > [6] "4250 EUR" > > >head(mileage) > [1] "21000?km" "119000?km" "36600?km" "92000?km" "140200?km" > [6] "90000?km" > > in the first example I can use: > > price <- sub(" EUR", "", price) > > to get > > "15450" "7900" "13800" "3990" "4500" "4250" > > but in the second example it doesn't work with > > mileage <- sub(" km, "", mileage)Hi. The problem is that "21000?km" does not contain a space, but a no-break space. This may be checked by charToRaw("21000?km") # copied from your email [1] 32 31 30 30 30 c2 a0 6b 6d charToRaw("21000 km") # after rewriting the space to a normal space [1] 32 31 30 30 30 20 6b 6d Either replace the no-break spaces by normal spaces, or use the abbreviation "\u00a0" for no-break space. mileage <- c("21000?km", "119000?km", "36600?km") # copied from your email sub("\u00a0km", "", mileage) [1] "21000" "119000" "36600" Hope this helps. Petr Savicky.