r - Assign names to data frame with as.data.frame function -


i trying convert matrix data frame, , assign names in 1 line.

as used ?as.data.frame there parameter called col.names doesn't seem work me, doing wrong?

as.data.frame(matrix(c(1:4), nrow=2), col.names=c("a","b")) 

output:

   v1 v2   1  1  3   2  2  4   

expected output:

    b   1  1  3   2  2  4   

i know can assign later `colnames(matrix) = c("a,"b), wondering if possible in 1 line. (

we can use dimnames argument in matrix , return column names such when converting data.frame

as.data.frame(matrix(1:4, nrow=2, dimnames = list(null, c("a", "b")))) #  b #1 1 3 #2 2 4 

while in op's code, matrix output didn't have column names, as.data.frame creates column names 'v1', 'v2' default. col.names argument not as.data.frame class matrix, didn't have effect

if quote documentation of ?as.data.frame

s3 method class 'matrix'

as.data.frame(x, row.names = null, optional = false, ..., stringsasfactors = default.stringsasfactors())


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -