简体   繁体   English

将列表类型转换为R,data.frame和as.data.frame中的数据帧类型不起作用

[英]convert list type to data frame type in R, data.frame and as.data.frame does not wok

I have created A with command data.frame in R, but the data type of A is "list", and I have already tried to convert A to data frame with as.data.frame but it does work, anyone else has same experience? 我已经在R中使用命令data.frame创建了A,但是A的数据类型是“列表”,并且我已经尝试过使用as.data.frame将A转换为数据帧,但是它确实有效,其他任何人都有相同的经验? the code are below: 代码如下:

A <- data.frame(rep(1,5), row.names=c("A","B","C","D","E"), check.rows = FALSE, check.names = FALSE)
mode(A)
A1 <- as.data.frame(A)
mode(A1)

As data.frame is a list with length of each list element ( column ) same, the mode returns a list . 由于data.frame是一个list ,每个list元素( column )的长度相同,因此该mode返回一个list We can use 我们可以用

class(A)
#[1] "data.frame"

and

is.data.frame(A)
#[1] TRUE

from @RHertel's commments 来自@RHertel的评论

is.list(A)  
#[1] TRUE

or 要么

str(A)
#'data.frame':   5 obs. of  1 variable:
# $ rep(1, 5): num  1 1 1 1 1

dput(A)

to confirm that a data.frame is also a list . 确认data.frame也是一个list

But, list also can have equal length and not a data.frame 但是, list也可以具有相等的长度,而不是data.frame

l1 <- as.list(1:5)
mode(l1)
#[1] "list"
class(l1)
#[1] "list"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM