R 将从矩阵创建的没有任何列和行名称的表转换为数据帧
我为我想要创建的矩阵编写了一个代码R 将从矩阵创建的没有任何列和行名称的表转换为数据帧,r,dataframe,R,Dataframe,我为我想要创建的矩阵编写了一个代码 tstp<-matrix(1:200, ncol = 4, byrow = TRUE) 我在这里介绍前四行。结果是这样的 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 如果它的类是数据帧,则它是我所需的输出。因此,我编写了一个代码,将其转换为数据帧,如下所示 > timestp<-data.frame(tstp) 这是我需要的课程 > class(timestp
tstp<-matrix(1:200, ncol = 4, byrow = TRUE)
我在这里介绍前四行。结果是这样的
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
如果它的类是数据帧,则它是我所需的输出。因此,我编写了一个代码,将其转换为数据帧,如下所示
> timestp<-data.frame(tstp)
这是我需要的课程
> class(timestp)
[1] "data.frame"
但我希望输出如下所示,带有数据类。frme
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
您可以这样做:
rownames(timestp) <- NULL
colnames(timestp) <- NULL
这将是输出:
> print(head(timestp), row.names = FALSE)
# 1 2 3 4
# 5 6 7 8
# 9 10 11 12
# 13 14 15 16
# 17 18 19 20
# 21 22 23 24
我们可以使用
as.data.frame
,并将可选
参数设置为TRUE
timestp <- as.data.frame(tstp, optional = TRUE)
colnames(df)
#NULL
timestp我的主要问题是删除列名。当他将其转换为数据时,生成了x1x2x3x4frame@MohsinWaqascolnames(timestp)
timestp<-data.frame(tstp, row.names = NULL)
print(timestp, row.names = FALSE)
> print(head(timestp), row.names = FALSE)
# 1 2 3 4
# 5 6 7 8
# 9 10 11 12
# 13 14 15 16
# 17 18 19 20
# 21 22 23 24
timestp <- as.data.frame(tstp, optional = TRUE)
colnames(df)
#NULL