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
Gabor Grothendieck
2007-Feb-07 02:55 UTC
[R] How to reorder rows in dataframe by text flag
Try this:
DF <- data.frame(
col1 = factor(c("31*", "0", "102*",
"71*", "31", "66", "47")),
col2 = factor(c("66", "0*", "66",
"80", "2*", "31*", "38"))
)
replace(DF, TRUE, as.numeric(sub("*", "", as.matrix(DF),
fixed = TRUE)))
On 2/6/07, Dale Steele <dale.w.steele at gmail.com>
wrote:> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>