converting to numeric

Hi!

I need to convert the WLTP and stop column to numeric, but struggling since there are more than numbers there. Any suggestions how I will do it?

Code so far, need to plot this for an assignment.

library(tidyverse)
library(rvest)
library(XML)
library(ggrepel)
library(ggeasy)

cars <- read_html("https://www.motor.no/aktuelt/motors-store-vintertest-av-rekkevidde-pa-elbiler/217132")

tables <- cars %>% html_table(fill=TRUE)

cartable <-  tables[[1]]

cartable <- cartable[-1,]

colnames(cartable) <- c("Modell (temp. varierte fra 0° til -10°)", "WLTP", "stop", "Avvik")

# Lager plottet 
plot1 <- cartable %>%
  ggplot(aes(x = `WLTP-tall`, y = STOPP)) +
  geom_point() +
  scale_y_continuous(breaks = seq(from=200, to=600, by=100)) +
  scale_x_continuous(breaks = seq(from=200, to=600, by=100)) +
  labs(x= "WLTP",
       y= "STOPP") +
  theme_gray()
plot1

Hi,

As this is for an assignment, we can give you tips and pointers but no solutions.

So my tip is to use some simple regular expressions to extract the numbers you need from the columns and convert them to numeric values.

Here is an example using regex that's not extracting numbers, but text from a string containing other symbols

library(tidyverse)

myData = data.frame(
  col1 = c("#test!", "#hello@", "text()")
)

myData$col1 = str_extract(myData$col1, "\\w+")
myData
#>    col1
#> 1  test
#> 2 hello
#> 3  text

Created on 2022-02-14 by the reprex package (v2.0.1)

You can use similar regular expressions to extract the numbers you need. Here is a great way to get started:

Just one more tip: When writing regex in string format, you need to add another \ to escape \ used in regex patterns: so the \w used in the example is \\w in the string.

Good luck

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.