1

I have executed the following script:

> results <- list()
> results$gp1 <- c(19, 18.5, 12)
> results$gp1 <- c(19, 17, 15)
> results$gp2 <- c(19, 18.5, 12)
> results$gp3 <- c(20, 19.5, 18)
> results
$`gp1`
[1] 19 17 15

$gp2
[1] 19.0 18.5 12.0

$gp3
[1] 20.0 19.5 18.0

> df_res <- data.frame(results)

> df_res_a <- as.data.frame(results)

> str(df_res)
'data.frame':   3 obs. of  3 variables:
 $ gp1: num  19 17 15
 $ gp2: num  19 18.5 12
 $ gp3: num  20 19.5 18

> str(df_res_a)
'data.frame':   3 obs. of  3 variables:
 $ gp1: num  19 17 15
 $ gp2: num  19 18.5 12
 $ gp3: num  20 19.5 18

It seems like both as.data.frame(x) and data.frame(x) work similarly when coercing the list into data frame. So which method should be used? Especially in case of large data volumes?

ProgramSpree
  • 372
  • 5
  • 21
  • 1
    For large dataset, try `data.table::setDF`. – mt1022 Aug 21 '18 at 06:46
  • @Tim Biegeleisen Thanks for the link ... but the only answer to my particular question I got from those answers is that `as.data.frame(x)` is faster. I want to know any other _reasons I should use the above method rather than `data.frame(x)` for coercing R objects into data frames_. I also want to know _why_ the former method is faster. – ProgramSpree Aug 21 '18 at 11:21
  • 1
    Fine, I reopened the question, under the assumption that you read the duplicate link end to end and really did not get an answer to your question. – Tim Biegeleisen Aug 21 '18 at 11:23

0 Answers0