rfor-loop

How to count number of combinations of 2 columns in R


I'm a beginner to code and I study RStudio at school, and I'm stuck with this question with "mtcars" dataset.

I already create a crosstab of cylinder and gear, but I need to show one more thing is "how many observations given the car has 4 cylinders with 3 gears, 4 cylinders with 4 gears, etc. Report which combination is recorded in this data set and how many observations for this type of car."

And I'm stucked with how to report the number of observations for each type of combination.

I can create the crosstab, and already asked GPT and googled how to report the number of observations, but I couldn't find an answer for it, I try using for-loop, but I didn't generate any result.

crosstabs <- data.frame('Cylinder'= mtcars$cyl,'Gear'=mtcars$gear)
print(crosstabs)
number_observations = 0
for (i in crosstabs$Cylinder) {
  for (j in crosstabs$Gear) {
    if (crosstabs[i,j] != number_observations){
      number_observations=number_observations+1
      print(paste("There are", number_observations, "cars with", i, "cylinders and", j, "gears."))
      }
    }
  }

I mean that if the combination is not yet in the number_observations, then I'll add the combinations and count 1 more to number_observations. However, it seems to be incorrect. It doesn't show any error in syntax, but no result is returned. Can you please help me with this question, please? I need your help. Thank you very much in prior!


Solution

  • Find two ways to solve your problem below:

    # way 1
    crosstabs = xtabs(~ cyl + gear, data=mtcars)
    # way 2
    crosstabs = with(mtcars, table(cyl, gear))
    
       gear
    cyl  3  4  5
      4  1  8  2
      6  2  4  1
      8 12  0  2
    

    Using the output above, you will be able to answer the question "Report which combination is recorded in this data set and how many observations for this type of car."

    In case you prefer the output to be a data.frame and not a table object, then you can convert from table to data.frame as shown below:

    as.data.frame(crosstabs)
      cyl gear Freq
    1   4    3    1
    2   6    3    2
    3   8    3   12
    4   4    4    8
    5   6    4    4
    6   8    4    0
    7   4    5    2
    8   6    5    1
    9   8    5    2