1

I'm looking to figure out what's going on with the generation of this frequency table for each attribute by variant number. For some reason I keep getting a 'all arguments must have same length' error for the table in the for() argument.

I've checked the names of the variants are correct, and ensured that length(jar.beta$variant) is equal to length(jar.beta$overall) and all of the other attributes but it's not behaving. The attributes are vectors, but I've tried to change them to factors.

Hoping to find some help. Thanks in advance!

#Sample Input
library(tibble)
jar.beta <- tribble(
  ~date, ~variant, ~appearance, ~crunch, ~spring, ~density, ~season, ~salt, ~overall,
  1-1-19, 11, 4, 5, 6, 1, 4, 2, 5,
  1-1-19, 11, 4, 5, 1, 1, 3, 7, 8,
  1-1-19, 1.4, 1, 1, 6, 1, 2, 2, 2.5,
  1-1-19, 1.4, 4, 5, 6, 1, 4, 2, 5,
  1-1-19, 1.3, 4, 5, 6, 1, 4, 2, 5,
  1-1-19, 1.3, 4, 5, 6, 1, 4, 2, 5
)  

jar.beta$variant <- as.factor(jar.beta$variant)
jar.beta$date <-as.factor(jar.beta$date)  

#Code in Question 
percentage <- vector("list", 7)                     
names(percentage) <- colnames(jar.beta[3:9])
for (i in 1:9){
    attribute.freq  <- table(jar.beta$variant,jar.beta[,i+2])
    percentage[[i]] <- as.matrix(100*prop.table(attribute.freq,margin=1),2)
    }

#Desired Output as a Percent Frequency Table
                 2   2.5     3   3.5     4   4.5     5   5.5     6   6.5     7     8   8.5
  11         16.67 33.33 16.67  0.00  0.00  0.00 16.67  0.00  0.00  0.00 16.67  0.00  0.00
  1.4        0.00  0.00 16.67  0.00 16.67 16.67 50.00  0.00  0.00  0.00  0.00  0.00  0.00
  1.3        0.00  0.00  0.00  0.00  0.00  0.00 66.67 16.67  0.00  0.00  0.00  0.00 16.67

Bob
  • 21
  • 2
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 18 '19 at 16:42
  • 1
    Perhaps `i+2` is greater than the number of columns in `jar.beta`. Do you want `i in 1:7` instead? – Andrew Gustar Oct 18 '19 at 17:08
  • @MrFlick I was able to put in a sample input and desired output. Hopefully this can help a bit – Bob Oct 21 '19 at 01:35
  • well, `i` goes from 1 to 9 and you have 9 columns. When you run `jar.beta[,i+2]` and `i` is 8, then you are asking for column 10 which does not exist. So maybe you just want `i` to go from 1 to 7. – MrFlick Oct 21 '19 at 14:45
  • I tried it out after Andrew made a comment on it as well. Unfortunately, I got the same error as before. Would there be a different way to code this to accomplish the same thing? – Bob Oct 21 '19 at 16:03

0 Answers0