[R] Help with a third ggplot error

Boris Steipe bor|@@@te|pe @end|ng |rom utoronto@c@
Mon Jun 17 11:09:12 CEST 2019


(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.




> On 2019-06-15, at 15:40, Sam Charya via R-help <r-help using r-project.org> wrote:
> 
> Hello All,
> I need help with splitting a string. My data frame is in the following format:
>                       V11            DD Pack0.002   FTA English News0.003  FTA Complimentary0.004                 WB1.185               WION1.186         Al Jazeera0.007      Animal Planet2.368    Asianet Movies17.709      Calcutta News0.0010    Comedy Central5.90
> I read the file from a csv and set header = False, hence it is named V1. 
> The data consists of names of Channels and their prices. For example: Row 1: Name of the Channel is 'DD Pack' and the Price is 0.00.similarly for Row 5, the name of the Channel is 'WION' and the price is 1.18.
> similarly for Row 8: The name of the Channel is 'Asianet Movies' and the price is 17.70. 
> 
> My question is: How would I separate the data into 2 columns: One for the Channel name and one for the Price. 
> For example. The Heading should be for Col1: 'Channel Name' and for Col2: 'Price'The data under  'Channel Name' should be 'DD Pack' and for 'Price' should be 0.00 and so on and so forth. 
> The letters and the numeric appears together so there is no separator and I am not being able to figure this out. Kindly please help resolve this. 
> Many Thanks in advance for your help. This is my first  question ever to the community so apologies if I have made a mistake in sending it to the wrong group - kindly direct if that is the case. 
> sam. 
> ********************************************************
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using 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.



More information about the R-help mailing list