gpt4 book ai didi

r - 将单行表转换为 data.frame

转载 作者:行者123 更新时间:2023-12-01 23:01:35 24 4
gpt4 key购买 nike

考虑一张表:

ta <- structure(c(`0` = 1L, `2` = 3L, `4` = 4L, `5` = 2L), .Dim = 4L, .Dimnames = list(
x = c("0", "2", "4", "5")), class = "table")

#> ta
# x
# 0 2 4 5
# 1 3 4 2

对于简单的表格,常规方式(as.data.frame.matrix)似乎不起作用:

> as.data.frame.matrix(ta) 
Error in d[[2L]] : subscript out of bounds

那么,有没有一种简单的方法可以将只有一行的表格转换为数据框?到目前为止,我想到的唯一方法是

as.data.frame(t(as.data.frame.array(ta)))
0 2 4 5
ta 1 3 4 2

有没有更简单的方法?

最佳答案

或者另一种选择是将 rbinddata.frame 一起使用:

data.frame(rbind(ta), check.names = FALSE)

# 0 2 4 5
#ta 1 3 4 2

关于r - 将单行表转换为 data.frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71720059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com