One liner wanted: Create data frame and give colnames: R data.frame(…, colnames = c(“a”, “b”, “c”))

后端 未结 2 2010
别跟我提以往
别跟我提以往 2020-12-11 08:01

Is there an easier (i.e. one line of code instead of two!) way to do the following:

results <- as.data.frame(str_split_fixed(c(\"SampleID_someusefulinfo.c         


        
2条回答
  •  [愿得一人]
    2020-12-11 08:29

    Use setNames() around a data.frame

    setNames(data.frame(matrix(c(1,2,3,4),nrow=2,ncol=2)), c("a","b"))
    #  a b
    #1 1 3
    #2 2 4
    

    ?setNames:

    a convenience function that sets the names on an object and returns the object

    > setNames
    function (object = nm, nm) 
    {
        names(object) <- nm
        object
    }
    

提交回复
热议问题