我的data.frame Analysis
是105,000个3个变量。请考虑以下缩略示例,其中Height
FlowerColours
记录在Time
之上:
Height <- c(10, 12, 11, 12, 16, 17, 16, 16, 9, 9, 10, 12)
FlowerColours <- c("Black", "Black", "Black", "Black", "Red", "Red",
"Red", "Red", "Yellow", "Yellow", "Yellow", "Yellow")
Time <- c(1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2)
Analysis <- data.frame(Height, FlowerColours, Time, stringsAsFactors=FALSE)
我希望添加一个新列Analysis$Paddock
,每隔Paddock
每隔FlowerColours
添加一个不同的值,但我不确定该怎么做?例如:
Analysis$Paddock <- c(1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2)
这似乎相当简单,但我一直无法找到类似的例子......并对自己变得非常沮丧!谢谢。
答案 0 :(得分:0)
尝试
Analysis$Paddock <- with(Analysis, ave(seq_along(FlowerColours),
FlowerColours,FUN=function(x) as.numeric(gl(length(x),
2, length(x)))))
Analysis$Paddock
#[1] 1 1 2 2 1 1 2 2 1 1 2 2
或者
library(data.table)
setDT(Analysis)[, Paddock:= cumsum(c(TRUE, diff(Time) <0)), by = FlowerColours]
或者可以用cumsum(c(TRUE, diff(Time) <0))
ceiling(seq_len(.N)/2)