r - Assign is not working as expected when recoding data frame variables? -


i've been having issue i've been trying recode many variables @ once. seemed easiest way things use assign , place variable in .globalenv. see it's not working outside of function.

does have idea why,

assign('dataframe$col1', 3 - dataframe$col1, env = .globalenv) 

seems have no effect on dataframe$col1?

using assign, can done in complicated way

 assign('dataframe', `[[<-`(dataframe, 'col',               value = 3- dataframe$col), envir=.globalenv)   dataframe$col  #[1]  2  1  0 -1 -2 

less complicated , safer be

 dataframe$col <- 3-dataframe$col 

or if using data.table

 library(data.table)  setdt(dataframe)[, col:= 3- col]  

and dplyr/magrittr option is

 library(dplyr)  library(magrittr)  dataframe %<>%          mutate(col = 3 - col) 

data

 dataframe <- data.frame(col= 1:5) 

Comments