[R] assign color to subsets

chalabi.elahe at yahoo.de chalabi.elahe at yahoo.de
Sun Apr 24 23:15:27 CEST 2016


now after this:

  
   df_both <- subset(df, grepl("t1", Command) & grepl("t2", Command))

I use factor to apply the subset to df but then the Command level becomes 0


   df_both$Command=factor(df_both$Command)

   str(df_both)
   
   $ Protocol     : Factor w/ 0 levels: 
Do you know what is the reason?
Thanks for replying



On Sunday, April 24, 2016 12:18 PM, jim holtman <jholtman at gmail.com> wrote:



'grepl' returns a logical vector; you have to use this to get your subset.  You can use:

df_tq <- subset(df, grepl("t1", Command))
df_t2 <- subset(df, grepl("t2", Command))

# if you want to also get a subset that has both, use

df_both <- subset(df, grepl("t1", Command) & grepl("t2", Command))





Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sun, Apr 24, 2016 at 2:36 PM, <chalabi.elahe at yahoo.de> wrote:

Thanks Jim,
>
>my problem is that in Command I have 2229 levels and I want to do subsets based on the names I have in Command. for example if the name has t1 or t2 in it or if it has both of them.and then I need to plot in a way that colors are names with t1,names with t2 and names with both. But now even the grepl I use for the subsets does not work correct! :(((
>
>hast1=grepl("t1", df$Command, fixed=TRUE)
>hast2=grepl("t2", df$Command, fixed=TRUE)
>
>
>On Sunday, April 24, 2016 9:16 AM, jim holtman <jholtman at gmail.com> wrote:
>
>
>
>You never did provide a reproducible example or say how you wanted to plot.  Here is a way to get a subset of t1 or t2, and you can then use it as input to ggplot:
>
>library(dplyr)
>your_subset <- df %>%
>          mutate(key = grep(".*(t1|t2).*", "\\1", Command, value = TRUE)) %>%
>          filter(!(Command %in% c('t1', 't2')))
>
>This will give you a subset with just t1/t2 and you can use 'key' as the colour option for ggplot.
>
>
>
>
>Jim Holtman
>Data Munger Guru
>
>What is the problem that you are trying to solve?
>Tell me what you want to do, not how you want to do it.
>
>On Sat, Apr 23, 2016 at 4:16 PM, ch.elahe via R-help <r-help at r-project.org> wrote:
>
>Hi
>>I have the following df and I created two subsets but I don't know how to use these subsets as the colors of my plot.
>>
>>
>>   data.frame': 36919 obs. of 162 variables
>>   $TE           :int 38,41,11,52,48,75,.....
>>   $TR           :int 100,210,548,546,.....
>>   $Command       :factor W/2229 levels "_localize_PD","_localize_tre_t2","_abdomen_t1_seq","knee_pd_t1_localize"...
>>
>>and the subsets are names in Command which has t1 or t2 letters:
>>
>>
>>
>>  hast1=grepl("t1", df$Command, fixed=TRUE)
>>  hast2=grepl("t2", df$Command, fixed=TRUE)
>>
>>
>>the colors I want in my plot are : hast1 and hast2
>>Thanks for any help.
>>Elahe
>>
>>______________________________________________
>>R-help at 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