首页 > 解决方案 > 如何在此数据框中添加具有 dplyr 的 mutate 函数的列,以计算与第 0 天相比的重量差异?

问题描述

我在大学上 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

标签: rdataframedplyr

解决方案


你必须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"))

推荐阅读