R simply not running a line of code in a function -


i trying write function graph 4 dataframes against each other, , have narrowed entirety of issues have been having down 1 line of code r reason not running.

compare=function(a,b,c,d){  d1=a  d2=b  d3=c  d4=d   for(n in 1:4){    assign(paste0("colnames",n),colnames(get(paste0("d",n))))  } } 

when run line creates colnames1, colnames2, colnames3, , colnames4, oustide of function, works, if run using function, while d(1:4) created various dtaframes, colnames1:4 isnt created. know going on here?

you have specify global environment in assign exclusively, if want assign variable in function. try run through this article, , check code below.

assigntest1 = function(){   assign("val1", 1) } assigntest1() print(val1) # error in print(val1) : object 'val1' not found  # ----------  assigntest2 = function(){   assign("val2", 2, globalenv()) } assigntest2() print(val2) # [1] 2  # ----------  assigntest3 = function(){   val3 <- 3  } assigntest3() print(val3) # error in print(val3) : object 'val3' not found  # ----------  assigntest4 = function(){   val4 <<- 3 } assigntest4() print(val4) # [1] 3 

Comments