dataframe - Pass the additional argument "nrow" (no. of rows) to the as.data.frame function in R? -


(reproducible example given) how pass additional argument nrow as.data.frame in r?

in ?as.data.frame, given:
as.data.frame(x, row.names = null, optional = false, ...)
... additional arguments passed or methods.

with co-worker matrix(..., nrow), is:

set.seed(1) df <- as.data.frame(matrix(c(rnorm(5),rnorm(5), rnorm(5)), nrow=5, byrow=true)) df #           v1         v2         v3 # 1 -0.6264538  0.1836433 -0.8356286 # 2  1.5952808  0.3295078 -0.8204684 # 3  0.4874291  0.7383247  0.5757814 # 4 -0.3053884  1.5117812  0.3898432 # 5 -0.6212406 -2.2146999  1.1249309 

without matrix(..., nrow) simulator, is:

set.seed(1) df <- as.data.frame(c(rnorm(5),rnorm(5), rnorm(5))) df #   c(rnorm(5), rnorm(5), rnorm(5)) # 1                       -0.6264538  # 2                        0.1836433 # .................................. # 15                       1.1249309 

i want pass nrow argument as.data.frame replace job of matrix(...,nrow). file of as.data.frame seems achievable. how?

c(rnorm(5),rnorm(5), rnorm(5)) vector. (and, btw, simpler write rnorm(15).) when call as.data.frame on vector, s3 dispatch end using as.data.frame.vector. question assumes internally as.data.frame.vector converts input matrix before putting data frame. this incorrect assumption.

because as.data.frame.vector ever called on single vector, knows has 1 column deal has relatively simple job. can @ code typing as.data.frame.vector , see no matrices used , that, in method, ... not used in function body.

you have code works, as.data.frame(matrix(your_vector, nrow = your_nrow)). it's solution. content.

it makes sense matrix or as.matrix have nrow argument because elements of matrix must have same type. common vector (in elements must have same type) gets turned matrix rows , columns. data.frame allows each column of different types, "wrapping" input data 1 column next unusual - it's not assumed next column continuation of previous. given example, it's worth asking if want data frame - computations matrices faster simpler data structure.


there many ways create data frame want. following work (only column names differ, data values same). how generate input vector you.

set.seed(1) d1 = as.data.frame(matrix(rnorm(15), nrow = 5))  set.seed(1) d2 = data.frame(replicate(3, rnorm(5)))  set.seed(1) d3 = data.frame(rnorm(5), rnorm(5), rnorm(5))  set.seed(1) my_vectors = list(rnorm(5), rnorm(5), rnorm(5)) d4 = as.data.frame(do.call(cbind, my_vectors)) 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -