【问题标题】:Looping over Rows of a Dataframe in R在R中循环数据框的行
【发布时间】:2021-10-07 09:34:24
【问题描述】:

逐行循环

我有以下关于学生成绩的数据集。

name <- c("John", "Rachel", "Judy", "James", "Oloo")

english <- c("70", "50e", "19c", "38^", "33^")

math <- c("65", "25c", "68", "32^", "50")

science <- c("45", "50", "25c", "27e", "72")
             
social <- c("56", "76", "42", "23^", "68")    

marks <- data.frame(name, english, math, science, social)

marks
             

要通过考试,学生必须同时参加持续评估和期末考试,并且每个科目的成绩至少达到 40%。

Pass:如果学生在分数后有NOe^,则表示所有分数都在40 分及以上。

Supplementary:学生拥有^e^ 表示分数低于 40。e 的学生可能有超过 40 分,但仍因未能进行持续评估而失败。

special:学生有c,但没有e^

Supplementary and special:补充和特殊的结合。这意味着学生拥有e^ 或同时拥有c

Discontinued:一个学生有4个或更多e,4个或更多^。此外,学生可能会同时出现 4 次或更多次的 e^任务:对于每个学生(即一行数据),我想要R 中的一个代码,它将返回一个新列comment,如下所示。

name <- c("John", "Rachel", "Judy", "James", "Oloo")

english <- c("70", "50e", "19c", "38^", "33^")

math <- c("65", "25c", "68", "32^", "50")

science <- c("45", "50", "25c", "27^", "72")
             
social <- c("56", "76", "42", "23^", "68")  

comment <- c("Pass", "Supplementary & Special", "Special", "Discontinued", "Supplementary")

marks_1 <- data.frame(name, english, math, science, social, comment)

marks_1
             

再次注意: Pass:如果学生在分数后面有NO e^,则表示所有分数都在 40 分及以上。

Supplementary:学生拥有^e^ 表示分数低于 40。e 的学生可能有超过 40 分,但仍因未能参加持续评估而失败。

special:学生有c,但没有e^

Supplementary and special:补充和特殊的结合。这意味着学生拥有e^ 或同时拥有c

Discontinued:一个学生有4个或更多e,4个或更多^。此外,学生可能会同时使用 e^,这种情况会发生 4 次或更多次。

【问题讨论】:

  • 学生什么时候获得“特殊”价值?什么时候“停产”?第 5 行 Supplementary 但第 2 行 Supplementary &amp; Special 怎么样?
  • 罗纳克,谢谢。特别 - 有一个c。补充 - 当有 ^e 时。停产:在 4 门或更多科目中,学生获得少于 40 或包括 4 e 或 4 ^。在学生没有ce^ 的情况下通过,这意味着所有科目都超过 40 分或以上。第 5 行是补充,因为学生有 33^。第 2 行是补充和特殊的,因为学生在分数后有 ec。也许我解释得不好。
  • Pass:如果学生 NO e^,则所有分数都在 40 分及以上。 Supplementary:学生拥有^e^ 表示分数低于 40。e 的学生可能有超过 40 分,但仍因未能进行持续评估而失败。 special:学生有c,但没有e^Supplementary and special:补充和特殊的结合。这意味着学生拥有e^ 或两者同时拥有cDiscontinued:一个学生有 4 个或更多 e、4 个或更多 ^e^ 的组合发生 4 次或更多次。

标签: r tidyverse


【解决方案1】:

我认为您需要手动写下每条规则并将它们应用于每一行。

library(dplyr)

apply_rules <- function(x) {
  e_rule <- any(grepl('e', x, fixed = TRUE))
  c_rule <- any(grepl('c', x, fixed = TRUE))
  fail_rule <- any(grepl('^', x, fixed = TRUE))
  case_when(all(grepl('[\\^e]', x)) ~ 'Discontinued',
            (e_rule || fail_rule) && c_rule ~ 'Supplementary & Special', 
            fail_rule || e_rule ~ 'Supplementary',
            c_rule && (c_rule || fail_rule) ~ 'Special', 
            !(e_rule ||  c_rule) ~ 'Pass', 
            fail_rule ~ 'Discontinued' 
            )
}

marks %>%
  rowwise() %>%
  mutate(comment = apply_rules(c_across(english:social)))

#  name   english math  science social comment                
#  <chr>  <chr>   <chr> <chr>   <chr>  <chr>                  
#1 John   70      65    45      56     Pass                   
#2 Rachel 50e     25c   50      76     Supplementary & Special
#3 Judy   19c     68    25c     42     Special                
#4 James  38^     32^   27^     23^    Discontinued           
#5 Oloo   33^     50    72      68     Supplementary         

【讨论】:

    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 2012-11-06
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多