创建特定列的实体

时间:2014-03-23 18:04:11

标签: r

我有一个大型数据集" newdata"像这样:

TRT       TIME
1    4/28/2012 9:20
1    4/28/2012 9:20
1    4/28/2012 9:20
1    4/28/2012 9:20
4    4/28/2012 9:20
4    5/7/2012 23:10
4    5/7/2012 23:10
5    5/7/2012 23:10
5    5/7/2012 23:10
5    6/9/2012 22:00
5    6/9/2012 22:00
...

我希望创建一个新专栏" AMT"这样每当" TIME"改变,给出" TRT"到新列,否则给出0.预期的数据集应该是这样的:

TRT       TIME         AMT      
1    4/28/2012 9:20     1
1    4/28/2012 9:20     0
1    4/28/2012 9:20     0
1    4/28/2012 9:20     0
4    4/28/2012 9:20     0
4    5/7/2012 23:10     4
4    5/7/2012 23:10     0
5    5/7/2012 23:10     0
5    5/7/2012 23:10     0
5    6/9/2012 22:00     5
5    6/9/2012 22:00     0

有人能说明如何实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

DF <- read.table(text="  TRT,  TIME
1,    4/28/2012 9:20
1,    4/28/2012 9:20
1,    4/28/2012 9:20
1,    4/28/2012 9:20
4,    4/28/2012 9:20
4,    5/7/2012 23:10
4,    5/7/2012 23:10
5,    5/7/2012 23:10
5,    5/7/2012 23:10
5,    6/9/2012 22:00
5,    6/9/2012 22:00", header=TRUE, sep=",")

DF$TIME <- as.POSIXct(DF$TIME, format="%m/%d/%Y %H:%M", tz="UTC")
DF$AMT <- DF$TRT * c(TRUE, diff(DF$TIME) != 0)
#    TRT                TIME AMT
# 1    1 2012-04-28 09:20:00   1
# 2    1 2012-04-28 09:20:00   0
# 3    1 2012-04-28 09:20:00   0
# 4    1 2012-04-28 09:20:00   0
# 5    4 2012-04-28 09:20:00   0
# 6    4 2012-05-07 23:10:00   4
# 7    4 2012-05-07 23:10:00   0
# 8    5 2012-05-07 23:10:00   0
# 9    5 2012-05-07 23:10:00   0
# 10   5 2012-06-09 22:00:00   5
# 11   5 2012-06-09 22:00:00   0

答案 1 :(得分:0)

这是循环这种方式的一种丑陋方式,但它有效:

# define vector with number 1
AMT <- c(1)

for(i in 1:nrow(time)){

   # ifelse statement determining it time entry is equal to the previous entry
   test <- ifelse(time$TIME[i] != time$TIME[i - 1], time$TRT[i], 0)

   # append test to AMT vector
   AMT <- c(AMT, test)
}

# append AMT vector to time data frame
time$AMT <- AMT