Suppose I have the following dataframe. What I want to do is to create new variables with prefixes ~new., e.g. new.factor1, new.factor2, etc. Also, these new variables are created from the old variables based on the same conditions. Let's say the common conditions are that new.factor variables will replace 4 and 5 with 44 and 55 leaving the rest same as before. I want to implement this in a way that I don't have to feed the new names into code for each variable. This is the part I'm stuck with. The solutions I found need to be given the variable names, which I want the solution code to read from the existing variable names.
factor1 <- c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,8,9, 9, 10, 10)
factor2 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,16,17, 18, 19, 20)
factor3 <- rnorm(20)
factor4 <- c(1:20)
dataset <- data.frame(factor1, factor2, factor3, factor4)
Aucun commentaire:
Enregistrer un commentaire