Error in Loop: Number of items to replace is not a multiple of replacement length

Hi everyone,

I'm quite new in R studio and I have a problem with a code I have for a Loop. I used this code for another dataframe and it works well, but I have a problem figuring out why I can't make it work this time.

This is the code I used:

LIST <- matrix(nrow=4,ncol=5) 

c1 <- c()

rowlme <- c()

c.upper <- c()
c.lower <- c()

phylumlme=matrix(nrow=30,ncol=3) 

k=1  

for(i in c(9:12)){
  phylumlme=cbind(phylum_e[,1:2],phylum_e[,i])
  phylumlme <- as.data.frame(phylumlme)
  colnames(phylumlme)=list(a="Id",b="GDM",c="Gen")
  phylumlme$Id<-as.character(phylumlme$Id)
  phylumlme$Id<-as.factor(phylumlme$Id)
  phylumlme$GDM<-as.character(phylumlme$GDM)
  phylumlme$GDM<-as.factor(phylumlme$GDM)
  l <- lme(rank(Gen)~GDM, data=phylumlme, random=~1|Id, method="ML")
  str(phylumlme)
  
  Am <- Anova(l,type=2)[,3]
  names(Am) <- rownames(Anova(l))
  Sm <- summary(l)
  tvalue <- Sm$tTable[,4]
  pvalue <- Sm$tTable[,5]
  
  c1$Pvalue=NA
  
  c1$Contrast=NA
  
  try(c1 <- contrast(l, list(GDM=c("0")), list(GDM=c("1")), type="average"), silent=T)
  
  contrastResults <- c(c1$Contrast)
  contrastPvalue <- c(c1$Pvalue)
  c.upper <- c(c1$Upper)
  c.lower <- c(c1$Lower)
  row <- c(Am, contrastResults, contrastPvalue, c.lower, c.upper)
  LIST[k,] <- row
  
  k=k+1
}


This is the the error code I received:

'data.frame':	30 obs. of  3 variables:
 $ Id : Factor w/ 30 levels "1004","1005",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ GDM: Factor w/ 2 levels "0","1": 2 1 1 2 1 1 2 1 2 1 ...
 $ Gen: num  0 4.796 9.602 0.936 0 ...
Error in LIST[k, ] <- row : 
  number of items to replace is not a multiple of replacement length

I don't know what to do with that.
Thank you for your help !

If you add the line

print(row)

just before

LIST[k,] <- row

you will see what row is just before the error is thrown

Without phylum_ea it's hard to pinpoint the cause. Generally, though it's a dimension problem with a mismatch between LIST and row and it's not fruitful to specify exactly where by just eyeballing.

There are three ways to approach this, with varying degrees of mental effort.

  1. Debugging the R setup. I can help with a complete a reprex. See the FAQ that includes representative data.

  2. This eyeballs as a perfectly good for loop for a C derived language, but neither C or, say, Python have to worry about distinctions between the .Global and .Local environments. It's always possible to make a practice of writing loops in this style in a language that supports the style natively and to use {reticulate} and Python or {Rcpp} and C within an R session.

  3. Set aside the code temporarily and reconsider the problem functionally in terms of school algebra—f(x) = y

where

