On 10/25/2010 8:56 PM, Daisy Englert Duursma wrote:
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,

You've already gotten a couple of responses, but I thought I'd point out an different approach (the let-someone-else-deal-with-the-details approach). The reshape package has a colsplit function which is designed just for this sort of thing.

library("reshape")

ddd <- cbind(ddd, colsplit(ddd$group, split="_", names=c("group_a", "group_b")))

--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to