Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将从矩阵创建的没有任何列和行名称的表转换为数据帧_R_Dataframe - Fatal编程技术网

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@MohsinWaqas
colnames(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