Home > Blockchain >  Operations with a static row in R
Operations with a static row in R

Time:12-09

I have a df with the percentage of good, regular and bad students in two classrooms in the first two rows, and with the percentage of good, regular and bad students in the whole school, like this:

  Classr Good Regular   Bad 
  <chr> <dbl>   <dbl> <dbl> 
1 A        30      10    60   
2 B        40      10    50   
3 PR       80      10    10 

I need to calculate an index for any classroom which is the result of

|PRGood-GoodA| |PRRegular-RegularA| |PRBad-BadA| and for classroom B |PRGood-GoodB| |PRRegular-RegularB| |PRBad-BadB|

In this example, the result should be:

  Classr Good Regular   Bad  INDEX
  <chr> <dbl>   <dbl> <dbl>  <dbl>
1 A        30      10    60    100  
2 B        40      10    50     80
3 PR       80      10    10     NA <- This value doesn't matter

Any idea on how to do so?

I tried with this code but I get an error message:

df%>%
  mutate(across(2:4, ~.-tail(1,across(2:4)),))

CodePudding user response:

One option to achieve your desired result may look like so:

library(dplyr)

mutate(d, INDEX = rowSums(across(c(Good, Regular, Bad), ~ abs(.x[length(.x)] - .x))))
#>   Classr Good Regular Bad INDEX
#> 1      A   30      10  60   100
#> 2      B   40      10  50    80
#> 3     PR   80      10  10     0

DATA

d <- structure(list(Classr = c("A", "B", "PR"), Good = c(
  30L, 40L,
  80L
), Regular = c(10L, 10L, 10L), Bad = c(60L, 50L, 10L)), class = "data.frame", row.names = c(
  "1",
  "2", "3"
))
  • Related