I have a data set that contains gender and marital status represented by 0 and 1. So 1 is female and 0 is male. Married is 1 and unmarried is 0. When I make my two way table in R programming I am unable to determine which column and row relates to what data. So my question is how can I label the rows/columns of the table.
Asked
Active
Viewed 2,473 times
-1
-
1Use dimension names. Tables in R are addressed and names in the same manner as matrices. Generally functions that return tables allow naming, but you need to provide the code. – IRTFM Mar 18 '16 at 08:01
-
See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – David Arenburg Mar 18 '16 at 08:07
1 Answers
2
If you label the columns correctly there should be no ambiguity:
set.seed(123) # used for reproducibilty
df1 <- data.frame(married=sample(2,100,replace = TRUE)-1,
gender=sample(2,100,replace = TRUE)-1)
head(df1)
# married gender
#1 0 1
#2 1 0
#3 0 0
#4 1 1
#5 1 0
#6 0 1
table(df1)
# gender
#married 0 1
# 0 25 28
# 1 25 22
If you already have a data.frame and need to provide names to the columns, see ?colnames
.

RHertel
- 23,412
- 5
- 38
- 64
-
table(ACS$Sex, ACS$Married) thats my code im not using a data.frame just basic table function. I assume there is an argument i can use to set the names. – cyX Mar 18 '16 at 08:09
-
-
1This question is not reproducible so I don't see what's the point of posting an answer if you don't really know what the OP has and before getting any clarifications. Also `table` has a `dnn` attribute. – David Arenburg Mar 18 '16 at 08:11
-
-
1Then you can try `table(ACS$Sex,ACS$Married,dnn=list("Sex","Married"))` – RHertel Mar 18 '16 at 08:16
-
Okay that solves one problem, How do i change the 0 to be not married and 1 be married. Otherwise its hard to read this table without knowing 1 and 0's values – cyX Mar 18 '16 at 08:18
-
1@cyX For this I suggest that you rename the values in your data.frame: `ACS$Married <- as.factor(ACS$Married); levels(ACS$Married) <- c("not married", "married")` and correspondingly for `ACS$Sex`. – RHertel Mar 18 '16 at 08:30
-
kk thanks david and Rhertel for the dnn attribute thats pretty much all i wanted. – cyX Mar 18 '16 at 09:00