[R] add one variable to a data frame

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Sat May 12 00:50:22 CEST 2018

Um, maybe just

dat1$C <- match(dat1$B, unique(dat1$B))

Indexing 1:k with numbers between 1 and k is a bit of a no-op...

AFAICT, this even works without stringsAsFactors=FALSE


> On 11 May 2018, at 21:30 , MacQueen, Don <macqueen1 using llnl.gov> wrote:
> dat1$C <- seq(length(unique(dat1$B)))[ match( dat1$B, unique(dat1$B) )]

Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com

More information about the R-help mailing list