Skip to content

add identifier column by row

7 messages · Sarah Goslee, Don McKenzie, arun +1 more

#
You can use rep() to create the Date column, and data.frame() to combine it.

For your simple example,

newdata <- data.frame(dat, Date=rep(1:3, each=2))
On Tue, May 21, 2013 at 4:16 PM, Ye Lin <yelin at lbl.gov> wrote:

  
    
#
Do you want each number in "Date" to be repeated once (as in your  
example) or appear 48 times (so that you start over every 1440 rows,  
as in your request).

For the former,

rep(c(1:30),each=48)

fills the first 1440 rows.
On 21-May-13, at 1:16 PM, Ye Lin wrote:

            
Don McKenzie, Research Ecologist
Pacific Wildland Fire Sciences Lab
US Forest Service
phone: 206-732-7824

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
#
Duh -- I mean "for the latter"
On 21-May-13, at 1:25 PM, Don McKenzie wrote:

            
Don McKenzie, Research Ecologist
Pacific Wildland Fire Sciences Lab
US Forest Service
phone: 206-732-7824

Affiliate Professor
School of Environmental and Forest Sciences
University of Washington
#
May be this helps:

dat<- read.table(text="
ID????? Var
1????????? 1
2????????? 4
3??????? 6
4??????? 7
5????????? 7
6????????? 8
7??? ?? 9 ??? 
",sep="",header=TRUE)
?dat$Date<-cumsum(seq_len(nrow(dat))%%2)
?dat
#? ID Var Date
#1? 1?? 1??? 1
#2? 2?? 4??? 1
#3? 3?? 6??? 2
#4? 4?? 7??? 2
#5? 5?? 7??? 3
#6? 6?? 8??? 3
#7? 7?? 9??? 4
A.K.


----- Original Message -----
From: Ye Lin <yelin at lbl.gov>
To: R help <r-help at r-project.org>
Cc: 
Sent: Tuesday, May 21, 2013 4:16 PM
Subject: [R] add identifier column by row

I want to add identifier column (Date) to a time series data frame. I want
to name the "Date" column be from 1 to 30 every 1440 rows.

Say I have a data like this (I simply my actual data here):

$dat

ID? ? ?  Var
1? ? ? ? ? 1
2? ? ? ? ? 4
3? ? ? ?  6
4? ? ? ?  7
5? ? ? ? ? 7
6? ? ? ? ? 8

How can I add identifier column (Date) from 1 to 3 every 2 rows and have
output like this:

ID? ? ?  Var? Date
1? ? ? ? ? 1? ? ? 1
2? ? ? ? ? 4? ?  1
3? ? ? ?  6? ? ? 2
4? ? ? ?  7? ? ? 2
5? ? ? ? ? 7? ?  3
6? ? ? ? ? 8? ? ? 3

Thanks,

??? [[alternative HTML version deleted]]

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