I am hoping for some assistance with a problem that has puzzled me. Immediately below is the error messages I obtained when I tried to perform two functions. (A) tapply(outcome,na.rm=T,grp,mean) Error in tapply(outcome, na.rm = T, grp, mean) : arguments must have same length +++++++++++++++++++++++++++++++++++++++++++++++ (B) library(nlme) > anova(lme(outcome ~ grp * time, random = ~ 1 | subject)) Error in model.frame(formula, rownames, variables, varnames, extras, extranames, : variable lengths differ I assumed this was due to a missing value, as all the variables appeared to have the same number of values . However, when I ran the length command this wasn't the case: length(outcome) [1] 368 > length(grp) [1] 184 > length(subject) [1] 92 > length(time) [1] 184 Below is the syntax I have been using and the date frame that I generated - study1dat <- read.csv("c:\\study1rb.csv",header=T) attach (study1dat) outcome <- c(t1freq, t2freq,t3freq,t4freq) grp <- factor( rep(group, 2) ) time <- gl(4, 46) subject <- gl(46,1,92) data.frame(subject, grp, time, outcome) subject grp time outcome 1 1 0 1 4 2 2 0 1 3 3 3 0 1 7 4 4 0 1 0 5 5 0 1 1 6 6 0 1 7 7 7 0 1 7 8 8 0 1 7 9 9 0 1 7 10 10 0 1 5 11 11 0 1 3 12 12 0 1 1 13 13 0 1 7 14 14 0 1 7 15 15 0 1 1 16 16 0 1 7 17 17 0 1 2 18 18 0 1 7 19 19 0 1 7 20 20 0 1 3 21 21 0 1 7 22 22 0 1 7 23 23 0 1 3 24 24 0 1 7 25 25 0 1 7 26 26 0 1 5 27 27 0 1 7 28 28 0 1 7 29 29 0 1 5 30 30 0 1 7 31 31 0 1 5 32 32 0 1 5 33 33 0 1 7 34 34 0 1 7 35 35 0 1 7 36 36 0 1 7 37 37 0 1 6 38 38 0 1 4 39 39 0 1 4 40 40 0 1 7 41 41 0 1 3 42 42 0 1 4 43 43 0 1 7 44 44 0 1 7 45 45 0 1 7 46 46 0 1 2 47 1 0 2 4 48 2 1 2 1 49 3 1 2 4 50 4 1 2 0 51 5 1 2 7 52 6 1 2 7 53 7 1 2 4 54 8 1 2 0 55 9 1 2 5 56 10 1 2 0 57 11 1 2 5 58 12 1 2 5 59 13 1 2 7 60 14 1 2 5 61 15 1 2 1 62 16 1 2 6 63 17 1 2 4 64 18 1 2 6 65 19 1 2 5 66 20 1 2 1 67 21 1 2 7 68 22 1 2 5 69 23 1 2 7 70 24 1 2 4 71 25 1 2 3 72 26 1 2 7 73 27 1 2 4 74 28 1 2 4 75 29 1 2 7 76 30 1 2 0 77 31 1 2 7 78 32 1 2 3 79 33 1 2 7 80 34 1 2 2 81 35 1 2 4 82 36 1 2 1 83 37 1 2 4 84 38 1 2 2 85 39 1 2 5 86 40 1 2 1 87 41 1 2 0 88 42 1 2 7 89 43 1 2 3 90 44 1 2 6 91 45 1 2 2 92 46 1 2 4 93 1 0 3 6 94 2 0 3 3 95 3 0 3 5 96 4 0 3 2 97 5 0 3 4 98 6 0 3 7 99 7 0 3 7 100 8 0 3 4 101 9 0 3 7 102 10 0 3 4 103 11 0 3 6 104 12 0 3 3 105 13 0 3 7 106 14 0 3 7 107 15 0 3 3 108 16 0 3 7 109 17 0 3 1 110 18 0 3 7 111 19 0 3 6 112 20 0 3 4 113 21 0 3 7 114 22 0 3 6 115 23 0 3 0 116 24 0 3 6 117 25 0 3 6 118 26 0 3 6 119 27 0 3 7 120 28 0 3 7 121 29 0 3 7 122 30 0 3 2 123 31 0 3 6 124 32 0 3 7 125 33 0 3 7 126 34 0 3 7 127 35 0 3 7 128 36 0 3 7 129 37 0 3 5 130 38 0 3 4 131 39 0 3 6 132 40 0 3 7 133 41 0 3 3 134 42 0 3 4 135 43 0 3 7 136 44 0 3 7 137 45 0 3 1 138 46 0 3 2 139 1 0 4 2 140 2 1 4 1 141 3 1 4 4 142 4 1 4 0 143 5 1 4 7 144 6 1 4 2 145 7 1 4 7 146 8 1 4 NA 147 9 1 4 4 148 10 1 4 0 149 11 1 4 5 150 12 1 4 6 151 13 1 4 7 152 14 1 4 4 153 15 1 4 4 154 16 1 4 7 155 17 1 4 5 156 18 1 4 7 157 19 1 4 4 158 20 1 4 0 159 21 1 4 7 160 22 1 4 3 161 23 1 4 7 162 24 1 4 7 163 25 1 4 7 164 26 1 4 7 165 27 1 4 2 166 28 1 4 6 167 29 1 4 5 168 30 1 4 0 169 31 1 4 6 170 32 1 4 3 171 33 1 4 7 172 34 1 4 4 173 35 1 4 1 174 36 1 4 4 175 37 1 4 2 176 38 1 4 1 177 39 1 4 0 178 40 1 4 3 179 41 1 4 0 180 42 1 4 7 181 43 1 4 1 182 44 1 4 6 183 45 1 4 1 184 46 1 4 3 185 1 0 1 6 186 2 0 1 1 187 3 0 1 7 188 4 0 1 4 189 5 0 1 4 190 6 0 1 6 191 7 0 1 7 192 8 0 1 4 193 9 0 1 7 194 10 0 1 7 195 11 0 1 1 196 12 0 1 0 197 13 0 1 7 198 14 0 1 7 199 15 0 1 2 200 16 0 1 7 201 17 0 1 2 202 18 0 1 7 203 19 0 1 7 204 20 0 1 2 205 21 0 1 7 206 22 0 1 6 207 23 0 1 0 208 24 0 1 5 209 25 0 1 6 210 26 0 1 7 211 27 0 1 5 212 28 0 1 7 213 29 0 1 7 214 30 0 1 0 215 31 0 1 7 216 32 0 1 7 217 33 0 1 7 218 34 0 1 7 219 35 0 1 7 220 36 0 1 7 221 37 0 1 7 222 38 0 1 4 223 39 0 1 3 224 40 0 1 7 225 41 0 1 5 226 42 0 1 5 227 43 0 1 7 228 44 0 1 7 229 45 0 1 5 230 46 0 1 2 231 1 0 2 3 232 2 1 2 2 233 3 1 2 4 234 4 1 2 0 235 5 1 2 7 236 6 1 2 3 237 7 1 2 7 238 8 1 2 1 239 9 1 2 4 240 10 1 2 1 241 11 1 2 4 242 12 1 2 4 243 13 1 2 7 244 14 1 2 4 245 15 1 2 6 246 16 1 2 6 247 17 1 2 3 248 18 1 2 6 249 19 1 2 0 250 20 1 2 0 251 21 1 2 7 252 22 1 2 3 253 23 1 2 6 254 24 1 2 7 255 25 1 2 7 256 26 1 2 7 257 27 1 2 0 258 28 1 2 2 259 29 1 2 6 260 30 1 2 0 261 31 1 2 7 262 32 1 2 1 263 33 1 2 7 264 34 1 2 4 265 35 1 2 1 266 36 1 2 3 267 37 1 2 4 268 38 1 2 1 269 39 1 2 0 270 40 1 2 1 271 41 1 2 0 272 42 1 2 6 273 43 1 2 1 274 44 1 2 6 275 45 1 2 3 276 46 1 2 3 277 1 0 3 6 278 2 0 3 0 279 3 0 3 7 280 4 0 3 7 281 5 0 3 6 282 6 0 3 7 283 7 0 3 5 284 8 0 3 4 285 9 0 3 7 286 10 0 3 7 287 11 0 3 3 288 12 0 3 3 289 13 0 3 7 290 14 0 3 7 291 15 0 3 2 292 16 0 3 7 293 17 0 3 5 294 18 0 3 3 295 19 0 3 7 296 20 0 3 2 297 21 0 3 7 298 22 0 3 6 299 23 0 3 4 300 24 0 3 0 301 25 0 3 7 302 26 0 3 7 303 27 0 3 7 304 28 0 3 7 305 29 0 3 7 306 30 0 3 1 307 31 0 3 6 308 32 0 3 7 309 33 0 3 7 310 34 0 3 7 311 35 0 3 0 312 36 0 3 7 313 37 0 3 7 314 38 0 3 5 315 39 0 3 1 316 40 0 3 7 317 41 0 3 4 318 42 0 3 4 319 43 0 3 1 320 44 0 3 7 321 45 0 3 7 322 46 0 3 1 323 1 0 4 1 324 2 1 4 3 325 3 1 4 2 326 4 1 4 0 327 5 1 4 7 328 6 1 4 NA 329 7 1 4 6 330 8 1 4 NA 331 9 1 4 4 332 10 1 4 1 333 11 1 4 6 334 12 1 4 5 335 13 1 4 7 336 14 1 4 4 337 15 1 4 0 338 16 1 4 7 339 17 1 4 4 340 18 1 4 6 341 19 1 4 0 342 20 1 4 0 343 21 1 4 7 344 22 1 4 1 345 23 1 4 6 346 24 1 4 6 347 25 1 4 7 348 26 1 4 7 349 27 1 4 2 350 28 1 4 3 351 29 1 4 7 352 30 1 4 0 353 31 1 4 5 354 32 1 4 1 355 33 1 4 6 356 34 1 4 7 357 35 1 4 1 358 36 1 4 3 359 37 1 4 1 360 38 1 4 1 361 39 1 4 0 362 40 1 4 1 363 41 1 4 0 364 42 1 4 7 365 43 1 4 1 366 44 1 4 7 367 45 1 4 0 368 46 1 4 5 Any assistance that can be offered is appreciated, regards Bob Green
Duncan Murdoch
2006-May-08 14:09 UTC
[R] performing functions on variables of different length
On 5/8/2006 4:52 AM, Bob Green wrote:> I am hoping for some assistance with a problem that has puzzled me. > Immediately below is the error messages I obtained when I tried to perform > two functions. > > (A) > > tapply(outcome,na.rm=T,grp,mean) > Error in tapply(outcome, na.rm = T, grp, mean) : > arguments must have same length > +++++++++++++++++++++++++++++++++++++++++++++++ > (B) > > library(nlme) > > anova(lme(outcome ~ grp * time, random = ~ 1 | subject)) > Error in model.frame(formula, rownames, variables, varnames, extras, > extranames, : > variable lengths differ > > I assumed this was due to a missing value, as all the variables appeared to > have the same number of values . However, when I ran the length command > this wasn't the case: > > length(outcome) > [1] 368 > > length(grp) > [1] 184 > > length(subject) > [1] 92 > > length(time) > [1] 184 > > > Below is the syntax I have been using and the date frame that I generated - > > study1dat <- read.csv("c:\\study1rb.csv",header=T) > attach (study1dat) > > outcome <- c(t1freq, t2freq,t3freq,t4freq) > grp <- factor( rep(group, 2) ) > time <- gl(4, 46) > subject <- gl(46,1,92) > data.frame(subject, grp, time, outcome) > > subject grp time outcome > 1 1 0 1 4 > 2 2 0 1 3 > 3 3 0 1 7 > 4 4 0 1 0 > 5 5 0 1 1...> 365 43 1 4 1 > 366 44 1 4 7 > 367 45 1 4 0 > 368 46 1 4 5 > > Any assistance that can be offered is appreciated,The data.frame function will make repetitions of variables as necessary. Other functions won't necessarily do this. I'd recommend putting the variables into a dataframe (thus forcing them to be the same length), and then accessing columns directly from there, e.g. mydf <- data.frame(subject, grp, time, outcome) tapply(mydf$outcome,na.rm=T,mydf$grp,mean) # OR with(mydf, tapply(outcome,na.rm=T,grp,mean)) anova(lme(outcome ~ grp * time, random = ~ 1 | subject, data=mydf)) I haven't tested these suggestions, so there may be typos. Duncan Murdoch