Splitting a column in an R dataframe -


I have a column of data in the R data frame, which contains the value such as:

   

I want to remove the last part (starting from - #) so that blue- # 999 and Blue- # 105 While thinking about the same thing, how can I do this?

Use regular expression:

  & gt; DF & lt; - data.frame (col = c ("blue- # 105", "green-# 8845", "blue-9999"))) DF Colonel 1 Blue- # 105 2 Green- # 8845 3 Blue- # 99 9 & gt; DF $ COL & lt; - gsub ("- \\ #. *", "", DF $ call) & gt; DF Colonel 1 Blue2 Green3 Blue & gt;  

Here we say that all the strings starting with - # (where the comment char * code> # needs to be avoided) and After that whatever --- which is . * is in regular expression: any four (duplicate) is repeated repeatedly because it fits (stars) - will be replaced by empty string, or in other words, removed.


Comments