0

Say I have some data and I want to do a loop for multiple variables where I subtract '1' from oldvariable and create a new variable that adds 'p' to the original variable name. Here is some data. So say I have var1 and var2 and want to create var1p and var2p where var1p = var1 - 1. How can I loop this for many variables? Below the data is my attempt but it does not get the job done. I could subtract '1' but am not sure how to append to the df new variable names with these values.

df <- read.table(text =
                   "id var1 var2 var2p  var3p group
                 1  12    3    11   2    1
                 2  8     6    7   5    1
                 3  25    30   24   5    2
                 4  26    31   25   30    2
                 5  22    29   21   28    2", header = T)


new_data <- data.frame(lapply(df, function(x) x-1))
s_baldur
  • 29,441
  • 4
  • 36
  • 69
bvowe
  • 3,004
  • 3
  • 16
  • 33

2 Answers2

4

In base R, we can create a variable with columns to select (cols) and subtract -1 from those columns only and add "p" to their names to create new columns.

cols <- c("var1", "var2")
df[paste0(cols, "p")] <- df[cols] - 1

df
#  id var1 var2 group var1p var2p
#1  1   12    3     1    11     2
#2  2    8    6     1     7     5
#3  3   25   30     2    24    29
#4  4   26   31     2    25    30
#5  5   22   29     2    21    28
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
2

We can use mutate_at from dplyr to create new columns based on calculation on columns of interest

library(dplyr)
df %>% 
    mutate_at(vars(starts_with('var')), funs(p = .-1))

Or using data.table

library(data.table)
nm1 <- grep("var", names(df))
setDT(df)[, (nm1) := .SD - 1, .SDcols = nm1]
akrun
  • 874,273
  • 37
  • 540
  • 662