Why data frame column names different using = and <

This question already has an answer here:

  • What are the differences between “=” and “<-” in R? 6 answers

  • The <- operator not only assigns objects but creates them in the parent environment unlike = operator

    Renaming columns slightly:

    df1 <- data.frame(a1 = 1:5, b1 = 11:15)
    
    
    
    df1
    #   a1  b1
    # 1 1 11
    # 2 2 12
    # 3 3 13
    # 4 4 14
    # 5 5 15
    
    
    #The objects are only created in the dataframee but not in environment  
    # > exists(x = "a1")
    #[1] FALSE
    #> exists(x = "b1")
    #[1] FALSE
    
    
    #The objects are not only in created in the dataframe as well as in the environment
    
    df2 <- data.frame(a2 <- 1:5, b2 <- 11:15)
    df2
    #   a2....1.5 b2....11.15
    # 1        1         11
    # 2        2         12
    # 3        3         13
    # 4        4         14
    # 5        5         15
    
    # > exists(x = "a2")
    #[1] TRUE
    #> exists(x = "b2")
    #[1] TRUE
    

    If you want to have the column names as a, b, the correct syntax should be

    data.frame(a=1:5, b=1:5)
    

    The statement

    data.frame(a <- 1:5, b <- 1:5)
    

    R interprets it as if no column names are provided, so it treats the entire expression 'a <- 1:5' as the first column name, but there are 2 spaces and 3 illegal characters '<', '-', ':' that are not allowed in a column name, so each of them is changed to the character '.', hence you get the entire 'a....1.5' as the first column name, same goes for the second column.

    链接地址: http://www.djcxy.com/p/73850.html

    上一篇: WordPress gzinflate():调试时出现数据错误

    下一篇: 为什么数据框的列名使用=和<不同