@@ -71,3 +71,65 @@ test_that("convert2mvcfd works with stateColumns", {
71
71
expect_equal(out , expectedOut )
72
72
})
73
73
74
+ test_that(" convertMvcfd2cfd works" , {
75
+ # 2 columns
76
+ x <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state1 = c(1 , 2 , 1 ), state2 = c(2 , 1 , 3 ))
77
+ expectedOut <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state = as.factor(c(" 1_2" , " 2_1" , " 1_3" )))
78
+
79
+ out <- convertMvcfd2cfd(x )
80
+ expect_equal(out , expectedOut )
81
+
82
+ # > 2 columns
83
+ x <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state1 = c(1 , 2 , 1 ), state2 = c(2 , 1 , 3 ), state3 = c(" a" , " b" , " c" ))
84
+ expectedOut <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state = as.factor(c(" 1_2_a" , " 2_1_b" , " 1_3_c" )))
85
+
86
+ out <- convertMvcfd2cfd(x )
87
+ expect_equal(out , expectedOut )
88
+ })
89
+
90
+ test_that(" convertMvcfd2cfd works with custom sep" , {
91
+ x <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state1 = c(1 , 2 , 1 ), state2 = c(2 , 1 , 3 ))
92
+ expectedOut <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state = as.factor(c(" 1 & 2" , " 2 & 1" , " 1 & 3" )))
93
+
94
+ out <- convertMvcfd2cfd(x , sep = " & " )
95
+ expect_equal(out , expectedOut )
96
+ })
97
+
98
+ test_that(" convertMvcfd2cfd works with custom stateColumns" , {
99
+ x <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state1 = c(1 , 2 , 1 ), state2 = c(2 , 1 , 3 ))
100
+ expectedOut <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state = as.factor(c(" 1 & 2" , " 2 & 1" , " 1 & 3" )))
101
+
102
+ out <- convertMvcfd2cfd(x , sep = " & " )
103
+ expect_equal(out , expectedOut )
104
+ })
105
+
106
+ test_that(" convertMvcfd2cfd throws error with bad input" , {
107
+ x1 <- data.frame (id1 = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ))
108
+
109
+ expect_error(convertMvcfd2cfd(2 ), regexp = " data must be a data.frame" )
110
+ expect_error(convertMvcfd2cfd(x1 ), regexp = " Missing columns in data: id." )
111
+ })
112
+
113
+ test_that(" convertListCfd2Cfd works" , {
114
+ x1 <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state = c(1 , 2 , 1 ))
115
+ x2 <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.25 , 0.9 ), state = c(1 , 2 , 3 ))
116
+ x <- list (x1 , x2 )
117
+
118
+ expectedOut <- data.frame (
119
+ id = rep(1 , 5 ), time = c(0 , 0.25 , 0.5 , 0.9 , 1 ), state = as.factor(c(" 1_1" , " 1_2" , " 2_2" , " 2_3" , " 1_3" ))
120
+ )
121
+ out <- convertListCfd2Cfd(x )
122
+ expect_equal(out , expectedOut )
123
+ })
124
+
125
+ test_that(" convertListCfd2Cfd works with custom sep" , {
126
+ x1 <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.5 , 1 ), state = c(1 , 2 , 1 ))
127
+ x2 <- data.frame (id = c(1 , 1 , 1 ), time = c(0 , 0.25 , 0.9 ), state = c(1 , 2 , 3 ))
128
+ x <- list (x1 , x2 )
129
+
130
+ expectedOut <- data.frame (
131
+ id = rep(1 , 5 ), time = c(0 , 0.25 , 0.5 , 0.9 , 1 ), state = as.factor(c(" 1 & 1" , " 1 & 2" , " 2 & 2" , " 2 & 3" , " 1 & 3" ))
132
+ )
133
+ out <- convertListCfd2Cfd(x , sep = " & " )
134
+ expect_equal(out , expectedOut )
135
+ })
0 commit comments