Reputation: 85
My Dataframe looks similar to this
Date Key Value1 Value2
D1 1A 2
D1 2A 1 2
D2 3B 1 3
D2 1A 3
D3 2A 5
As you can see, the Key
itself ain't primary, However if we consider Date
and Key
together, each observation finds it as a primary key.
Now I wish to achieve that for every observation where Value1
and Value2
both are present, duplicate the row and break them into two rows. Something like this:
Date Key Value1 Value2 Value
D1 1A 2 2
D1 2A 1 2 1
D1 2A 1 2 2
D2 3B 1 3 1
D2 3B 1 3 3
D3 2A 5 5
Is there anyway I can achieve this? Thanks in advance.
Upvotes: 0
Views: 140
Reputation: 47320
You could also use unnest
library(tidyverse)
df %>%
rowwise %>%
mutate(Value3 = ifelse(is.na(Value2),list(Value1),list(c(Value1,Value2)))) %>%
unnest
# # A tibble: 7 x 5
# Date Key Value1 Value2 Value3
# <chr> <chr> <int> <int> <int>
# 1 D1 1A 2 NA 2
# 2 D1 2A 1 2 1
# 3 D1 2A 1 2 2
# 4 D2 3B 1 3 1
# 5 D2 3B 1 3 3
# 6 D2 1A 3 NA 3
# 7 D3 2A 5 NA 5
data
df <- read.table(text="
Date Key Value1 Value2
D1 1A 2 NA
D1 2A 1 2
D2 3B 1 3
D2 1A 3 NA
D3 2A 5 NA",h=T,strin=F
)
Upvotes: 0
Reputation: 11480
You probably want this:
df %>% gather(keys,Value,-Date,-Key) %>% filter(!is.na(Value)) %>% arrange(Date,Key)
Date Key keys Value
1 D1 1A Value1 2
2 D1 2A Value1 1
3 D1 2A Value2 2
4 D2 1A Value1 3
5 D2 3B Value1 1
6 D2 3B Value2 3
7 D3 2A Value1 5
Upvotes: 1