[R] divide column in a dataframe based on a character
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Tue Oct 26 06:23:39 CEST 2010
You are nearly there.
____
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)
ddd <- within(ddd, {
group <- as.character(group)
tmp <- do.call(rbind, strsplit(group, "_"))
group_b <- tmp[,2]
group_a <- tmp[,1]
rm(tmp, group)
})
____
> ddd
x y fac char group_a group_b
1 1 1 B a aa bb
2 1 2 B b bb cc
3 1 3 A c cc dd
4 1 4 A d dd ee
5 1 5 A e ee ff
6 1 6 C f ff gg
7 1 7 A g gg hh
8 1 8 B h ii jj
9 1 9 C i jj kk
10 1 10 B j kk ll
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Daisy Englert Duursma
Sent: Tuesday, 26 October 2010 1:57 PM
To: r-help at r-project.org
Subject: [R] divide column in a dataframe based on a character
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
______________________________________________
R-help at 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.
More information about the R-help
mailing list