Behavior of assignment operators ('=' and '<-') inside a function in R -


this related assignment operators in r: '=' , '<-'; however, question not answered there.

the linked question , answers explain using <- inside of function declares variable assignment in user workspace, variable can used after function called. (ed note: not stated in linked answer, , if stated, wrong. if made statement evaluation of argument lists , restricted calls of such functions global environment might correct.)

this seem explain following difference in behavior. following code produces data frame 1 might expect:

a <- data.frame(   sub = rep(c(1:3),each=3),   word = rep(c('hap','lap','sap'),3),   vowel_length = sample(c(1:100),9)   ) 

the result is:

  sub word vowel_length 1   1  hap           31 2   1  lap            2 3   1  sap           71 4   2  hap           58 5   2  lap           28 6   2  sap           20 7   3  hap           78 8   3  lap           72 9   3  sap           77 

however, if use <- inside of data.frame() function, follows, different result.

b <- data.frame(   sub <- rep(c(1:3),each=3),   word <- rep(c('hap','lap','sap'),3),   vowel_length <- sample(c(1:100),9)   ) 

this result is:

  sub....rep.c.1.3...each...3. word....rep.c..hap....lap....sap....3. 1                            1                                    hap 2                            1                                    lap 3                            1                                    sap 4                            2                                    hap 5                            2                                    lap 6                            2                                    sap 7                            3                                    hap 8                            3                                    lap 9                            3                                    sap   vowel_length....sample.c.1.100...9. 1                                  31 2                                  15 3                                   4 4                                   2 5                                  89 6                                  55 7                                  12 8                                  72 9                                  47 

i assume that, because using <- inside function declares variable globally, headers of data frame inherited global declaration, linked question , answers seem indicate. [see comments.]

however, i'm curious why get, example, sub....rep.c.1.3...each...3. header of first column in data frame instead of sub <- rep(c(1:3),each=3),, or instead of 1 1 1 2 2 2 3 3 3.

update:

as @anandamahto pointed out in deleted comment, setting check.names false produces following behavior.

c <- data.frame(   sub <- rep(c(1:3),each=3),   word <- rep(c('hap','lap','sap'),3),   vowel_length <- sample(c(1:100),9),   check.names=false ) 

where result is:

  sub <- rep(c(1:3), each = 3) word <- rep(c("hap", "lap", "sap"), 3) 1                            1                                    hap 2                            1                                    lap 3                            1                                    sap 4                            2                                    hap 5                            2                                    lap 6                            2                                    sap 7                            3                                    hap 8                            3                                    lap 9                            3                                    sap   vowel_length <- sample(c(1:100), 9) 1                                  15 2                                   3 3                                  82 4                                  33 5                                  99 6                                  53 7                                  89 8                                  77 9                                  47 

and clarify, question why behavior happening. in particular, why sub....rep.c.1.3...each...3. header instead of sub <- rep(c(1:3),each=3), or 1 1 1 2 2 2 3 3 3 check.names=true.

and now, suppose i'm curious why sub <- rep(c(1:3),each=3), header check.names=false?

it appears question strange naming r ends using, , you're wondering why doesn't have spaces, <, , on.

if that's actual question, should @ check.names argument in data.frame.

from ?data.frame:

check.names logical. if true names of variables in data frame checked ensure syntactically valid variable names , not duplicated. if necessary adjusted (by make.names) are.

thus, can names expecting setting check.names false:

b <- data.frame( sub <- rep(c(1:3),each=3),                   word <- rep(c('hap','lap','sap'),3),                   vowel_length <- sample(c(1:100),9),                  check.names = false) b #   sub <- rep(c(1:3), each = 3) word <- rep(c("hap", "lap", "sap"), 3) # 1                            1                                    hap # 2                            1                                    lap # 3                            1                                    sap # 4                            2                                    hap # 5                            2                                    lap # 6                            2                                    sap # 7                            3                                    hap # 8                            3                                    lap # 9                            3                                    sap #   vowel_length <- sample(c(1:100), 9) # 1                                  33 # 2                                  20 # 3                                   5 # 4                                  83 # 5                                  99 # 6                                  79 # 7                                  58 # 8                                  46 # 9                                  44 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -