Change column names to respective tables

I have to change the column names inside table function. But the issue is I have used for loop, so I am taking i and j to name column names. Below is the code. If you observe, in proptable i have used i and j but not working. The output is coming as i and j only . I need the respective column names as defined in for loop

df <- structure(list(Product = structure(c(2L, 1L, 3L), .Label = c("Product 100", 
"Product 75", "Product 87"), class = "factor"), Product.Family = structure(c(1L, 
2L, 2L), .Label = c("Product.Family 43", "Product.Family 48"), class = "factor"), 
    ColA = structure(1:3, .Label = c("A", "B", "C"), class = "factor")), class = "data.frame", row.names = c(NA, 
-3L))

p326 <- list()
  bs <- names(Filter(is.factor, df))
  for(i in bs)
  for(j in bs) {
    p326[[i]][[j]] <-  as.data.frame(prop.table(table(i=df[,i],j=df[,j]), margin=1)*100)
    do.call(rbind,p326) %>% as.data.frame()
  }

Again, please provide a proper reproducible example, if I run your code this is what I get

#> Error in do.call(rbind, p326) %>% as.data.frame(): could not find function "%>%"

Also, it would be helpful if you could provide a sample of your desired output, very likely there is a simpler way to get what you are looking for without using a for-loop.

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