[R] divide column in a dataframe based on a character

Daisy Englert Duursma daisy.duursma at gmail.com
Tue Oct 26 05:56:51 CEST 2010


Hello,

If I have a dataframe:

example(data.frame)
zz<-c("aa_bb","bb_cc","cc_dd","dd_ee","ee_ff","ff_gg","gg_hh","ii_jj","jj_kk","kk_ll")
ddd <- cbind(dd, group = zz)

and I want to divide the column named group by the "_", how would I do this?

so instead of the first row being
 x   y  fac char  group
 1  1   C    a     aa_bb

it should be:
 x  y fac  char group_a    group_b
 1  1   C    a      aa             bb



I know for a vector I can:
x1 <- c("a_b","b_c","c_d")
do.call("rbind",strsplit(x1, "_"))

but I am not sure how this relates to my data.frame

Thanks,
Daisy


-- 
Daisy Englert Duursma

Room E8C156
Dept. Biological Sciences
Macquarie University  NSW  2109
Australia



More information about the R-help mailing list