chipsin
chipsin

Reputation: 675

Appending summary value to dataframe of a larger length

I have a large data frame with multiple cases for each group (ID). I also have a smaller data frame which contains a case for each group (ID). I need to append the description from the smaller data frame to each sample from the larger data frame with the same ID.

Below is an example dataset

ID <- c('ID1','ID1','ID2','ID2','ID2','ID3','ID3','ID3','ID3','ID4')
data <- c(1,2,6,2,4,3,8,9,6,2)
large.dataframe <- data.frame(ID, data)

    ID data
1  ID1    1
2  ID1    2
3  ID2    6
4  ID2    2
5  ID2    4
6  ID3    3
7  ID3    8
8  ID3    9
9  ID3    6
10 ID4    2

ID <- c('ID1','ID2','ID3','ID4')
description <- c('D','E','F','G')
small.dataframe <- data.frame(ID, description)

   ID description
1 ID1           D
2 ID2           E
3 ID3           F
4 ID4           G

Desired output is below:

    ID data description
1  ID1    1           D
2  ID1    2           D
3  ID2    6           E
4  ID2    2           E
5  ID2    4           E
6  ID3    3           F
7  ID3    8           F
8  ID3    9           F
9  ID3    6           F
10 ID4    2           G

Upvotes: 0

Views: 27

Answers (2)

B. Christian Kamgang
B. Christian Kamgang

Reputation: 6489

Here is the data.table approach:

library(data.table)
setDT(large.dataframe)[small.dataframe, description := i.description, on = "ID"]


#      ID data description
#  1: ID1    1           D
#  2: ID1    2           D
#  3: ID2    6           E
#  4: ID2    2           E
#  5: ID2    4           E
#  6: ID3    3           F
#  7: ID3    8           F
#  8: ID3    9           F
#  9: ID3    6           F
# 10: ID4    2           G

Upvotes: 1

Dij
Dij

Reputation: 1378

This can be a job for left_join.

left_join(large.dataframe, small.dataframe, by ="ID")

    ID data description
1  ID1    1           D
2  ID1    2           D
3  ID2    6           E
4  ID2    2           E
5  ID2    4           E
6  ID3    3           F
7  ID3    8           F
8  ID3    9           F
9  ID3    6           F
10 ID4    2           G

merge renders the same result

merge(large.dataframe, small.dataframe, by = "ID")

    ID data description
1  ID1    1           D
2  ID1    2           D
3  ID2    6           E
4  ID2    2           E
5  ID2    4           E
6  ID3    3           F
7  ID3    8           F
8  ID3    9           F
9  ID3    6           F
10 ID4    2           G

And to hard code it in base R using match

large.dataframe$description <- 
  small.dataframe$description[match(large.dataframe$ID, small.dataframe$ID)]

large.dataframe

    ID data description
1  ID1    1           D
2  ID1    2           D
3  ID2    6           E
4  ID2    2           E
5  ID2    4           E
6  ID3    3           F
7  ID3    8           F
8  ID3    9           F
9  ID3    6           F
10 ID4    2           G

Upvotes: 2

Related Questions