Home > Software engineering >  how to summary column of the same suffix in R
how to summary column of the same suffix in R

Time:12-14

I want to get mean and max/min column of suffix is .score from below data.frame.And also convert A = 2,B = 1,C = 0, count every student number of ABC.

I can use this code one by one column, but it's will spend so many time.Can anyone improve the code?

df %>% mutate(C = ifelse(C == "A", 2,ifelse(C == "B", 1, 0)))

df <- data.frame(Name = c("Kevin", "Mary", "Cobe", "Linda", "Lisa"), 
                 ID = c("AZ4524", "AZ4525", "AZ4527", "AZ4544", "AZ4572"), 
                 C = c("B", "A", "A", "C", "A"), C.Score = c(72, 89, 80, 65, 88), 
                 M = c("A", "A", "B", "A", "B"), M.Score = c(95, 89, 70, 85, 87), 
                 E = c("A", "C", "A", "A", "B"), E.Score = c(82, 61, 88, 94, 88), 
                 S = c("A", "A", "A", "B", "A"), S.Score = c(91, 95, 89, 73, 97))

CodePudding user response:

One approach is to use apply():

idx1 <- which(grepl("Score", colnames(df)))
apply(df[ , idx1], 2, function(x) c(mean(x), min(x), max(x)))
#      C.Score M.Score E.Score S.Score
# [1,]    78.8    85.2    82.6      89
# [2,]    65.0    70.0    61.0      73
# [3,]    89.0    95.0    94.0      97
idx2 <- which(colnames(df) %in% c("C", "M", "E", "S"))
apply(df[, idx2], 2, function(x) 3 - as.numeric(factor(x, levels=LETTERS[1:3])))
#      C M E S
# [1,] 1 2 2 2
# [2,] 2 2 0 2
# [3,] 2 1 2 2
# [4,] 0 2 2 1
# [5,] 2 1 1 2

CodePudding user response:

Data

df <- 
  data.frame(
    Name = c("Kevin", "Mary", "Cobe", "Linda", "Lisa"), 
    ID = c("AZ4524", "AZ4525", "AZ4527", "AZ4544", "AZ4572"), 
    C = c("B", "A", "A", "C", "A"), C.Score = c(72, 89, 80, 65, 88), 
    M = c("A", "A", "B", "A", "B"), M.Score = c(95, 89, 70, 85, 87), 
    E = c("A", "C", "A", "A", "B"), E.Score = c(82, 61, 88, 94, 88), 
    S = c("A", "A", "A", "B", "A"), S.Score = c(91, 95, 89, 73, 97)
    )

Code

library(tidyr)
library(dplyr)

df %>% 
  mutate(
    across(
      .cols = C:S,
      .fns = ~case_when(
        . == "A" ~ 2,
        . == "B" ~ 1,
        TRUE ~ 0
       )
      )
    )

Output

   Name     ID C C.Score M M.Score E E.Score S S.Score
1 Kevin AZ4524 1       0 2       0 2       0 2      91
2  Mary AZ4525 2       0 2       0 0       0 2      95
3  Cobe AZ4527 2       0 1       0 2       0 2      89
4 Linda AZ4544 0       0 2       0 2       0 1      73
5  Lisa AZ4572 2       0 1       0 1       0 2      97
  •  Tags:  
  • r
  • Related