Flip order Columns / Rows in a table












1















I'm using the epiR package as it does nice 2 by 2 contingency tables with odds ratios, and population attributable fractions.



As is common my data is coded
0 = No
1 = Yes



So when I do



tabele(var_1,var_2)


The output comes out as a table aligned like



enter image description here



For its input though epiR wants the top left square to be Exposed+VE Outcome+VE - i.e the top left square should be Var 1==1 and Var 2==1



Currently I do this by recoding the zeroes to 2 or alternatively by setting as a factor and using re-level. Both of these are slightly annoying for other analyses as in general I want Outcome+VE to come after Outcome-VE



So I wondered if there is an easy way (?within table) to flip the orientation of table so that it essentially inverts the ordering of the rows/columns?



Hope the above makes sense - happy to provide clarification if not.





Edit: Thanks for suggestions below; just for clarification I want to be able to do this when calling table from existing dataframe variable - i.e when what I am doing is table(data$var_1, data$var_2) - ideally without having to create a whole new object










share|improve this question

























  • Is "tabele" a typo, and meant to be "table"?

    – zx8754
    Nov 26 '18 at 8:52
















1















I'm using the epiR package as it does nice 2 by 2 contingency tables with odds ratios, and population attributable fractions.



As is common my data is coded
0 = No
1 = Yes



So when I do



tabele(var_1,var_2)


The output comes out as a table aligned like



enter image description here



For its input though epiR wants the top left square to be Exposed+VE Outcome+VE - i.e the top left square should be Var 1==1 and Var 2==1



Currently I do this by recoding the zeroes to 2 or alternatively by setting as a factor and using re-level. Both of these are slightly annoying for other analyses as in general I want Outcome+VE to come after Outcome-VE



So I wondered if there is an easy way (?within table) to flip the orientation of table so that it essentially inverts the ordering of the rows/columns?



Hope the above makes sense - happy to provide clarification if not.





Edit: Thanks for suggestions below; just for clarification I want to be able to do this when calling table from existing dataframe variable - i.e when what I am doing is table(data$var_1, data$var_2) - ideally without having to create a whole new object










share|improve this question

























  • Is "tabele" a typo, and meant to be "table"?

    – zx8754
    Nov 26 '18 at 8:52














1












1








1








I'm using the epiR package as it does nice 2 by 2 contingency tables with odds ratios, and population attributable fractions.



As is common my data is coded
0 = No
1 = Yes



So when I do



tabele(var_1,var_2)


The output comes out as a table aligned like



enter image description here



For its input though epiR wants the top left square to be Exposed+VE Outcome+VE - i.e the top left square should be Var 1==1 and Var 2==1



Currently I do this by recoding the zeroes to 2 or alternatively by setting as a factor and using re-level. Both of these are slightly annoying for other analyses as in general I want Outcome+VE to come after Outcome-VE



So I wondered if there is an easy way (?within table) to flip the orientation of table so that it essentially inverts the ordering of the rows/columns?



Hope the above makes sense - happy to provide clarification if not.





Edit: Thanks for suggestions below; just for clarification I want to be able to do this when calling table from existing dataframe variable - i.e when what I am doing is table(data$var_1, data$var_2) - ideally without having to create a whole new object










share|improve this question
















I'm using the epiR package as it does nice 2 by 2 contingency tables with odds ratios, and population attributable fractions.



As is common my data is coded
0 = No
1 = Yes



So when I do



tabele(var_1,var_2)


The output comes out as a table aligned like



enter image description here



For its input though epiR wants the top left square to be Exposed+VE Outcome+VE - i.e the top left square should be Var 1==1 and Var 2==1



Currently I do this by recoding the zeroes to 2 or alternatively by setting as a factor and using re-level. Both of these are slightly annoying for other analyses as in general I want Outcome+VE to come after Outcome-VE



So I wondered if there is an easy way (?within table) to flip the orientation of table so that it essentially inverts the ordering of the rows/columns?



Hope the above makes sense - happy to provide clarification if not.





Edit: Thanks for suggestions below; just for clarification I want to be able to do this when calling table from existing dataframe variable - i.e when what I am doing is table(data$var_1, data$var_2) - ideally without having to create a whole new object







r contingency






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 22:21









zx8754

29.8k76399




29.8k76399










asked Nov 26 '18 at 8:15









mmarksmmarks

31517




