1

我正在尝试使用这些数据创建一个函数,并且非常感谢您的帮助!

    example<- data.frame(Day=c(2,4,8,16,32,44,2,4,8,16,32,44,2,4,8,16,32,44),
          Replicate=c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,
                      1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,
                      1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3),
          Treament=c("CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC","CC",
                     "HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP","HP",
                     "LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL","LL"),
          AFDM=c(94.669342,94.465752,84.897023,81.435993,86.556221,75.328294,94.262162,88.791240,75.735474,81.232403,
                 67.050593,76.346244,95.076522,88.968823,83.879073,73.958836,70.645724,67.184695,99.763156,92.022673,
                 92.245362,74.513934,50.083136,36.979418,94.872932,86.353037,81.843173,67.795465,46.622106,18.323099,
                 95.089932,93.244212,81.679814,65.352385,18.286525,7.517794,99.559972,86.759404,84.693433,79.196504,
                 67.456961,54.765706,94.074014,87.543693,82.492548,72.333367,51.304676,51.304676,98.340870,86.322153,
                 87.950873,84.693433,63.316485,63.723665))

例子:

我想插入一个新行,其 AFDM 值(例如,0.9823666)是用另一个函数计算的。

这个新行必须在第 2 天(并将其称为第 0 天),我想保留每个副本的名称和每个组的治疗。

因此,这个新行必须是:Day 0, Replicate=same, Treatment=same, AFDM=0.9823666

这样我以后可以对数据进行回归(从 0 到 44,每个处理 3 个重复)。

我希望有一个解决方案dplyr

提前致谢

4

2 回答 2

0

我们可以用 来创建一个分组列cumsum,然后用completefill其他列展开数据集

library(dplyr)
library(tidyr)     
example %>%
    group_by(grp = cumsum(Day == 2)) %>% 
    complete(Day =  c(0, unique(Day)), fill = list(AFDM = 0.9823666)) %>%
    fill(Replicate, Treament, .direction = 'updown')
# A tibble: 63 x 5
# Groups:   grp [9]
#     grp   Day Replicate Treament   AFDM
#   <int> <dbl>     <dbl> <chr>     <dbl>
# 1     1     0         1 CC        0.982
# 2     1     2         1 CC       94.7  
# 3     1     4         1 CC       94.5  
# 4     1     8         1 CC       84.9  
# 5     1    16         1 CC       81.4  
# 6     1    32         1 CC       86.6  
# 7     1    44         1 CC       75.3  
# 8     2     0         2 CC        0.982
# 9     2     2         2 CC       94.3  
#10     2     4         2 CC       88.8  
# … with 53 more rows
于 2020-06-27T20:05:42.450 回答
0

您可以使用默认值distinct获取唯一的ReplicateTreament,添加DayAFDM列,并将行绑定到原始数​​据框。

library(dplyr)

example %>%
  distinct(Replicate, Treament) %>%
  mutate(Day = 0, AFDM = 0.9823666) %>%
  bind_rows(example) %>%
  arrange(Replicate, Treament)

#   Replicate Treament Day       AFDM
#1          1       CC   0  0.9823666
#2          1       CC   2 94.6693420
#3          1       CC   4 94.4657520
#4          1       CC   8 84.8970230
#5          1       CC  16 81.4359930
#6          1       CC  32 86.5562210
#7          1       CC  44 75.3282940
#8          1       HP   0  0.9823666
#9          1       HP   2 99.7631560
#10         1       HP   4 92.0226730
#.....
于 2020-06-28T07:38:54.187 回答