[英]How to combine a list and a matrix into a dataframe
So I have a large data set about the dutch coast, where they divided the dutch coast in different area's and in these area there the have multiple transects along the beach where they measured the elevation every five meters. 因此,我有一个关于荷兰海岸的大型数据集,他们将荷兰海岸划分为不同区域,在这些区域中,沿着海滩有多个横断面,他们每5米测量一次海拔。 They named the different transect according to the distance from each other.
他们根据彼此之间的距离命名了不同的样带。
This data was in a netcdf file, but I finally got it in a very nice matrix that looks like this: 该数据位于netcdf文件中,但最终我得到了一个非常漂亮的矩阵,如下所示:
> a<-matrix(c(100,80,60,40,200,180,160,140),nrow=4,ncol=6)
> colnames(a) <- c(100,200,300,400,100,101)
> rownames(a) <- c(500,400,300,200)
> a
100 200 300 400 100 101
500 100 200 100 200 100 200
400 80 180 80 180 80 180
300 60 160 60 160 60 160
200 40 140 40 140 40 140
The column names are the different transect alongshore the beach and the row names are the cross shore distance. 列名称是沿海滩沿岸的不同横断面,行名称是跨岸距离。
However the "names" of the different transects are often the same for the different area's of the dutch coast. 但是,对于荷兰海岸的不同区域,不同样面的“名称”通常是相同的。 Now I have a list with the area's which I can put very nicely into the matrix:
现在,我有了一个包含该区域的列表,可以将其很好地放入矩阵中:
> b <- c(2,2,2,2,3,3)
> c<-rbind(b,a)
> c
100 200 300 400 100 101
b 2 2 2 2 3 3
500 100 200 100 200 100 200
400 80 180 80 180 80 180
300 60 160 60 160 60 160
200 40 140 40 140 40 140
However when I make a dataframe of this matrix, i get this: 但是,当我制作此矩阵的数据框时,我得到以下信息:
> d<-as.data.frame(as.table(c))
> d
Var1 Var2 Freq
1 b 100 2
2 500 100 100
3 400 100 80
4 300 100 60
5 200 100 40
6 b 200 2
7 500 200 200
8 400 200 180
9 300 200 160
10 200 200 140
11 b 300 2
12 500 300 100
13 400 300 80
14 300 300 60
15 200 300 40
16 b 400 2
17 500 400 200
18 400 400 180
19 300 400 160
20 200 400 140
21 b 100 3
22 500 100 100
23 400 100 80
24 300 100 60
25 200 100 40
26 b 101 3
27 500 101 200
28 400 101 180
29 300 101 160
30 200 101 140
And what I want is that the b (so the different coastal area's) are a extra column. 我想要的是b(所以是不同的沿海地区)是一栏。
So that it will look like this: 这样它将看起来像这样:
Var1 Var2 Freq var3 Var1 Var2频率var3
2 500 100 100 2
3 400 100 80 2
4 300 100 60 2
5 200 100 40 2
7 500 200 200 2
8 400 200 180 2
9 300 200 160 2
10 200 200 140 2
12 500 300 100 2
13 400 300 80 2
14 300 300 60 2
15 200 300 40 2
17 500 400 200 2
18 400 400 180 2
19 300 400 160 2
20 200 400 140 2
22 500 100 100 3
23 400 100 80 3
24 300 100 60 3
25 200 100 40 3
27 500 101 200 3
28 400 101 180 3
29 300 101 160 3
30 200 101 140 3
I am not really sure how to do this, this data comes from a netcdf file and maybe I did not transferred that in the right way... Furthermore my data set is of course much larger, and this is just a small sample how it looks like. 我不太确定如何执行此操作,该数据来自netcdf文件,也许我没有以正确的方式进行传输...此外,我的数据集当然更大,这只是一个小示例看起来像。 If I can give more information, just say it.
如果我能提供更多信息,请说出来。
a
and b
contain different data types, so you shouldn't combine them in a matrix. a
和b
包含不同的数据类型,因此您不应将它们组合成矩阵。 Keeping them separate will also make your code simpler. 将它们分开会使代码更简单。
You can convert a
from wide form to long form using melt
from reshape2
. 你可以转换
a
从形式广泛使用长型melt
从reshape2
。
library(reshape2)
cbind(melt(a), rep(b, each = nrow(a)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.