31517













  • Is "tabele" a typo, and meant to be "table"?

    – zx8754
    Nov 26 '18 at 8:52



















  • Is "tabele" a typo, and meant to be "table"?

    – zx8754
    Nov 26 '18 at 8:52

















Is "tabele" a typo, and meant to be "table"?

– zx8754
Nov 26 '18 at 8:52





Is "tabele" a typo, and meant to be "table"?

– zx8754
Nov 26 '18 at 8:52












2 Answers
2






active

oldest

votes


















5














Table is a simple matrix. You can just call indices in reverse order.



xy <- table(data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2]))

variable
value a b
0 20 22
1 30 28

xy[2:1, 2:1]

variable
value b a
1 20 30
0 30 20





share|improve this answer
























  • Thanks - is there a way to do this when calling table from existing dataframe - i.e when what I am doing is table(data$var_1, data$var_2)

    – mmarks
    Nov 26 '18 at 22:19





















3














Using factor levels:



# reproducible example (adapted from Roman's answer)
df1 <- data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2])

table(df1)
# variable
# value a b
# 0 32 23
# 1 18 27

#convert to factor, specify levels
df1$value <- factor(df1$value, levels = c("1", "0"))
df1$variable <- factor(df1$variable, levels = c("b", "a"))

table(df1)
# variable
# value b a
# 1 24 26
# 0 26 24





share|improve this answer
























  • Thanks - as mentioned above I'm trying to find an example that doesn't involve actually altering the values - because its a quirk of epiR that it wants this layout so I just want something to flip the values essentially within the call to function

    – mmarks
    Nov 26 '18 at 22:16






  • 1





    then table(transform(df1, value = factor(value, levels = c("1", "0"), variable = factor(variable, levels = c("b", "a")))

    – Moody_Mudskipper
    Dec 2 '18 at 2:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477008%2fflip-order-columns-rows-in-a-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Table is a simple matrix. You can just call indices in reverse order.



xy <- table(data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2]))

variable
value a b
0 20 22
1 30 28

xy[2:1, 2:1]

variable
value b a
1 20 30
0 30 20





share|improve this answer
























  • Thanks - is there a way to do this when calling table from existing dataframe - i.e when what I am doing is table(data$var_1, data$var_2)

    – mmarks
    Nov 26 '18 at 22:19


















5














Table is a simple matrix. You can just call indices in reverse order.



xy <- table(data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2]))

variable
value a b
0 20 22
1 30 28

xy[2:1, 2:1]

variable
value b a
1 20 30
0 30 20





share|improve this answer
























  • Thanks - is there a way to do this when calling table from existing dataframe - i.e when what I am doing is table(data$var_1, data$var_2)

    – mmarks
    Nov 26 '18 at 22:19
















5












5








5







Table is a simple matrix. You can just call indices in reverse order.



xy <- table(data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2]))

variable
value a b
0 20 22
1 30 28

xy[2:1, 2:1]

variable
value b a
1 20 30
0 30 20





share|improve this answer













Table is a simple matrix. You can just call indices in reverse order.



xy <- table(data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2]))

variable
value a b
0 20 22
1 30 28

xy[2:1, 2:1]

variable
value b a
1 20 30
0 30 20






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 8:22









Roman LuštrikRoman Luštrik

49.8k19109160




49.8k19109160













  • Thanks - is there a way to do this when calling table from existing dataframe - i.e when what I am doing is table(data$var_1, data$var_2)

    – mmarks
    Nov 26 '18 at 22:19





















  • Thanks - is there a way to do this when calling table from existing dataframe - i.e when what I am doing is table(data$var_1, data$var_2)

    – mmarks
    Nov 26 '18 at 22:19



















Thanks - is there a way to do this when calling table from existing dataframe - i.e when what I am doing is table(data$var_1, data$var_2)

– mmarks
Nov 26 '18 at 22:19







Thanks - is there a way to do this when calling table from existing dataframe - i.e when what I am doing is table(data$var_1, data$var_2)

– mmarks
Nov 26 '18 at 22:19















3














Using factor levels:



# reproducible example (adapted from Roman's answer)
df1 <- data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2])

table(df1)
# variable
# value a b
# 0 32 23
# 1 18 27

#convert to factor, specify levels
df1$value <- factor(df1$value, levels = c("1", "0"))
df1$variable <- factor(df1$variable, levels = c("b", "a"))

