Home > database >  Using mutate to create variables with extra operations in R
Using mutate to create variables with extra operations in R

Time:12-15

I want to create a new column called "1adj" from the column "1", which has also used round and mean and minus -100, which I can do:

1adj <- round(mean(DF$1)-100)

However, I want to do this with multiple DF columns (20-27) using GGplot, using mutate to create them as new columns in the DF. I can create the extra columns:

DF<-DF %>%
    mutate(across(20:27,
            .names = "{.col}Adj"))

But Where would I add the extra parameters as reflected in the first line of code, so I don't have to type them all out? Any help appreciated.

CodePudding user response:

I think this is what you are looking for. You can just add the function into across.

library(tidyverse)

DF %>%
  rowwise() %>%
  mutate(across(c(2:4),
                .names = "{.col}Adj", list( ~ round(mean(
                  .x
                ) - 100))))

Output

# A tibble: 5 × 7
# Rowwise: 
     id first second third firstAdj secondAdj thirdAdj
  <dbl> <dbl>  <dbl> <dbl>    <dbl>     <dbl>    <dbl>
1     1  39.0   42.3  37.9      -61       -58      -62
2     2  20.1   32.9  47.8      -80       -67      -52
3     3  37.6   33.8  48.9      -62       -66      -51
4     4  31.5   21.6  23.2      -69       -78      -77
5     5  49.2   22.8  29.9      -51       -77      -70

Data

DF <-
  structure(
    list(
      id = c(1, 2, 3, 4, 5),
      first = c(
        38.9932923251763,
        20.0923723727465,
        37.640398349613,
        31.4673039061017,
        49.192731983494
      ),
      second = c(
        42.341671793256,
        32.936319950968,
        33.8184517389163,
        21.5938150603324,
        22.8182014194317
      ),
      third = c(
        37.9402944352478,
        47.8147878032178,
        48.8590325415134,
        23.1802612892352,
        29.9195193173364
      )
    ),
    class = "data.frame",
    row.names = c(NA,-5L)
  )

CodePudding user response:

Just keep it simple (if per-row value is desired, i.e., no mean):

df %>% 
    mutate(
        across(-id, ~round(.-100), .names = "{.col}Adj")
    )
  id    first   second    third firstAdj secondAdj thirdAdj
1  1 38.99329 42.34167 37.94029      -61       -58      -62
2  2 20.09237 32.93632 47.81479      -80       -67      -52
3  3 37.64040 33.81845 48.85903      -62       -66      -51
4  4 31.46730 21.59382 23.18026      -69       -78      -77
5  5 49.19273 22.81820 29.91952      -51       -77      -70

For mean you just do mean I guess:

df %>% 
    mutate(
        across(-id, ~round(mean(.)-100), .names = "{.col}Adj")
    )
  id    first   second    third firstAdj secondAdj thirdAdj
1  1 38.99329 42.34167 37.94029      -65       -69      -62
2  2 20.09237 32.93632 47.81479      -65       -69      -62
3  3 37.64040 33.81845 48.85903      -65       -69      -62
4  4 31.46730 21.59382 23.18026      -65       -69      -62
5  5 49.19273 22.81820 29.91952      -65       -69      -62

Thanks @Andrew Gillreath-Brown for providing example data.

  • Related