running through a dataframe then apply a multiplier to certain rows in R

I'm looking at running through a dataframe called combinedpred and if that row contains a certain a value apply a multiplier to another value within a certain column of that same row.

I think the best way to do it is by having a for loop to run through the dataframe then have an if statement to check if that value is there then do this if not do something else to do a different column in that same row.

for example:

dataframe combinedpred:

monday | Tuesday     | Wednesday | Thurday    | class
0.99   |  0.2643     |   0.234   |  0.22343   | Maths
0.32   |  0.2123     |   0.22    |  0.63      | Science
0.233  |  0.6423     |   0.24    |  0.73      | English

what i think the code will look like:

for(i in 1:nrow(combinedpred)) {
  if (conbinedpred[i] %in% 'class'){
     combinedpred[i,"Maths"] * 1.5
  } if (conbinedpred[i] %in% 'class') {
     combinedpred[i,"English"| "science"] * 1.9
  } if (conbinedpred[i] %in% 'class') {
    combinedpred[i,"history"] * 1.1
  }
}

Outcome:

as the first column has maths in it apply a 1.5 multiplier to it to Monday column but only on that row. I've just realise by example above is slightly wrong.

728x90

3 Answers running through a dataframe then apply a multiplier to certain rows in R

A loop isn't optimal, you should rather try this:

conbinepred$monday <- ifelse(conbinepred$class=="Maths", conbinepred$monday*1.5, ifelse(conbinepred$class=="Science", conbinepred$monday*1.9, conbinepred$monday*1.1))

4 months ago

Using dplyr will likely be cleaner and faster, both to run and type:

library(dplyr)

df <- data.frame(
    x = as.character(LETTERS[1:10]),
    y = 1:10
)

df %>% 
    mutate(
        y = ifelse(x == "D", y * 100, y)
    )

Another way, with dplyr as well, might be to add a column that holds the multipliers.

df <- data.frame(
    x = as.character(LETTERS[1:3]),
    y = 1:3,
    z = runif(3),
    multipliers = c(5, .3, .2)
)

df %>% 
    mutate(
        y = y * multipliers, # multiply
        z = z * multipliers
    ) %>% 
    select(-multipliers) # remove multipliers

Below is a full solution: create a data.frame that holds the multiplier respective to each each class, merge them then multiply. Cleaner, faster, easier to maintain.

multipliers <- data.frame(
    class = LETTERS[1:3],
    multiplier = c(.3, 5, 100)
)

df <- data.frame(
    class = sample(LETTERS[1:3], 100, replace = TRUE),
    x = runif(100),
    y = runif(100)
)

df %>% 
    left_join(multipliers, by = "class") %>% 
    mutate(
        x = x * multiplier,
        y = y * multiplier
    ) %>% 
    select(-multiplier)

4 months ago

Is that what you want. Multiply the entire row with the right multiplier based on class.

lookupTable <- c(Maths = 1.5, Science= 1.9, English= 1.9, History = 1.1)
df1$multipl <- lookupTable[df1$class]

df1[,1:4] <- df1[,1:4] * df1$multipl

df1 <-
structure(list(monday = c(0.99, 0.32, 0.233), Tuesday = c(0.2643, 
0.2123, 0.6423), Wednesday = c(0.234, 0.22, 0.24), Thurday = c(0.22343, 
0.63, 0.73), class = c("Maths", "Science", "English")), row.names = c(NA, 
-3L), class = "data.frame")

4 months ago