2014-01-13 24 views
0

我有一個df,如下所示,並想通過noms(唯一id)拆分df,然後向每個組添加x行數。然後我想重組。 x對於每個組將是不同的,並且將等於將正整數增加到12的行數(換句話說,對於每個人,x = 12的最高正整數)。ddply分裂並將行添加到每個組

ddply似乎是這裏的顯而易見的選項,但我在添加行時遇到問題。我可以使用以下代碼創建一個新列

x<-ddply(df,.(noms),transform, new_time=numbers) 

但是這並不能解決爲每個人添加額外行的問題。我認爲'mutate'可能會爲我做這件事,但除了我的邏輯在這裏很糟糕,它不會添加在行上。

x<-ddply(df,.(noms),mutate, new_time=numbers+(tail(df$numbers-12))) 

是否可以使用ddply添加行?甚至分裂?任何幫助將非常感激。先謝謝你。

這裏是df和以下是所需的輸出。

df 
    noms numbers 
1 jane  -6 
2 jane  -5 
3 jane  -4 
4 jane  -3 
5 jane  -2 
6 jane  -1 
7 jane  1 
8 jane  2 
9 jane  3 
10 jane  4 
11 john  -2 
12 john  -1 
13 john  1 
14 john  2 
15 john  3 
16 john  4 
17 john  5 
18 john  6 
19 john  7 
20 john  8 
21 mary  -1 
22 mary  1 
23 mary  2 
24 mary  3 
25 mary  4 
26 mary  5 
27 mary  6 
28 mary  7 
29 mary  8 
30 mary  9 
31 tom  -4 
32 tom  -3 
33 tom  -2 
34 tom  -1 
35 tom  1 
36 tom  2 
37 tom  3 
38 tom  4 
39 tom  5 
40 tom  6 

所需的輸出

dff 
    noms nums new_times 
1 jane -6  -6 
2 jane -5  -5 
3 jane -4  -4 
4 jane -3  -3 
5 jane -2  -2 
6 jane -1  -1 
7 jane 1   1 
8 jane 2   2 
9 jane 3   3 
10 jane 4   4 
11 jane NA   5 
12 jane NA   6 
13 jane NA   7 
14 jane NA   8 
15 jane NA   9 
16 jane NA  10 
17 jane NA  11 
18 jane NA  12 
19 john -2  -2 
20 john -1  -1 
21 john 1   1 
22 john 2   2 
23 john 3   3 
24 john 4   4 
25 john 5   5 
26 john 6   6 
27 john 7   7 
28 john 8   8 
29 john NA   9 
30 john NA  10 
31 john NA  11 
32 john NA  12 
33 mary -1  -1 
34 mary 1   1 
35 mary 2   2 
36 mary 3   3 
37 mary 4   4 
38 mary 5   5 
39 mary 6   6 
40 mary 7   7 
41 mary 8   8 
42 mary 9   9 
43 mary NA  10 
44 mary NA  11 
45 mary NA  12 
46 tom -4  -4 
47 tom -3  -3 
48 tom -2  -2 
49 tom -1  -1 
50 tom 1   1 
51 tom 2   2 
52 tom 3   3 
53 tom 4   4 
54 tom 5   5 
55 tom 6   6 
56 tom NA   7 
57 tom NA   8 
58 tom NA   9 
59 tom NA  10 
60 tom NA  11 
61 tom NA  12 

編輯

謝謝@rrs爲他的貢獻。該代碼工作正常玩具的數據,但在真正的數據集,以下錯誤彈出

Error in rep(NA, length(pootdf$new_numbers) - length(pootdf$time)) : 
    invalid 'times' argument 

玩具數據和真實數據之間的唯一區別是,大數據是在大約400,000行大得多。兩個名稱變量都設置爲因子,數字變量設置爲整數。我已經將大型DF劃分爲約100行的較小的可管理的DF,並且仍然出現錯誤。有誰知道會發生什麼,以及我可能會如何解決它?以下是追溯。

traceback() 
7: .fun(piece, ...) 
6: function (i) 
    { 
     piece <- pieces[[i]] 
     if (.inform) { 
      res <- try(.fun(piece, ...)) 
      if (inherits(res, "try-error")) { 
       piece <- paste(capture.output(print(piece)), collapse = "\n") 
       stop("with piece ", i, ": \n", piece, call. = FALSE) 
      } 
     } 
     else { 
      res <- .fun(piece, ...) 
     } 
     progress$step() 
     res 
    }(1L) 
5: .Call("loop_apply", as.integer(n), f, env) 
4: loop_apply(n, do.ply) 
3: llply(.data = .data, .fun = .fun, ..., .progress = .progress, 
     .inform = .inform, .parallel = .parallel, .paropts = .paropts) 
2: ldply(.data = pieces, .fun = .fun, ..., .progress = .progress, 
     .inform = .inform, .parallel = .parallel, .paropts = .paropts) 
1: ddply(pootdf, .(hai_dispense_number), AddRows) 

回答

2

我認爲這會做你想要什麼:

AddRows <- function(df) { 
    new_numbers <- seq(from = min(df$numbers), to = 12) 
    new_numbers <- new_numbers[new_numbers != 0] 
    noms <- rep(unique(df$noms), length(new_numbers)) 
    numbers <- c(df$numbers, rep(NA, length(new_numbers) - length(df$numbers))) 

    return(data.frame(noms, numbers, new_numbers)) 
} 

ddply(df, .(noms), AddRows) 
+0

謝謝你這麼多寫一個函數你的幫助。你的代碼完全適用於我的玩具數據,但是當我應用到我的真實數據時,出現以下錯誤代碼錯誤(NA,長度(new_numbers) - 長度(pootdf $ hai_dispense_number)): 無效'times'參數。我的DF是375168行 - 但是應該可以增加右邊的行數。我知道,我的慾望將低於601092行。 – user2363642

+0

發現錯誤 - 與您的代碼無關 - 更多與我的數據有關! – user2363642

相關問題