问题
Given a long format data set like this:
ID_2<-c('A','A','A','B','B','C','E','E','F','F','H','H','H')
Type<-c('Blk','Wht','Gre','Blk','Wht','Blk','Blk','Wht','Blk','Wht','Wht','Blk','Gre')
Count<-c(1,2,2,1,2,1,2,1,2,1,2,1,2)
DF2<-data.frame(ID_2,Type,Count)
I would like to add a specific set of metadata for each unique ID (ID_2). The metadata would be found in a separate data frame like so:
Year<-c(2005,2005,2006,2006,2007,2008,2008,2008)
Location<-c('EAST','EAST','WEST','WEST','NORTH','EAST','EAST','EAST')
Site<-c(1,2,3,4,5,6,7,8)
ID_1<-c('A','B','C','NAN','E','F','NAN','H')
DF1<-data.frame(Year,Location,Site,ID_1)
I would like to add the metadata from DF1 to the long format of DF2 (matching up ID_1 and ID_2), so that each row of DF2 contains the proper metadata from DF1
I also need to deal with the blank locations, so that any unique site number from DF_1 that does not have a corresponding data entry in DF_2 gets a flagged entry. The end result would look like this:
Year Location Site ID Type Count
2005 EAST 1 A Blk 1
2005 EAST 1 A Wht 2
2005 EAST 1 A Gre 2
2005 EAST 2 B Blk 1
2006 WEST 3 C Blk 1
2007 NORTH 5 E Blk 2
2007 NORTH 5 E Wht 1
2008 EAST 6 F Blk 2
2008 EAST 6 F Wht 1
2008 EAST 8 H Wht 2
2008 EAST 8 H Blk 1
2008 EAST 8 H Gre 2
2006 WEST 4 Flag Flag -999
2008 EAST 7 Flag Flag -999
回答1:
This appears to be a simple merge()
> merge(DF1, DF2, by.x = "ID_1", by.y = "ID_2", all = TRUE)
ID_1 Year Location Site Type Count
1 A 2005 EAST 1 Blk 1
2 A 2005 EAST 1 Wht 2
3 A 2005 EAST 1 Gre 2
4 B 2005 EAST 2 Blk 1
5 B 2005 EAST 2 Wht 2
6 C 2006 WEST 3 Blk 1
7 E 2007 NORTH 5 Blk 2
8 E 2007 NORTH 5 Wht 1
9 F 2008 EAST 6 Blk 2
10 F 2008 EAST 6 Wht 1
11 H 2008 EAST 8 Wht 2
12 H 2008 EAST 8 Blk 1
13 H 2008 EAST 8 Gre 2
14 NAN 2006 WEST 4 <NA> NA
15 NAN 2008 EAST 7 <NA> NA
You'll have to do a little bit of extra work to replace the NA
values with whatever you actually want to use.
来源:https://stackoverflow.com/questions/18001095/adding-identifer-metadata-columns-to-a-long-format-data-set