简体   繁体   English

在R中转换数据格式

[英]Convert data format in R

I have a data set, dat, which was got from a model run. 我有一个数据集dat,它是从模型运行中获得的。

The head of the dataset looks like this: 数据集的头部如下所示:

[[1]]

[1] -1

[[2]]

[2] -2

[[3]]

[3] -1

[[4]]

[4] 0

[[5]]

[5] -6

[[6]]

[6] -7

How can I convert dat to a simple data frame with a single column like this 我如何将dat转换为具有这样的单列的简单数据框

-1
-2
-1
0
-6
-7

Thanks 谢谢

Dan

You have something like this: 你有这样的事情:

L <- as.list(1:10)
L

So, one way is to: 因此,一种方法是:

> data.frame(name = t(data.frame(L)))
     name
X1L     1
X2L     2
X3L     3
X4L     4
X5L     5
X6L     6
X7L     7
X8L     8
X9L     9
X10L   10

Replace name with whatever you want the name of the variable to be. name替换name您想要的变量名称。

You probably want to use the unlist function. 您可能要使用unlist功能。 For example: 例如:

unlist(list(1,2,3,4,5))
[1] 1 2 3 4 5

And you can turn it into a column by cbind ing the results 您可以通过cbind结果将其变成一列

a = unlist(list(1,2,3,4,5))
> cbind(a)
     a
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5

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

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