x is phylum_e
y is a tabular presentation of the five test statistics developed in the for loop
f is a function or composite function (f(g(x)) that will transform x into y

x can be inspected in its final form to conform that the objects needed can be transformed into the statistics
y is given as LIST which is dim() 4 5

y constrains f in that the return value of f must fit within y. Philosophically, y is output modified only by being populated with values within its structure it may be build programmatically, but it is known in advance what typeof everything is and where it slots dimensionally.

Considered this way, f must place either five variable vectors column wise or four row records row wise. If done in a for loop with

then

must have have length five. Consider however the difference in the length of the first and second row objects below

library(car)
#> Loading required package: carData
l <- lm(conformity ~ fcategory*partner.status, data=Moore,
          contrasts=list(fcategory=contr.sum, partner.status=contr.sum))
Am <- Anova(l,type=2)[,3]
names(Am) <- rownames(Anova(l))
Sm <- summary(l)
tvalue <- Sm$tTable[,4]
pvalue <- Sm$tTable[,5]

(row <- c(1,2,3,4,5)) |> length()
#> [1] 5
(row <- c(Am,2,3,4,5)) |> length()
#> [1] 8
Am
#>                fcategory           partner.status fcategory:partner.status 
#>                0.2769585               10.1206922                4.1846233 
#>                Residuals 
#>                       NA

Created on 2023-03-23 with reprex v2.0.2

The algebraic style of analysis, focusing on the what rather than the how is analytic and analysis is hard-seeming, static, less fluid and intuitive than how we often approach problems, which is to blaze ahead looking for some button to push differently to "make it work."

It is the first time I did this, so I hope it will be ok.

 data.frame(
+   stringsAsFactors = FALSE,
+              Feces = c(2,2,2,2,2,2,2,2,2,2,2,
+                        2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2),
+               AgeM = c(".",".",".",".",".",".",
+                        ".",".",".",".",".",".",".",".",".",".",".",".",
+                        ".",".",".",".",".",".",".",".",".",".",".",
+                        "."),
+                BMI = c(".",".",".",".",".",".",
+                        ".",".",".",".",".",".",".",".",".",".",".",".",
+                        ".",".",".",".",".",".",".",".",".",".",".",
+                        "."),
+               BMIg = c(".",".",".",".",".",".",
+                        ".",".",".",".",".",".",".",".",".",".",".",".",
+                        ".",".",".",".",".",".",".",".",".",".",".",
+                        "."),
+              SexeB = c("1","2","2","1","2","2",
+                        "2","2","2","1","2","1","1","2","1","2","1","1",
+                        "2","1","1","1","2","1","2","2","2","2","1",
+                        "1"),
+             IMCpre = c(NA,NA,NA,NA,NA,NA,NA,NA,
+                        NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,
+                        NA,NA,NA,NA,NA,NA,NA,NA),
+   Actinobacteriota = c(0,4.79627142292859,
+                        9.6016686954278,0.936176624548455,0,12.7278699873943,
+                        9.51005614711799,1.02213897726925,10.097453103877,0,
+                        8.99062766006379,3.08587274405323,0,5.63618291089911,0,
+                        13.5912873400957,0,4.32560533648629,7.43524412465196,0,
+                        3.53047460027145,6.74181662096592,0,12.6723413631516,
+                        9.7319936987104,0,0,5.65681515304462,9.93354204340869,
+                        11.5864962134379),
+       Bacteroidota = c(0.657925332000861,
+                        11.9530060609906,11.4147052118096,11.7771698038494,
+                        10.8707735264602,1.87043750834246,8.09200612838812,9.96573966453783,
+                        6.90644434485874,9.84407789950601,11.2068550125141,
+                        13.6046681802104,18.3103991755461,10.7092795339724,0,
+                        9.0717371282259,0,12.9009377315435,0,11.0456078012402,0,
+                        13.9262721730943,12.6671414418058,9.9348574664441,
+                        15.2654151446842,0,0,10.4762813478554,4.59100543568373,
+                        1.37285468164623),
+         Firmicutes = c(10.4184352176161,
+                        9.92809532854541,8.10807531849262,9.96426105702548,10.2164295168172,
+                        10.2108898095977,14.9496645362555,12.4279094827263,
+                        13.8385838561187,10.2030092268829,8.94636646150262,
+                        13.6067270730954,9.57890009399775,11.2587041810381,
+                        13.4208543148522,13.4832527487818,15.9111191340145,
+                        9.93950422393423,9.69509972975999,9.44094838227359,
+                        12.1799394809243,12.9659646102721,9.65683671005289,11.3009568477833,
+                        15.1471309977389,11.562512117246,15.5345202733844,
+                        9.89413391745394,11.7486561096556,9.96493309583661),
+     Proteobacteria = c(9.96639324463059,
+                        10.7326091003568,9.50399644810617,11.3487214589489,9.80382427742247,
+                        9.96339257345624,12.889066497652,11.7869768971523,
+                        9.78469554927908,6.37330162190131,10.6896756986345,
+                        9.95638975394507,7.89008471152421,9.8952196458271,
+                        9.96722625883599,8.85766234354353,9.96722625883599,
+                        11.5162463003369,10.3510398028036,8.26191549968875,
+                        9.95193271220375,8.09440083858662,7.6056246505235,4.53659386809178,
+                        7.24414887432571,9.96722625883599,9.96722625883599,
+                        9.93523263596951,11.434053037473,10.7596237997704),
+                 Id = as.factor(c("1004",
+                                  "1005","1006","1012","1013","1014","1020",
+                                  "1021","1028","1029","1030","1036","1037",
+                                  "1044","1052","1059","1060","1061","1067",
+                                  "1068","1069","1075","1076","1077","1083",
+                                  "1084","1085","1091","1092","1093")),
+                GDM = as.factor(c("1","0",
+                                  "0","1","0","0","1","0","1","0","0","1",
+                                  "0","1","1","1","1","1","1","1","1",
+                                  "1","0","1","1","0","0","1","0","0"))

This is the code I can't use

LIST <- matrix(nrow=4,ncol=5) 

c1 <- c()

rowlme <- c()

c.upper <- c()
c.lower <- c()

phylumlme=matrix(nrow=30,ncol=3) 

k=1  

for(i in c(9:12)){
  phylumlme=cbind(phylum_e[,1:2],phylum_e[,i])
  str(phylum_e)
  phylumlme <- as.data.frame(phylumlme)
  colnames(phylumlme)=list(a="Id",b="GDM",c="Gen")
  phylumlme$Id<-as.character(phylumlme$Id)
  phylumlme$Id<-as.factor(phylumlme$Id)
  phylumlme$GDM<-as.character(phylumlme$GDM)
  phylumlme$GDM<-as.factor(phylumlme$GDM)
  l <- lme(rank(Gen)~GDM, data=phylumlme, random=~1|Id, method="ML")
  summary(l)
  str(phylumlme)
  
  Am <- Anova(l,type=2)[,3]
  names(Am) <- rownames(Anova(l))
  Sm <- summary(l)
  tvalue <- Sm$tTable[,4]
  pvalue <- Sm$tTable[,5]
  
  c1$Pvalue=NA
  
  c1$Contrast=NA
  
  try(c1 <- contrast(l, list(GDM=c("0")), list(GDM=c("1")), type="average"), silent=T)
  
  contrastResults <- c(c1$Contrast)
  contrastPvalue <- c(c1$Pvalue)
  c.upper <- c(c1$Upper)
  c.lower <- c(c1$Lower)
  row <- c(Am, contrastResults, contrastPvalue, c.lower, c.upper)
  print(row)
  LIST[k,] <- row
  
  k=k+1
}

And this is the error code I received:

Error in LIST[k, ] <- row : 
  number of items to replace is not a multiple of replacement length

Thank you for your help !

Can't reproduce without additional data object

Error in cbind(phylum_e[, 1:2], phylum_e[, i]) :
object 'phylum_e' not found

That's a potential source of the error

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.