0

I want to make a mean matrix, whose first row is mean value of columns of df1 who have value i on their clster column, and whose 2nd row is mean value of columns of df2 who have value i on their clster column. I ran code below:

for (i in 1:k) {
    df.1[[i]] <- assign(paste0("df1.clstr",i), df1[df1$clster==i,])
    df.2[[i]] <- assign(paste0("df2.clstr",i), df2[df2$clster==i,])
    Mean.list[[i]]<- matrix(colSums(df.1[[i]])/nrow(df.1[[i]]),colSums(df.2[[i]])/nrow(df.2[[i]])),nrow = 2, ncol = 5, byrow = TRUE)
}

I get error :

Error in colSums(dth[dth$clster == i, ]) : 'x' must be numeric

when I use as.numeric, I get:

Error in is.data.frame(x) : 'list' object cannot be coerced to type 'double'

What is my mistake here?

user438383
  • 5,716
  • 8
  • 28
  • 43
Mathica
  • 25
  • 1
  • 6
  • I think these errors are quite informative. You can only take the colSums if your data frame is numeric - the error is telling you that your dataframe contains non numeric values. You must find and remove these values rather than just running ``as.numeric`` – user438383 Jun 29 '21 at 18:04
  • Also, please provide a reprocuible example of your data so that others can help more easily. Have a read of [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – user438383 Jun 29 '21 at 18:05

1 Answers1

1

We could do this with Map. Based on the code showed, we could split the 'df1', 'df2' by 'cluster', into a list of datasets. Use Map to loop over the corresponding elements, get the colMeans of each element of the list and rbind together

Mean.list <- Map(function(dat1, dat2) 
       rbind(colMeans(dat1),
       colMeans(dat2)),
       split(df1, df1$cluster), split(df2, df2$cluster))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Although I solved my problem in another way, your suggestion is very neat. I would consider this Map() for later uses. – Mathica Jun 29 '21 at 19:44