Splitting a column in an R dataframe -



Splitting a column in an R dataframe -

i have column of info in r info frame has values such as:

blue-#105 green-#8845 yellow-#5454 blue-#999

i want remove lastly number part (starting @ -#) blue-#999 , blue-#105 consider same thing when plotting. how accomplish this?

use regular expressions:

> df <- data.frame(col=c("blue-#105", "green-#8845", "blue-#999")) > df col 1 blue-#105 2 green-#8845 3 blue-#999 > df$col <- gsub("-\\#.*", "", df$col) > df col 1 bluish 2 greenish 3 bluish >

here strings starting -# (where comment char # needs escaped) , followed whatever --- .* in regular look lingo: char (the dot) repeated many times fits (the star) --- replaced empty string, or in other words, removed.

r

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -