I have a data frame with a lot of variables whose names include tags.
mydf <- data.frame( var_x = 1:5, var_y = runif(5), var_z = runif(5), other_x = 10:14, other_p = runif(5), other_r = runif(5) ) mydf var_x var_y var_z other_x other_p other_r 1 1 0.2700212 0.05893272 10 0.6212327 0.6177092 2 2 0.1284033 0.27333098 11 0.6933060 0.7520978 3 3 0.7313771 0.69352560 12 0.3154764 0.8479646 4 4 0.2400357 0.25151053 13 0.2057361 0.5138406 5 5 0.1797793 0.78550584 14 0.6671606 0.5801830
I would like to split var_* variables var_x and other_* variables using other_x . How can i make it easy?
I tried using mutate_each for dplyr . The following works if there is only one group to scale. How can I automate this for each tag?
library(dplyr) scale_var <- mydf$var_x mydf %>% mutate_each(funs(./scale_var), matches("^var"))
I tried to write my own function as follows.
mymutate <- function(data, type) { scale_var <- mydf[[paste0(type, "_x")]] data %>% mutate_each( funs(./scale_var), matches(paste0("^", type)) ) }
But when I tried to run it only on one type mymutate(mydf, type = "var") , it threw an error that I really don't understand: Error in paste0("^", type) : object 'type' not found
UPDATE
I would like to use only the new variables, so it doesn't matter that the method also divides the x variables.
I have many tags like var and other , so I donβt want to write them in each case. This is why I tried to build my own function to use it later with lapply .
UPDATE2
These are the variables of my data frame.
[1] "location_50_all_1" "location_50_both_sides_important_1" [3] "location_50_left_important_1" "location_50_other_important_1" [5] "location_50_right_important_1" "ownership_all_1" [7] "ownership_both_sides_important_1" "ownership_left_important_1" [9] "ownership_other_important_1" "ownership_right_important_1" [11] "person_all_1" "person_both_sides_important_1" [13] "person_left_important_1" "person_other_important_1" [15] "person_right_important_1" "union_all_1" [17] "union_both_sides_important_1" "union_left_important_1" [19] "union_other_important_1" "union_right_important_1" [21] "total_left_important" "total_right_important" [23] "total_both_sides_important" "total_other_important" [25] "total_firm_officials" "left" [27] "right" "connected"
I would like to split location_50* variables by location_50_all_1 and the same goes for location_200* , ownership* , person* , union* .
Update3
Here is the answer to the question why 'type' not found .