Re: [R] add one variable to a data frame

2018-05-11 Thread MacQueen, Don
Of course... Thanks -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 Lab cell 925-724-7509 On 5/11/18, 3:50 PM, "R-help on behalf of peter dalgaard" wrote: Um, maybe just dat1$C <- match(dat1$B, unique(dat1$B))

Re: [R] add one variable to a data frame

2018-05-11 Thread peter dalgaard
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 -pd > On 11 May 2018, at 21:30 , MacQueen, Don wrote: > > dat1$C <- seq(length(unique(dat1$B)))[ match( dat1$B, uniqu

Re: [R] add one variable to a data frame

2018-05-11 Thread MacQueen, Don
Interesting comments, and they serve as a reminder that so much depends on: -- what is known or can be assumed about the structure of the incoming data -- what will be done with the data next (and is this a one-time effort, or will it be repeated multiple times with similarly-structured but di

Re: [R] add one variable to a data frame

2018-05-11 Thread Ding, Yuan Chun
and then got answer after coming back from lunch. I am a beginner in R, so question might be trivial. Ding From: Bert Gunter [mailto:bgunter.4...@gmail.com] Sent: Friday, May 11, 2018 1:37 PM To: Sarah Goslee Cc: Ding, Yuan Chun; r-help mailing list Subject: Re: [R] add one variable to a data

Re: [R] add one variable to a data frame

2018-05-11 Thread Ding, Yuan Chun
-Original Message- From: MacQueen, Don [mailto:macque...@llnl.gov] Sent: Friday, May 11, 2018 12:30 PM To: Ding, Yuan Chun; Sarah Goslee Cc: r-help mailing list Subject: Re: [R] add one variable to a data frame Sarah's solutions are good, and here's another, even more basic: tmp1 &l

Re: [R] add one variable to a data frame

2018-05-11 Thread Bert Gunter
Sarah et. al.: As a matter of aesthetics (i.e. my personal ocd-ness) I prefer using the public API of an object, i.e. *not* to makes use of the representation of a factor as essentially an integer vector with labels, but rather to use its documented behavior. (Feel free to ignore this remark!) An

Re: [R] add one variable to a data frame

2018-05-11 Thread MacQueen, Don
1, 2018 11:40 AM To: Ding, Yuan Chun Cc: r-help mailing list Subject: Re: [R] add one variable to a data frame [Attention: This email came from an external source. Do not open attachments or click on links from unknown senders or unexpected emails.]

Re: [R] add one variable to a data frame

2018-05-11 Thread Ding, Yuan Chun
Hi Sarah, Thank you so much!! I got your good ideas. Ding -Original Message- From: Sarah Goslee [mailto:sarah.gos...@gmail.com] Sent: Friday, May 11, 2018 11:40 AM To: Ding, Yuan Chun Cc: r-help mailing list Subject: Re: [R] add one variable to a data frame [Attention: This email came

Re: [R] add one variable to a data frame

2018-05-11 Thread Sarah Goslee
Hi, Here's one way to approach it, using the coercion of factor to numeric. Note that I changed your data.frame() statement to avoid coercing strings to factors, just to make it simpler to set the levels. dat1 <-data.frame(N=seq(1, 12,1), B=c("29_log","29_log", "29_log", "27_cat", "27_cat", "1_l

[R] add one variable to a data frame

2018-05-11 Thread Ding, Yuan Chun
Hi All, I have a data frame dat1: dat1 <-data.frame(N=seq(1, 12,1), B=c("29_log","29_log", "29_log", "27_cat", "27_cat", "1_log", "1_log", "1_log", "1_log", "1_log",