简体   繁体   中英

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. 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:

> 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.

So that it will look like this:

Var1 Var2 Freq 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. 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. Keeping them separate will also make your code simpler.

You can convert a from wide form to long form using melt from reshape2 .

library(reshape2)
cbind(melt(a), rep(b, each = nrow(a)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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