I have this:
library(tidyr)
haves <- data.frame(
model1_prediction = c(1, 2)
, model2_prediction = c(3, 4)
, model3_prediction = c(5, 6)
, actuals = c(99.1, 99.2)
)
model1_prediction model2_prediction model3_prediction actuals
1 3 5 99.1
2 4 6 99.2
I would like to obtain this:
wants <- data.frame(
long = c(1, 2, 3, 4, 5, 6)
, actuals = c(99.1, 99.2, 99.1, 99.2, 99.1, 99.2)
)
wants
long actuals
1 99.1
2 99.2
3 99.1
4 99.2
5 99.1
6 99.2
My long wounded working attempt is the following but wonder if there is a better way? Thanks.
t1 <- haves %>%
select(
model1_prediction
, model2_prediction
, model3_prediction
) %>%
mutate(
id = row_number()
) %>%
gather(
key, long, -id
)
t1
t2 <- haves %>%
select(
actuals
) %>%
mutate(
id = row_number()
) %>%
gather(
key, actuals, - id
)
t2
my_longwounded_wants <- t1 %>%
inner_join(t2, by = c("id" = "id")) %>%
select(
long
, actuals
)
my_longwounded_wants