我对R非常陌生,来自更多的python背景。我正在一个需要使用R的项目中,结果几乎完成了,但是在最后一个问题上迷迷糊糊。
我在R中编写了一个函数,该函数以绝对糟糕的方式为我提供了所要查找的输出。
我想将其放入R数据框中,以comp_count,方差,execution_time和准确性作为列。我还需要删除空值。有什么建议吗?
编辑:要求输出dput(结果)。
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))
我可以通过以下方式访问单独的组件:
results[[2]]
$comp_count
[1] 2
$variance
[1] 32.6
$execution_time
[1] 2.15086
$accuracy
[1] 0.7827798
解决此问题的最佳方法是什么?感谢所有帮助!
答案 0 :(得分:2)
不幸的是,您没有以易于复制和粘贴的格式提供示例数据;对于以后的帖子,最好使用successCallback()
,尤其是当数据结构是问题描述的关键组成部分时。
我将生成一些(希望)具有代表性且最少的样本数据
dput
然后您可以使用
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)
)
将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
元素行绑定到list
中。
请注意,这要求所有(非{data.frame
)NULL
个元素都具有相同数量的元素。
另一种选择是使用list
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
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