[R] creating data.frame from interaction of factors

Marc Schwartz marc_schwartz at comcast.net
Thu Feb 15 18:07:08 CET 2007


On Thu, 2007-02-15 at 08:34 -0800, jiho.han wrote:
> Hello, R experts-
> 
> If I create a new factor, say Z, by combining other factors, say X and Y, is
> there ways to create a data.frame from the resulting factor Z? For example,
> suppose I enter the following:
> 
> X = 1:3
> Y = letters[1:3]
> Z = interaction( X, Y, sep=":")
> 
> Then, I want to get a data.frame that look like 
> 
> 1 a
> 2 b
> 3 c
> 
> Help me--- Thanks

> as.data.frame(do.call("rbind", strsplit(as.character(Z), ":")))
  V1 V2
1  1  a
2  2  b
3  3  c


See ?strsplit, ?do.call and ?rbind

HTH,

Marc Schwartz



More information about the R-help mailing list