table(df1)
# variable
# value b a
# 1 24 26
# 0 26 24





share|improve this answer
























  • Thanks - as mentioned above I'm trying to find an example that doesn't involve actually altering the values - because its a quirk of epiR that it wants this layout so I just want something to flip the values essentially within the call to function

    – mmarks
    Nov 26 '18 at 22:16






  • 1





    then table(transform(df1, value = factor(value, levels = c("1", "0"), variable = factor(variable, levels = c("b", "a")))

    – Moody_Mudskipper
    Dec 2 '18 at 2:29
















3














Using factor levels:



# reproducible example (adapted from Roman's answer)
df1 <- data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2])

table(df1)
# variable
# value a b
# 0 32 23
# 1 18 27

#convert to factor, specify levels
df1$value <- factor(df1$value, levels = c("1", "0"))
df1$variable <- factor(df1$variable, levels = c("b", "a"))

table(df1)
# variable
# value b a
# 1 24 26
# 0 26 24





share|improve this answer
























  • Thanks - as mentioned above I'm trying to find an example that doesn't involve actually altering the values - because its a quirk of epiR that it wants this layout so I just want something to flip the values essentially within the call to function

    – mmarks
    Nov 26 '18 at 22:16






  • 1





    then table(transform(df1, value = factor(value, levels = c("1", "0"), variable = factor(variable, levels = c("b", "a")))

    – Moody_Mudskipper
    Dec 2 '18 at 2:29














3












3








3







Using factor levels:



# reproducible example (adapted from Roman's answer)
df1 <- data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2])

table(df1)
# variable
# value a b
# 0 32 23
# 1 18 27

#convert to factor, specify levels
df1$value <- factor(df1$value, levels = c("1", "0"))
df1$variable <- factor(df1$variable, levels = c("b", "a"))

table(df1)
# variable
# value b a
# 1 24 26
# 0 26 24





share|improve this answer













Using factor levels:



# reproducible example (adapted from Roman's answer)
df1 <- data.frame(value = rbinom(100, size = 1, prob = 0.5),
variable = letters[1:2])

table(df1)
# variable
# value a b
# 0 32 23
# 1 18 27

#convert to factor, specify levels
df1$value <- factor(df1$value, levels = c("1", "0"))
df1$variable <- factor(df1$variable, levels = c("b", "a"))

table(df1)
# variable
# value b a
# 1 24 26
# 0 26 24






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 8:50









zx8754zx8754

29.8k76399




29.8k76399













  • Thanks - as mentioned above I'm trying to find an example that doesn't involve actually altering the values - because its a quirk of epiR that it wants this layout so I just want something to flip the values essentially within the call to function

    – mmarks
    Nov 26 '18 at 22:16






  • 1





    then table(transform(df1, value = factor(value, levels = c("1", "0"), variable = factor(variable, levels = c("b", "a")))

    – Moody_Mudskipper
    Dec 2 '18 at 2:29



















  • Thanks - as mentioned above I'm trying to find an example that doesn't involve actually altering the values - because its a quirk of epiR that it wants this layout so I just want something to flip the values essentially within the call to function

    – mmarks
    Nov 26 '18 at 22:16






  • 1





    then table(transform(df1, value = factor(value, levels = c("1", "0"), variable = factor(variable, levels = c("b", "a")))

    – Moody_Mudskipper
    Dec 2 '18 at 2:29

















Thanks - as mentioned above I'm trying to find an example that doesn't involve actually altering the values - because its a quirk of epiR that it wants this layout so I just want something to flip the values essentially within the call to function

– mmarks
Nov 26 '18 at 22:16





Thanks - as mentioned above I'm trying to find an example that doesn't involve actually altering the values - because its a quirk of epiR that it wants this layout so I just want something to flip the values essentially within the call to function

– mmarks
Nov 26 '18 at 22:16




1




1





then table(transform(df1, value = factor(value, levels = c("1", "0"), variable = factor(variable, levels = c("b", "a")))

– Moody_Mudskipper
Dec 2 '18 at 2:29





then table(transform(df1, value = factor(value, levels = c("1", "0"), variable = factor(variable, levels = c("b", "a")))

– Moody_Mudskipper
Dec 2 '18 at 2:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53477008%2fflip-order-columns-rows-in-a-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Lallio

Futebolista

Jornalista