I am very new to R, and come from more of a python background. I am working on a project that requires using R and am nearly finished with the results, but have stumbled on this final issue.
I wrote a function in R that gave me the output I was looking for, but in an absolutely abysmal way.
I would like to put this into an R dataframe, with comp_count, variance, execution_time, and accuracy as columns. I would need to drop the null values as well. Any advice?
Edit: output of dput(results) asked for.
list(NULL, list(comp_count = 2L, variance = 32.6, execution_time = 2.15086028575897,
accuracy = 0.782779827798278), list(comp_count = 3L, variance = 43.3,
execution_time = 2.671033902963, accuracy = 0.832069775243207),
list(comp_count = 4L, variance = 52, execution_time = 3.50883383353551,
accuracy = 0.839405121323941), list(comp_count = 5L,
variance = 58.6, execution_time = 3.92320603132248, accuracy = 0.842938611204294),
list(comp_count = 6L, variance = 64.3, execution_time = 4.39138143459956,
accuracy = 0.843139885944314), list(comp_count = 7L,
variance = 69.2, execution_time = 5.430861667792, accuracy = 0.843609527004361),
list(comp_count = 8L, variance = 73.7, execution_time = 6.1574295481046,
accuracy = 0.844459353684446), list(comp_count = 9L,
variance = 77.1, execution_time = 6.21873023509979, accuracy = 0.845219724924522),
list(comp_count = 10L, variance = 80.5, execution_time = 7.65496598482132,
accuracy = 0.846069551604607), list(comp_count = 11L,
variance = 83.8, execution_time = 6.71301571528117, accuracy = 0.846024823884602),
list(comp_count = 12L, variance = 87, execution_time = 8.45073886712392,
accuracy = 0.846740467404674), list(comp_count = 13L,
variance = 89.9, execution_time = 9.64280251661936, accuracy = 0.846293190204629),
list(comp_count = 14L, variance = 92.5, execution_time = 9.80074710051219,
accuracy = 0.846382645644638), list(comp_count = 15L,
variance = 94.7, execution_time = 10.3888318975766, accuracy = 0.846091915464609),
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, list(comp_count = 30L, variance = 100,
execution_time = 20.8339449683825, accuracy = 0.8589958626859))
I can access separate components by this:
results[[2]]
$comp_count
[1] 2
$variance
[1] 32.6
$execution_time
[1] 2.15086
$accuracy
[1] 0.7827798
What is the best way to go about this? All help is greatly appreciated!
Unfortunately you don't provide sample data in an easily copy&paste-able format; for future posts it's better to use dput
, especially when the data structure is a critical component of your problem description.
I will generate some (hopefully) representative and minimal sample data
lst <- list(
NULL,
list(comp_count = 2, variance = 32.6, execution_time = 2.15, accuracy = 0.78),
list(comp_count = 3, variance = 43.3, execution_time = 2.67, accuracy = 0.83)
)
Then you can use
do.call(rbind.data.frame, lst)
# comp_count variance execution_time accuracy
#2 2 32.6 2.15 0.78
#21 3 43.3 2.67 0.83
to row-bind list
elements into a data.frame
.
Note that this requires all (non- NULL
) list
elements to have the same number of elements.
Another option is to use dplyr::bind_rows
library(dplyr)
bind_rows(lst)
## A tibble: 2 x 4
# comp_count variance execution_time accuracy
# <dbl> <dbl> <dbl> <dbl>
#1 2 32.6 2.15 0.78
#2 3 43.3 2.67 0.83
With your updated sample data
do.call(rbind.data.frame, lst)
# comp_count variance execution_time accuracy
#2 2 32.6 2.150860 0.7827798
#21 3 43.3 2.671034 0.8320698
#3 4 52.0 3.508834 0.8394051
#4 5 58.6 3.923206 0.8429386
#5 6 64.3 4.391381 0.8431399
#6 7 69.2 5.430862 0.8436095
#7 8 73.7 6.157430 0.8444594
#8 9 77.1 6.218730 0.8452197
#9 10 80.5 7.654966 0.8460696
#10 11 83.8 6.713016 0.8460248
#11 12 87.0 8.450739 0.8467405
#12 13 89.9 9.642803 0.8462932
#13 14 92.5 9.800747 0.8463826
#14 15 94.7 10.388832 0.8460919
#15 30 100.0 20.833945 0.8589959
lst <- list(NULL, list(comp_count = 2L, variance = 32.6, execution_time = 2.15086028575897,
accuracy = 0.782779827798278), list(comp_count = 3L, variance = 43.3,
execution_time = 2.671033902963, accuracy = 0.832069775243207),
list(comp_count = 4L, variance = 52, execution_time = 3.50883383353551,
accuracy = 0.839405121323941), list(comp_count = 5L,
variance = 58.6, execution_time = 3.92320603132248, accuracy = 0.842938611204294),
list(comp_count = 6L, variance = 64.3, execution_time = 4.39138143459956,
accuracy = 0.843139885944314), list(comp_count = 7L,
variance = 69.2, execution_time = 5.430861667792, accuracy = 0.843609527004361),
list(comp_count = 8L, variance = 73.7, execution_time = 6.1574295481046,
accuracy = 0.844459353684446), list(comp_count = 9L,
variance = 77.1, execution_time = 6.21873023509979, accuracy = 0.845219724924522),
list(comp_count = 10L, variance = 80.5, execution_time = 7.65496598482132,
accuracy = 0.846069551604607), list(comp_count = 11L,
variance = 83.8, execution_time = 6.71301571528117, accuracy = 0.846024823884602),
list(comp_count = 12L, variance = 87, execution_time = 8.45073886712392,
accuracy = 0.846740467404674), list(comp_count = 13L,
variance = 89.9, execution_time = 9.64280251661936, accuracy = 0.846293190204629),
list(comp_count = 14L, variance = 92.5, execution_time = 9.80074710051219,
accuracy = 0.846382645644638), list(comp_count = 15L,
variance = 94.7, execution_time = 10.3888318975766, accuracy = 0.846091915464609),
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, list(comp_count = 30L, variance = 100,
execution_time = 20.8339449683825, accuracy = 0.8589958626859))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.