Skip to content
Prev 380004 / 398500 Next

Help with a third ggplot error

Thanks a lot Boris, I tried out your worked out?solution and it works just perfectly fine. No doubt I need a lot of practice with regexr and the pattern?stated by you - I will do that now. Thanks a lot for pointing me in the right direction. Appreciate it a lot.
 Sam.?
On Monday, 17 June 2019, 2:39:14 pm GMT+5:30, Boris Steipe <boris.steipe at utoronto.ca> wrote:
(Technically you are now thread-hijacking. But here goes:)


mydf <- data.frame(V11 = c("DD Pack0.002",
? ? ? ? ? ? ? ? ? ? ? ? ? "FTA English News0.003",
? ? ? ? ? ? ? ? ? ? ? ? ? "FTA Complimentary0.004"),
? ? ? ? ? ? ? ? ? stringsAsFactors = FALSE)

# regex matching start-of-string(letters or blanks)(numbers, a decimal
# point, more numbers)end-of-string: "^([a-zA-Z ]+)([0-9]+\\.[0-9]+)$"

# first check that all elements are matched by the regex. If not, an assumption
# of how the strings are patterned is not true ... 
all(grepl("^([a-zA-Z ]+)([0-9]+\\.[0-9]+)$", V11))? # must be true!

mydf$`Channel name` <- gsub("^([a-zA-Z ]+)([0-9]+\\.[0-9]+)$", "\\1", mydf$V11)
mydf$Price? ? ? ? ? <- gsub("^([a-zA-Z ]+)([0-9]+\\.[0-9]+)$", "\\2", mydf$V11)

mydf
#? ? ? ? ? ? ? ? ? ? ? V11? ? ? Channel name Price
# 1? ? ? ? ? DD Pack0.002? ? ? ? ? DD Pack 0.002
# 2? FTA English News0.003? FTA English News 0.003
# 3 FTA Complimentary0.004 FTA Complimentary 0.004


Note this _will_ give wrong results if channel names like "ABC4" exist.


B.