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))
0

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

6 Comments

This works swimmingly. Now suppose instead of subtracting 1 I wanted to recode a given value (so say recode 22 = 4)). Would I replace df[cols] - 1 with recode(df[cols], 22=4)
@bvowe you want to replace all 22 in the dataframe with 4 ?
Just for example as I am learning in R say I wanted to replace all 22 in var1 and var2 with 4?
One way is by using replace, replace(df[cols], df[cols] == 22, 4)
So I would put cols <- c("var1", "var2") df[paste0(cols, "p")] <-replace(df[cols], df[cols] == 22, 4)
|
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]

1 Comment

Thank you this is a awesome answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.