如何在此数据框中添加带有dplyr的mutate函数的列,以计算与第0天相比的权重差?

时间:2018-10-16 09:48:26

标签: r dataframe dplyr

我正在上大学的R课,虽然不是编程方面的佼佼者,但实践却很完美;所以我想知道是否有人可以帮助我。我在计算正确的函数以计算每个小鸡与时间0相比的重量差异时遇到了麻烦。我知道我没得到它的一些简单信息,但是花了很多时间寻找解决方法。

问题 4.在完整的数据集中,引入一个新变量,该变量测量与第0天相比当前的体重差异。将此变量命名为weightgain

我有密码

library(dplyr)

complete2 <- complete %>% mutate(weightgain = weight)

    weight Time Chick Diet
1       42    0     1    1
2       51    2     1    1
3       59    4     1    1
4       64    6     1    1
5       76    8     1    1
6       93   10     1    1
7      106   12     1    1
8      125   14     1    1
9      149   16     1    1
10     171   18     1    1
11     199   20     1    1
12     205   21     1    1
13      40    0     2    1
14      49    2     2    1
15      58    4     2    1
16      72    6     2    1
17      84    8     2    1
18     103   10     2    1
19     122   12     2    1
20     138   14     2    1
21     162   16     2    1
22     187   18     2    1
23     209   20     2    1
24     215   21     2    1
25      43    0     3    1
26      39    2     3    1
27      55    4     3    1
28      67    6     3    1
29      84    8     3    1
30      99   10     3    1
31     115   12     3    1
32     138   14     3    1
33     163   16     3    1
34     187   18     3    1
35     198   20     3    1
36     202   21     3    1

2 个答案:

答案 0 :(得分:1)

您必须首先group_by,然后才能mutate

library(dplyr)

complete2 <- complete %>% 
  group_by(Chick) %>%
  mutate(weightgain = weight - weight[Time == 0])

head(complete2)
## A tibble: 6 x 5
## Groups:   Chick [1]
#  weight  Time Chick  Diet weightgain
#   <int> <int> <int> <int>      <int>
#1     42     0     1     1          0
#2     51     2     1     1          9
#3     59     4     1     1         17
#4     64     6     1     1         22
#5     76     8     1     1         34
#6     93    10     1     1         51

dput格式的数据。

complete <-
structure(list(weight = c(42L, 51L, 59L, 64L, 76L, 93L, 106L, 
125L, 149L, 171L, 199L, 205L, 40L, 49L, 58L, 72L, 84L, 103L, 
122L, 138L, 162L, 187L, 209L, 215L, 43L, 39L, 55L, 67L, 84L, 
99L, 115L, 138L, 163L, 187L, 198L, 202L), Time = c(0L, 2L, 4L, 
6L, 8L, 10L, 12L, 14L, 16L, 18L, 20L, 21L, 0L, 2L, 4L, 6L, 8L, 
10L, 12L, 14L, 16L, 18L, 20L, 21L, 0L, 2L, 4L, 6L, 8L, 10L, 12L, 
14L, 16L, 18L, 20L, 21L), Chick = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), Diet = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L)), class = "data.frame", row.names = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36"))

答案 1 :(得分:1)

另一种方法是对行进行排序,以确保时间= 0在每个小鸡中排在首位:

complete %>%
  arrange(Chick, Time) %>% 
  group_by(Chick) %>% 
  mutate(weightgain = weight - first(weight))