Walde - 1 month ago 10

R Question

I have two data frames:

`data <- data.frame(ID1 = c("D1","D1","D1","D1","D2","D2"),`

ID3 = c("A","A","D","D","B","C"),

Attribute = c("KL","KM","KL","KM","AB","AB"),

Value = c(2,2,3,3,5,7))

data_new <- data.frame(ID1 = c("D1","D1","D2","D2"),

ID2 = c("D1-1","D1-1","D2-1","D2-1"),

Attribute = c("KL","TA","AB","AA"))

Now I would like to incorporate the

`Values`

`ID3`

`expected_output <- data.frame(ID1 = c("D1","D1","D1","D1","D2","D2","D2","D2"),`

ID2 = c("D1-1","D1-1","D1-1","D1-1","D2-1","D2-1","D2-1","D2-1"),

ID3 = c("A","A","D","D","B","B","C","C"),

Attribute = c("KL","TA","KL","TA","AB","AA","AB","AA"),

Value = c(2,2,3,3,5,5,7,7))

Can anyone help me on this?

What I tried so far is a simple merge.

`mergeddata <- merge(data_new,data,`

by.x = c("ID1"),

by.y = c("ID1"))

The outcome looks promising for

`ID1 = D2`

`Attribute.x`

`ID = D1`

any help?

Answer

I think you could try the following:

```
mergeddata <- merge(data_new ,unique(data[, c("ID1", "ID3", "Value")]), by = "ID1")
```

Source (Stackoverflow)

Comments