根据另一列在列中添加值

时间:2019-12-06 03:41:16

标签: r

来自

x <- read.csv("stats.csv", header = TRUE)

我有两列:

Gender   Score
male     20
female   25
male     10
female   10

例如,我如何添加男性的总分?

1 个答案:

答案 0 :(得分:2)

我们可以使用

@app.route("/") def index():

如果“女性”应保留为library(dplyr) x %>% mutate(totalScore = sum(Score[Gender == "male"]))

NA

同时针对“性别”

 x %>%
      mutate(totalScore  = case_when(Gender == "male" ~ sum(Score),
         TRUE ~ NA_real_))

或者在 x %>% group_by(Gender) %>% mutate(totalScore = sum(Score))

base R

或者有选择地为行添加

x['totalScore'] <- with(x, sum(Score[Gender == "male"]))