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.
Can I ask questions from a course I am taking here?
General questions are always welcome!
Please do ask general questions about things like:
How to use R
How to use the RStudio IDE or RStudio Cloud
How to work with tidyverse packages
Where to find resources to help you learn or solve problems
Specific questions can be OK, if you follow these rules:
Never copy-paste instructions from an assignment (even for online courses).
Explicitly mention the course you are taking and use the #homework tag.
Ask your question as a reproducible example (reprex) , preferably prepared for posting using the reprex package .
Want to maximize your chances of getting the help you need? Keep readin…
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
system
Closed
March 7, 2022, 1:19pm
3
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.