[R] Split rownames into factors

Christopher Bare cbare at systemsbiology.org
Tue Jul 28 01:07:36 CEST 2009


Hi,

I'm not an R expert, but I thought I'd give your question a shot anyway.

First, it looks like you're starting with a matrix, rather than a
list. Let's hope I guessed that right:

> m = matrix(c(324, 65, 543, 23, 54, 8765, 213, 43, 65))
> rownames(m) = c('X1Jan08', 'X1Jun08', 'X1Dec08', 'X2Jan08', 'X2Jun08', 'X2Dec08', 'X3Jan08', 'X3Jun08', 'X3Dec08')
> m
       [,1]
X1Jan08  324
X1Jun08   65
X1Dec08  543
X2Jan08   23
X2Jun08   54
X2Dec08 8765
X3Jan08  213
X3Jun08   43
X3Dec08   65

You can pull the individual values out of the compound thing in
row names using regular expressions like so:

> gsub('([A-Z]\\d+)([A-Za-z]+)(\\d+)', '\\2 \\3', rownames(m), perl=T)

With that, we can make a data.frame:

> df = data.frame(Value=m[,1], Date=gsub('([A-Z]\\d+)([A-Za-z]+)(\\d+)', '\\2 \\3', rownames(m), perl=T), Group=gsub('([A-Z]\\d+)([A-Za-z]+)(\\d+)', '\\1', rownames(m), perl=T))

The old compound row names hold over from the matrix, but we can cure
that easily enough:

> rownames(df) = NULL

> df
 Value   Date Group
1   324 Jan 08    X1
2    65 Jun 08    X1
3   543 Dec 08    X1
4    23 Jan 08    X2
5    54 Jun 08    X2
6  8765 Dec 08    X2
7   213 Jan 08    X3
8    43 Jun 08    X3
9    65 Dec 08    X3

Both Date and Group will be coerced to factors, which is probably what
you want with Group and maybe not with Data.

If I'm wrong and you really have a list, it's not that different.
First, get a vector of values:

> data.list = list(X1Jan08=324, X1Jun08=65, X1Dec08=543, X2Jan08=23, X2Jun08=54, X2Dec08=8765, X3Jan08=213, X3Jun08=43, X3Dec08=65)

> values = as.vector(data.list, mode="integer")

The rest is very similar to what's above. I hope this helps,

-chris

On Mon, Jul 27, 2009 at 3:10 PM, jimdare<jamesdare26 at gmail.com> wrote:
>
> Hi Guys,
>
> I was wondering how you would go about solving the following problem:
>
> I have a list where the grouping information is in the row names.
>
> Rowname [,1]
>
> X1Jan08  324
> X1Jun08  65
> X1Dec08  543
> X2Jan08  23
> X2Jun08  54
> X2Dec08  8765
> X3Jan08  213
> X3Jun08  43
> X3Dec08  65
>
> How can I create the following dataframe:
>       Value    Date    Group
> [1,]  324      Jan 08    X1
> [2,]  65       Jun 08    X1
> [3,]  543     Dec 08    X1
>  etc.................
>
> Thanks for your help!
> James
>
> --
> View this message in context: http://www.nabble.com/Split-rownames-into-factors-tp24689181p24689181.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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