R D群减法

wljmcqd8  于 2023-10-13  发布在  其他
关注(0)|答案(5)|浏览(99)

我有这个数据

DATA = data.frame(STUDENT=c(1, 1, 2, 2, 3, 3, 4, 5),
TIME=c(1, 2, 1, 2, 1, 2, 2, 1),
SCORE=c(6, 9, 9, 5, 5, 5, 8, 9))

试着让这个

DATA2 = data.frame(STUDENT=c(1, 2, 3, 4, 5), 
SCOREDIFFERENCE=c(3, -4, 0, NA, NA))

其中,我对所有测量值取SCORE[TIME==2]-SCORE[TIME==1],并且当只有一个测量值时,则值SCOREDIFFERENCE为NA
我试试这个:
DATA2 = DATA %>% group_by(STUDENT) %>% mutate(SCOREDIFFERENCE = SCORE[TIME==2]-SCORE[TIME==1])无成功

gv8xihay

gv8xihay1#

使用tidyverse的解决方案:

library(tidyverse)

DATA = data.frame(STUDENT=c(1, 1, 2, 2, 3, 3, 4, 5),
TIME=c(1, 2, 1, 2, 1, 2, 2, 1),
SCORE=c(6, 9, 9, 5, 5, 5, 8, 9))

DATA %>% pivot_wider(names_from = TIME, values_from = SCORE) %>% 
  group_by(STUDENT) %>% 
  summarise(SCOREDIFF = `2`-`1`)

# A tibble: 5 × 2
# STUDENT SCOREDIFF
# <dbl>     <dbl>
# 1         3
# 2        -4
# 3         0
# 4        NA
# 5        NA
vaj7vani

vaj7vani2#

您可以在summarise中尝试diff,使用.by = STUENT

DATA %>%
    summarise(SCOREDIFF = if (n() < 2) NA else diff(SCORE), .by = STUDENT)

或者像@LMc建议的那样,

DATA %>%
    summarise(SCOREDIFF = SCORE[2] - SCORE[1], .by = STUDENT)

这给

STUDENT SCOREDIFF
1       1         3
2       2        -4
3       3         0
4       4        NA
5       5        NA
waxmsbnn

waxmsbnn3#

基地的另一个选择。

DATA = data.frame(STUDENT=c(1, 1, 2, 2, 3, 3, 4, 5),
                  TIME=c(1, 2, 1, 2, 1, 2, 2, 1),
                  SCORE=c(6, 9, 9, 5, 5, 5, 8, 9))
aggregate(SCORE ~ STUDENT, data= DATA,  FUN = "diff") |> 
     setNames(c("STUDENT", "Diff"))
3b6akqbq

3b6akqbq4#

或者使用lead

DATA %>% group_by(STUDENT) %>% mutate(SCOREDIFFERENCE=lead(SCORE)-SCORE) %>% slice_head(n=1)

创建于2023-09-29带有reprex v2.0.2

# A tibble: 5 × 4
# Groups:   STUDENT [5]
  STUDENT  TIME SCORE SCOREDIFFERENCE
    <dbl> <dbl> <dbl>           <dbl>
1       1     1     6               3
2       2     1     9              -4
3       3     1     5               0
4       4     2     8              NA
5       5     1     9              NA
bd1hkmkf

bd1hkmkf5#

对时间不按顺序的鲁棒性:

DATA %>%
  reframe(score_dif = SCORE[TIME==2] - SCORE[TIME==1], .by = STUDENT)

相关问题