-1
我之前問過類似的問題,但似乎我還不夠清楚。我有一個32列的data.frame。我想滿足這個特定標準R:根據上面一行中的值更改連續值
df$resting == "toolong".
在從迪文我曾嘗試使用下面的代碼完成了這個建議,每行下面創建重複行。
df[ unlist(mapply(rep, rownames(df), 1+(df$resting=="toolong"))) , ]
這工作得很好,但現在我需要改變一些「父」行和新創建重複行的值。變量行動和時間父行需要被設置爲
action <- "for"
time <- 60
重複的行需要有變量行動和時間設置爲
action <- "l" # which is what it is already so this can be ignored for now
time <- "parent row time" - 60 # I am unsure how to code this.
這裏是一個顯示數據結構的示例data.frame(原始數據庫中有更多列)。
id <- c(1,1,1,1,2,2,2,3,3,3)
resting <- c("f","toolong","t","f","f","toolong","t","f","toolong","t")
time <- c(23,145,34,16,17,134,67,89,123,12)
act <- c("f","l","f","d","d","l","f","d","l","d")
df <- data.frame(id, resting, time, act)
這裏是最後的DF應該是什麼樣子。
id resting time act
1 1 f 23 f
2 1 toolong 60 for
2.1 1 toolong 85 l
3 1 t 34 f
4 1 f 16 d
5 2 f 17 d
6 2 toolong 60 for
6.1 2 toolong 74 l
7 2 t 67 f
8 3 f 89 d
9 3 toolong 60 for
9.1 3 toolong 63 l
10 3 t 12 d
謝謝,蒂姆
我解釋瞭如何響應在評論你的要求做。 –