Hi,
This is my dataframe:
chisq_matrix <- as.table(rbind(c(18, 36, 21, 9, 6),
c(12, 36, 45, 36, 21),
c(6, 9, 9, 3, 3),
c(3, 9, 9, 6, 3)))
dimnames(chisq_matrix) <- list(Marital_status = c("Never_married", "Married", "Divorced", "Widowed"),
Education_level = c("Middleschoolorlower", "Highschool", "Bachelors", "Masters", "PhDorhigher"))
which gives me this:
I would like to get this:
and so on to fill up all dataframe. How do I do it, please.
Hello,
You can try this:
chisq_matrix$Middleschoolorlower <- paste0(chisq_matrix$Middleschoolorlower, " ", "-", " ", chisq_matrix$Marital_status, "/Middleschoolorlower")
However, the output of the code you have written does not correspond to the first table you show.
chisq_matrix = chisq_matrix %>% as.data.frame.matrix()
This is corrected code.
Yes, sorry, I forgot to mention that chisq_matrix has to be a data frame in this case. But if you use
as.data.frame.matrix()
instead of
as.data.frame()
the code I have provided above won't give you the output you want.
I would do this:
class(chisq_matrix) # table
chisq_matrix <- as.data.frame(chisq_matrix)
class(chisq_matrix) # data.frame
chisq_matrix$Middleschoolorlower <- paste0(chisq_matrix$Middleschoolorlower, " ", "-", " ", chisq_matrix$Marital_status, "/Middleschoolorlower")
Thank you for your reply, I can do it your way step-by-step, but maybe you know more automatic solution for all columns at once ?
system
Closed
6
This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.