Dale Steele
2007-Feb-07 00:40 UTC
[R] Data management problem (reorder rows in dataframe by text flag)
Given two columns of type character in a dataframe of the form: col1 col2 31* 66 0 0* 102* 66 71* 80 31 2* 66 31* 47 38* How do I generate the following dataframe? Ie. col1 contains row item with "*" and col2 contains row member without "*" col1 col2 31 66 0 0 102 66 71 80 2 31 31 66 38 47 Partial ideas thus far.... grep("*",col1,fixed=T) as.numeric(gsub("*","",col1)) Thanks. --Dale
Christian Bieli
2007-Feb-07 12:39 UTC
[R] Data management problem (reorder rows in dataframe by text flag)
how about: t.d <- data.frame(col1=c("31*","0","102*","71*","31","66","47"), col2=c("66","0*","66","80","2*","31*","38*"), stringsAsFactors = FALSE) t.x <- apply(t.d,1,function(x) x[order(unlist(x)==grep("\\*$", unlist(x),value=TRUE))]) t.d2 <- data.frame(col1=t.x[1,],col2=sub("\\*$","",t.x[2,])) greets christian -- Christian Bieli, project assistant Institute of Social and Preventive Medicine University of Basel, Switzerland Steinengraben 49 CH-4051 Basel Tel.: +41 61 270 22 12 Fax: +41 61 270 22 25 christian.bieli at unibas.ch www.ispm-unibasel.ch