Update the dataframe based on a column

I have a data frame like below and have millions of records, so I want to add column for total_score and expected_score. If region1 has "all region" then add columns t_all_region, t_east, t_west and add values from total_score.

so here we have 3 regions then create three columns t_all_region, t_east, t_west and add values from total_score

Same needed to do for expected_score.

df <- data.frame(total_score=c(4.5,12.2,4.6,9.2,12.2,36.4),
                 Region1=c("All region",NA,NA,"All region","All region",NA),

Looks like you might be looking for an if_else statement. Interestingly if_else() is slightly different from ifelse(). if_else() is from dplyr while ifelse() is native to R.
Using dplyr

df %>%
  mutate(t_all_region = if_else(Region1 == "All region",

And then repeat for t_east and t_west

Here is a generic approach.

df <- data.frame(
  total_score = c(4.5, 12.2, 4.6, 9.2, 12.2, 36.4),
  expected_score = c(4.5, 12.1, NA, 9.2, 12.2, NA),
  Region1 = c("All region", NA, NA, "All region", "All region", NA),
  Region2 = c("EAST", "EAST", "EAST", "EAST", "EAST", NA),
  Region3 = c("West", NA, "West", "West", "West", "West")

reg <- names(select(df,  starts_with("Reg")))
names(reg) <- reg

(regnames <- map(reg, \(x){
  make.names(unique(na.omit(pull(df, x))))

(df2 <- mutate(
    \(x){if_else(is.na(x), NA, total_score)},
    .names = "t_{col}")

regnames2 <- paste0("t_", regnames)
names(regnames2) <- paste0("t_", names(regnames))

names_from_regnames2 <- Vectorize(function(x) { regnames2[[x]]}, USE.NAMES = FALSE)

(fin <- rename_with(df2,
  .cols = names(regnames2),
  .fn = names_from_regnames2

Hello my name is Alexis Nsabimana, I would like to ask you the script used to keep the first coordinates for each day for my data
#keep another set of coordinates if at least 6 hours later
#keep a third set of coordinates if at least 6 hours later again
Because I am working on home range estimations of chimpanzees community

I would really encourage you to review the following guide, FAQ: Tips for writing R-related questions.
For example, the guide emphasizes asking coding questions with formatted code-chunks and a reprex.

You may have noticed folks here requesting minimal reprexes, that's because asking questions this way saves answerers a lot of time.

Reproducible Examples:

  • help make your question clear and replicable
  • increases the probability folks will reach out and try to help,
  • reduces the number of back-and-forths required to understand the question,
  • and makes your question and suggested solutions more useful to folks in the future researching similar problems.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.