Variable lengths differ error when using lm in R
<p>I am trying to run a linear model on time and a monthly factor, however I am getting the error:</p>

<pre><code>Error in model.frame.default(formula = ts.data ~ time2 + factor(month2),  : 
  variable lengths differ (found for 'time2')
</code></pre>

<p>This is how I created the variables:</p>

<pre><code>time2&lt;-seq(along=ts.data)
month2&lt;-rep(1:12,length=length(ts.data))
</code></pre>

<p>However running length(time2), length(month2) and length(ts.data) gives the same number, does anyone know how I fix the error?</p>

<p>Trying to run lm :</p>

<pre><code>lm(ts.data~time2+factor(month2))
</code></pre>

<p>The data I am using:</p>

<pre><code>structure(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 
64, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 78238, 73928, 70708, 75175, 70744, 65604, 61227, 62635, 
47652, 51507, 81874, 98236, 99401, 94756, 94697, 93732, 100334, 
139355, 88575, 94169, 86084, 98249, 95321, 87822, 80256, 81875, 
86293, 80712, 79533, 82847, 84498, 84185, 78382, 82701, 80491, 
91140, 86847, 96727, 101295, 99450, 87783, 101246, 97913, 100081, 
96346, 93608, 90648, 99105, 90920, 84960, 82591, 88090, 89980, 
87778, 87429, 81898, 77285, 80369, 73193, 65139, 60126, 57219, 
94204, 112472, 157199, 154791.5, 154294.88, 161920.63, 147408.75, 
134418, 132158.5, 104572.5, 96831, 91045.88, 141182.63, 214759.25, 
216647, 184598.38, 210794.38, 182403.75, 193001.63, 176807.38, 
186552.63, 201375.88, 181861.25, 193234.88, 187240.25, 168242, 
172475.13, 188996.25, 179663.88, 192861.63, 187461.25, 188670.5, 
198826.25, 208696.5, 180490.75, 202265.88, 187966.13, 203342.13, 
194850.38, 230582.63, 212517, 223432.5, 196511.63, 229582.25, 
206120.63, 225629.88, 209769.63, 210797.63, 213215.75, 215144.88, 
223266, 230747.63, 228573, 223828.88, 202102.88, 192863.63, 206675.13, 
195647.5, 173897.25, 183788.88, 158511.38, 138559, 114163.25, 
110399.13, 164751.13, 270772, 90430, 81719, 79183, 85428, 79372, 
72361, 66207, 55403, 51693, 60280, 98698, 123059, 121550, 107662, 
107863, 107630, 114685, 169659, 100104, 107598, 97728, 112850, 
107784, 97580, 92709, 99098, 99482, 100543, 98856, 106081, 108248, 
104769, 96966, 100093, 103107, 114944, 108001, 126289, 135213, 
129717, 121688, 134421, 127318, 127412, 121922, 119045, 116989, 
126286, 116707, 106627, 98219, 111225, 117279, 113725, 114633, 
100633, 95478, 98394, 87616, 75329, 68274, 70658, 122995, 145224, 
155833, 131896.5, 138340.63, 145610, 130653.13, 122562.75, 115850.5, 
91749.88, 81787.13, 85457.5, 142931.63, 214970, 216836.63, 175902, 
180757.88, 175233.63, 168982.13, 168727.25, 173501, 182731.38, 
152260.63, 182607, 179326.5, 157693.13, 161004.75, 172990.5, 
166204.38, 175172.63, 186446.13, 202645.38, 202500.25, 204148.38, 
187763.5, 207269.75, 183334.88, 206552.5, 207270.13, 226123.88, 
239037.88, 214656.38, 216552.75, 231406.75, 207365.63, 217873.5, 
200308.88, 201696.5, 208984.75, 227723.38, 212083.25, 206262.38, 
186596.25, 215496.63, 199399, 184933.25, 195925.63, 190318.63, 
170375.38, 171624.13, 154537.13, 133532.25, 119179.13, 113297.88, 
174946.5, 304690, 108567, 99358, 97299, 103628, 96936, 89254, 
83761, 72058, 66685, 74491, 117292, 139878, 139585, 130180, 130079, 
127562, 136152, 197149, 118619, 127875, 118094, 134989, 130688, 
121475, 112367, 114805, 117087, 118526, 118038, 123988, 127511, 
125790, 116702, 123049, 124260, 141232, 133809, 156349, 162637, 
158367, 144491, 164389, 155305, 161401, 151829, 144188, 142702, 
156405, 141937, 129857, 120318, 132823, 138201, 135058, 129275, 
119897, 112924, 120385, 108134, 94062, 86695, 88434, 145426, 
167100, 184196.63, 166628.38, 168193.13, 190280.88, 154984.75, 
153784.38, 148033.75, 121304.25, 107303.25, 108003.13, 168770, 
240983.75, 242817.13, 220229.38, 222805.75, 205068.75, 205204.25, 
192598.25, 206565.38, 227284.88, 199258.25, 227122.88, 209076.13, 
194855.5, 196357.25, 206865.88, 209580.13, 222190.88, 234610.88, 
229339.13, 219321.63, 232571.75, 218584.75, 246116.38, 229563, 
256776.75, 257335.25, 271507, 272014, 265850.5, 253426.63, 291759.63, 
262608.88, 279417.25, 264583.25, 256634.88, 271024.88, 283927.13, 
270597.38, 264222.5, 235009.13, 258379.25, 246485.5, 240163.25, 
238369.88, 240961.5, 219826.75, 212077.5, 194937, 166299, 141284.88, 
130153.38, 206775, 342062.88), .Dim = c(64L, 8L), .Dimnames = list(
    NULL, c("Week_Number", "Campaign_Period", "Control_Traffic", 
    "Control_Revenue", "VOD_Test_Traffic", "VOD_Test_Revenue", 
    "TV_Test_Traffic", "TV_Test_revenue")), .Tsp = c(1, 2.21153846153846, 
52), class = c("mts", "ts", "matrix"))
</code></pre>