0
$\begingroup$

Having a dataset like this:

What model change I use to show positive and negative transitions between the the 5 variables and correlate them?

dframe <- structure(list(id = c(14768L, 18180L), col1 = c(-0.6084, -0.3227
), col2 = c(-1.4887, -1.1797), col3 = c(3.8402, 3.0491), col4 = c(-1.8265, 
-1.3248), col5 = c(0.4078, 0.7862), col1_new = c(-0.4582, -0.2094
), col2_new = c(-1.3878, -1.5926), col3_new = c(3.3112, 3.2756
), col4_new = c(-1.6242, -1.2361), col5_new = c(0.5014, 0.5925
)), class = "data.frame", row.names = c(NA, -2L))

and from here

library(data.table)
library(tidyverse)

setDT(dframe)

# take each pair of original and new values, and move each pair from columns 
# to their own row (with two columns, "current" and "new"
dframe_new <- melt(dframe, id.var = "id", measure = patterns("\\d$", "new$"), 
    value.name = c("current", "new")) %>% 
mutate(
    diff = new-current, 
    Change = case_when(
        diff > 0 ~ "Increase",
        diff == 0 ~ "No Change",
        TRUE ~ "Decrease"
    )
)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.