I could not find anywhere how I can do this seemingly simple task (Retrieving column names in R was not helpful).

I have an R table defined as sports_by_sex <- table(ais$sex, ais$sport). This is using DAAG's ais dataset. If I want to get the name of the sport at sports_by_sex[, 2], how would I do that? When I print(sports_by_sex), this is my result:

    B_Ball Field Gym Netball Row Swim T_400m T_Sprnt Tennis W_Polo
  f     13     7   4      23  22    9     11       4      7      0
  m     12    12   0       0  15   13     18      11      4     17

So for the example of sports_by_sex[, 2], for example, I would like to get Field back.


1 Answer 1


Found the solution - access the index you want on colnames():

colnames(sports_by_sex)[2], for example.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.