kyle.landskroner at actelion.com
2011-Mar-11 12:28 UTC
[R] insertion of a row between individuals
Can someone help with a fairly simple task? I have a data set where I would like to insert a 0 time event between individuals: what I have: VAR DATE TIME CONC COVAR 1 NOV2 0.25 10 group1 1 NOV2 0.5 20 group1 1 NOV2 1 5 group1 1 NOV2 2 1 group1 1 NOV2 3 0.1 group1 2 NOV2 0.25 10 group2 2 NOV2 0.5 20 group2 2 NOV2 1 5 group2 2 NOV2 2 0.5 group2 2 NOV2 3 0.01 group2 . . 1000 NOV2 3 10 group2 ...to what I would like to have: VAR DATE TIME CONC COVAR 1 NOV2 0 0 group1 1 NOV2 0.25 10 group1 1 NOV2 0.5 20 group1 1 NOV2 1 5 group1 1 NOV2 2 1 group1 1 NOV2 3 0.1 group1 2 NOV2 0 0 group2 2 NOV2 0.25 10 group2 2 NOV2 0.5 20 group2 2 NOV2 1 5 group2 2 NOV2 2 0.5 group2 2 NOV2 3 0.01 group2 . . 1000 NOV2 0 0 group2 1000 NOV2 3 10 group2 Any help would be appreciated. Thanks, Kyle The information of this email and in any file transmitted with it is strictly confidential and may be legally privileged. It is intended solely for the addressee. If you are not the intended recipient, any copying, distribution or any other use of this email is prohibited and may be unlawful. In such case, you should please notify the sender immediately and destroy this email. The content of this email is not legally binding unless confirmed by letter. Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of the sender's company. For further information about Actelion please see our website at http://www.actelion.com [[alternative HTML version deleted]]
Try this: newX <- rbind(unique(transform(x, TIME = 0, CONC = 0)), x) newX[order(newX$VAR),] On Fri, Mar 11, 2011 at 9:28 AM, <kyle.landskroner@actelion.com> wrote:> Can someone help with a fairly simple task? > I have a data set where I would like to insert a 0 time event between > individuals: > > what I have: > > VAR DATE TIME CONC COVAR > 1 NOV2 0.25 10 group1 > 1 NOV2 0.5 20 group1 > 1 NOV2 1 5 group1 > 1 NOV2 2 1 group1 > 1 NOV2 3 0.1 group1 > 2 NOV2 0.25 10 group2 > 2 NOV2 0.5 20 group2 > 2 NOV2 1 5 group2 > 2 NOV2 2 0.5 group2 > 2 NOV2 3 0.01 group2 > . > . > 1000 NOV2 3 10 group2 > > > ...to what I would like to have: > VAR DATE TIME CONC COVAR > 1 NOV2 0 0 group1 > 1 NOV2 0.25 10 group1 > 1 NOV2 0.5 20 group1 > 1 NOV2 1 5 group1 > 1 NOV2 2 1 group1 > 1 NOV2 3 0.1 group1 > 2 NOV2 0 0 group2 > 2 NOV2 0.25 10 group2 > 2 NOV2 0.5 20 group2 > 2 NOV2 1 5 group2 > 2 NOV2 2 0.5 group2 > 2 NOV2 3 0.01 group2 > . > . > 1000 NOV2 0 0 group2 > 1000 NOV2 3 10 group2 > > Any help would be appreciated. > Thanks, > Kyle > > > The information of this email and in any file transmitted with it is > strictly confidential and may be legally privileged. > It is intended solely for the addressee. If you are not the intended > recipient, any copying, distribution or any other use of this email is > prohibited and may be unlawful. In such case, you should please notify the > sender immediately and destroy this email. > The content of this email is not legally binding unless confirmed by > letter. > Any views expressed in this message are those of the individual sender, > except where the message states otherwise and the sender is authorised to > state them to be the views of the sender's company. For further information > about Actelion please see our website at http://www.actelion.com > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Hi: df <- read.table(textConnection(" VAR DATE TIME CONC COVAR 1 NOV2 0.25 10 group1 1 NOV2 0.5 20 group1 1 NOV2 1 5 group1 1 NOV2 2 1 group1 1 NOV2 3 0.1 group1 2 NOV2 0.25 10 group2 2 NOV2 0.5 20 group2 2 NOV2 1 5 group2 2 NOV2 2 0.5 group2 2 NOV2 3 0.01 group2"), header = TRUE) u <- df[which(!duplicated(df$VAR)), ] # copy unique VARs to u u$TIME <- u$CONC <- 0 # set TIME and CONC to 0 df <- rbind(df, u) # row concatenate df <- df[order(df$VAR, df$DATE, df$TIME), ] # sort VAR DATE TIME CONC COVAR 11 1 NOV2 0.00 0.00 group1 1 1 NOV2 0.25 10.00 group1 2 1 NOV2 0.50 20.00 group1 3 1 NOV2 1.00 5.00 group1 4 1 NOV2 2.00 1.00 group1 5 1 NOV2 3.00 0.10 group1 61 2 NOV2 0.00 0.00 group2 6 2 NOV2 0.25 10.00 group2 7 2 NOV2 0.50 20.00 group2 8 2 NOV2 1.00 5.00 group2 9 2 NOV2 2.00 0.50 group2 10 2 NOV2 3.00 0.01 group2 HTH, Dennis On Fri, Mar 11, 2011 at 4:28 AM, <kyle.landskroner@actelion.com> wrote:> Can someone help with a fairly simple task? > I have a data set where I would like to insert a 0 time event between > individuals: > > what I have: > > VAR DATE TIME CONC COVAR > 1 NOV2 0.25 10 group1 > 1 NOV2 0.5 20 group1 > 1 NOV2 1 5 group1 > 1 NOV2 2 1 group1 > 1 NOV2 3 0.1 group1 > 2 NOV2 0.25 10 group2 > 2 NOV2 0.5 20 group2 > 2 NOV2 1 5 group2 > 2 NOV2 2 0.5 group2 > 2 NOV2 3 0.01 group2 > . > . > 1000 NOV2 3 10 group2 > > > ...to what I would like to have: > VAR DATE TIME CONC COVAR > 1 NOV2 0 0 group1 > 1 NOV2 0.25 10 group1 > 1 NOV2 0.5 20 group1 > 1 NOV2 1 5 group1 > 1 NOV2 2 1 group1 > 1 NOV2 3 0.1 group1 > 2 NOV2 0 0 group2 > 2 NOV2 0.25 10 group2 > 2 NOV2 0.5 20 group2 > 2 NOV2 1 5 group2 > 2 NOV2 2 0.5 group2 > 2 NOV2 3 0.01 group2 > . > . > 1000 NOV2 0 0 group2 > 1000 NOV2 3 10 group2 > > Any help would be appreciated. > Thanks, > Kyle > > > The information of this email and in any file transmitted with it is > strictly confidential and may be legally privileged. > It is intended solely for the addressee. If you are not the intended > recipient, any copying, distribution or any other use of this email is > prohibited and may be unlawful. In such case, you should please notify the > sender immediately and destroy this email. > The content of this email is not legally binding unless confirmed by > letter. > Any views expressed in this message are those of the individual sender, > except where the message states otherwise and the sender is authorised to > state them to be the views of the sender's company. For further information > about Actelion please see our website at http://www.actelion.com > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]