Dear useRs,
I am sure this is a fairly simple problem, but I just cannot get my head around 
it.


I have a dataframe which contains several factor variables. I can use table() 
to tell me how many different combinations there are of these variables. What I 
should like to do is to add a column to my original dataframe which labels each 
row according to the unique combination of factors.


E.g. in the simple example below I create a dataframe 'df' with 3 columns, the 
values of which take 0 or 1. I can then classify each row in the table and I 
find that I have 4 unique combinations of factors. I would now like to add a 
fourth column to df which labels each row according to whether it was unique 
combination 1,2,3 or 4:

x1=c(rep(0:1,6))
x2=c(rep(c(1,1,0,0)6))
x3=c(rep(1,6),rep(0,6))
df=data.frame(x1,x2,x3)
tabledf=as.data.frame(with(df, table(x1,x2,x3)))
res=c(3,4,3,4,3,4,1,2,1,2,1,2)
desired=data.frame(x1,x2,x3,res)
df
tabledf
desired


I realise that this is probably quite simple to do, I am just struggling to get 
my head around it! Help much appreciated in advance.

Cheers,

Simon

--------------------------------
Simon O'Hanlon BSc, MSc
Department of Infectious Disease Epidemiology
Imperial College London
St. Mary's Hospital
London
W2 1PG


______________________________________________
R-help@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.

Reply via email to