How to split a data frame based on one variable and create cross table separetely between other two variable...

Multi tool use
up vote
-1
down vote
favorite
My dataset: df
PID<-c(1,2,3,4,5,6,7,8,9)
gender<-c(1,1,0,1,0,0,0,1,1)
smoking<-c(1,1,0,0,0,0,1,0,1)
disease<-c(1,0,0,1,1,1,0,1,0)
BMI<-c(24,23,21,28,29,21,18,19,16)
df<-data.frame(PID, gender, smoking, disease, BMI)
I want to split this dataset based on gender. Then develop crosstab between smoking and disease. How to do this?
Expected outcome(first question):
Gender:1
crosstab between smoking and disease
Gender:2
Crosstab between smoking and disease.
Expected outcome (second question):
Gender:1
mean of BMI
Gender:2
mean of BMI
r
add a comment |
up vote
-1
down vote
favorite
My dataset: df
PID<-c(1,2,3,4,5,6,7,8,9)
gender<-c(1,1,0,1,0,0,0,1,1)
smoking<-c(1,1,0,0,0,0,1,0,1)
disease<-c(1,0,0,1,1,1,0,1,0)
BMI<-c(24,23,21,28,29,21,18,19,16)
df<-data.frame(PID, gender, smoking, disease, BMI)
I want to split this dataset based on gender. Then develop crosstab between smoking and disease. How to do this?
Expected outcome(first question):
Gender:1
crosstab between smoking and disease
Gender:2
Crosstab between smoking and disease.
Expected outcome (second question):
Gender:1
mean of BMI
Gender:2
mean of BMI
r
Have you tried anything yet? I'd thinkxtabs
andaggregate
should answer both questions.
– r2evans
Nov 5 at 2:28
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
My dataset: df
PID<-c(1,2,3,4,5,6,7,8,9)
gender<-c(1,1,0,1,0,0,0,1,1)
smoking<-c(1,1,0,0,0,0,1,0,1)
disease<-c(1,0,0,1,1,1,0,1,0)
BMI<-c(24,23,21,28,29,21,18,19,16)
df<-data.frame(PID, gender, smoking, disease, BMI)
I want to split this dataset based on gender. Then develop crosstab between smoking and disease. How to do this?
Expected outcome(first question):
Gender:1
crosstab between smoking and disease
Gender:2
Crosstab between smoking and disease.
Expected outcome (second question):
Gender:1
mean of BMI
Gender:2
mean of BMI
r
My dataset: df
PID<-c(1,2,3,4,5,6,7,8,9)
gender<-c(1,1,0,1,0,0,0,1,1)
smoking<-c(1,1,0,0,0,0,1,0,1)
disease<-c(1,0,0,1,1,1,0,1,0)
BMI<-c(24,23,21,28,29,21,18,19,16)
df<-data.frame(PID, gender, smoking, disease, BMI)
I want to split this dataset based on gender. Then develop crosstab between smoking and disease. How to do this?
Expected outcome(first question):
Gender:1
crosstab between smoking and disease
Gender:2
Crosstab between smoking and disease.
Expected outcome (second question):
Gender:1
mean of BMI
Gender:2
mean of BMI
r
r
edited Nov 5 at 2:31
asked Nov 5 at 2:15
Bikram Adhitya Adhikari
154
154
Have you tried anything yet? I'd thinkxtabs
andaggregate
should answer both questions.
– r2evans
Nov 5 at 2:28
add a comment |
Have you tried anything yet? I'd thinkxtabs
andaggregate
should answer both questions.
– r2evans
Nov 5 at 2:28
Have you tried anything yet? I'd think
xtabs
and aggregate
should answer both questions.– r2evans
Nov 5 at 2:28
Have you tried anything yet? I'd think
xtabs
and aggregate
should answer both questions.– r2evans
Nov 5 at 2:28
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
No need for external packages:
xtabs(~smoking+disease+gender,data=df)
# , , gender = 0
# disease
# smoking 0 1
# 0 1 2
# 1 1 0
# , , gender = 1
# disease
# smoking 0 1
# 0 0 2
# 1 2 1
aggregate(df$BMI, list(gender=df$gender), FUN=mean)
# gender x
# 1 0 22.25
# 2 1 22.00
Similarly (thanks thelatemail):
aggregate(BMI ~ gender, data = df, FUN=mean)
1
aggregate(BMI ~ gender, data=df, FUN=mean)
might be a bit more readable and keep the consistent formula interface. Or evenaggregate(df["BMI"], df["gender"], FUN=mean)
if the formulas are not to your taste.
– thelatemail
Nov 5 at 3:56
add a comment |
up vote
1
down vote
Here is a possible way for the first question using dplyr
:
library(dplyr)
library(magrittr)
> df %>% split(gender) %>% lapply(function(x) tab=xtabs(gender~smoking+disease, data=x))
$`0`
disease
smoking 0 1
0 0 0
1 0 0
$`1`
disease
smoking 0 1
0 0 2
1 2 1
You really don't needdlpyr
here ...xtabs(~smoking+disease+gender,data=df)
does effectively the same thing
– r2evans
Nov 5 at 3:19
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
No need for external packages:
xtabs(~smoking+disease+gender,data=df)
# , , gender = 0
# disease
# smoking 0 1
# 0 1 2
# 1 1 0
# , , gender = 1
# disease
# smoking 0 1
# 0 0 2
# 1 2 1
aggregate(df$BMI, list(gender=df$gender), FUN=mean)
# gender x
# 1 0 22.25
# 2 1 22.00
Similarly (thanks thelatemail):
aggregate(BMI ~ gender, data = df, FUN=mean)
1
aggregate(BMI ~ gender, data=df, FUN=mean)
might be a bit more readable and keep the consistent formula interface. Or evenaggregate(df["BMI"], df["gender"], FUN=mean)
if the formulas are not to your taste.
– thelatemail
Nov 5 at 3:56
add a comment |
up vote
2
down vote
No need for external packages:
xtabs(~smoking+disease+gender,data=df)
# , , gender = 0
# disease
# smoking 0 1
# 0 1 2
# 1 1 0
# , , gender = 1
# disease
# smoking 0 1
# 0 0 2
# 1 2 1
aggregate(df$BMI, list(gender=df$gender), FUN=mean)
# gender x
# 1 0 22.25
# 2 1 22.00
Similarly (thanks thelatemail):
aggregate(BMI ~ gender, data = df, FUN=mean)
1
aggregate(BMI ~ gender, data=df, FUN=mean)
might be a bit more readable and keep the consistent formula interface. Or evenaggregate(df["BMI"], df["gender"], FUN=mean)
if the formulas are not to your taste.
– thelatemail
Nov 5 at 3:56
add a comment |
up vote
2
down vote
up vote
2
down vote
No need for external packages:
xtabs(~smoking+disease+gender,data=df)
# , , gender = 0
# disease
# smoking 0 1
# 0 1 2
# 1 1 0
# , , gender = 1
# disease
# smoking 0 1
# 0 0 2
# 1 2 1
aggregate(df$BMI, list(gender=df$gender), FUN=mean)
# gender x
# 1 0 22.25
# 2 1 22.00
Similarly (thanks thelatemail):
aggregate(BMI ~ gender, data = df, FUN=mean)
No need for external packages:
xtabs(~smoking+disease+gender,data=df)
# , , gender = 0
# disease
# smoking 0 1
# 0 1 2
# 1 1 0
# , , gender = 1
# disease
# smoking 0 1
# 0 0 2
# 1 2 1
aggregate(df$BMI, list(gender=df$gender), FUN=mean)
# gender x
# 1 0 22.25
# 2 1 22.00
Similarly (thanks thelatemail):
aggregate(BMI ~ gender, data = df, FUN=mean)
edited Nov 5 at 4:03
answered Nov 5 at 3:19


r2evans
23.7k32856
23.7k32856
1
aggregate(BMI ~ gender, data=df, FUN=mean)
might be a bit more readable and keep the consistent formula interface. Or evenaggregate(df["BMI"], df["gender"], FUN=mean)
if the formulas are not to your taste.
– thelatemail
Nov 5 at 3:56
add a comment |
1
aggregate(BMI ~ gender, data=df, FUN=mean)
might be a bit more readable and keep the consistent formula interface. Or evenaggregate(df["BMI"], df["gender"], FUN=mean)
if the formulas are not to your taste.
– thelatemail
Nov 5 at 3:56
1
1
aggregate(BMI ~ gender, data=df, FUN=mean)
might be a bit more readable and keep the consistent formula interface. Or even aggregate(df["BMI"], df["gender"], FUN=mean)
if the formulas are not to your taste.– thelatemail
Nov 5 at 3:56
aggregate(BMI ~ gender, data=df, FUN=mean)
might be a bit more readable and keep the consistent formula interface. Or even aggregate(df["BMI"], df["gender"], FUN=mean)
if the formulas are not to your taste.– thelatemail
Nov 5 at 3:56
add a comment |
up vote
1
down vote
Here is a possible way for the first question using dplyr
:
library(dplyr)
library(magrittr)
> df %>% split(gender) %>% lapply(function(x) tab=xtabs(gender~smoking+disease, data=x))
$`0`
disease
smoking 0 1
0 0 0
1 0 0
$`1`
disease
smoking 0 1
0 0 2
1 2 1
You really don't needdlpyr
here ...xtabs(~smoking+disease+gender,data=df)
does effectively the same thing
– r2evans
Nov 5 at 3:19
add a comment |
up vote
1
down vote
Here is a possible way for the first question using dplyr
:
library(dplyr)
library(magrittr)
> df %>% split(gender) %>% lapply(function(x) tab=xtabs(gender~smoking+disease, data=x))
$`0`
disease
smoking 0 1
0 0 0
1 0 0
$`1`
disease
smoking 0 1
0 0 2
1 2 1
You really don't needdlpyr
here ...xtabs(~smoking+disease+gender,data=df)
does effectively the same thing
– r2evans
Nov 5 at 3:19
add a comment |
up vote
1
down vote
up vote
1
down vote
Here is a possible way for the first question using dplyr
:
library(dplyr)
library(magrittr)
> df %>% split(gender) %>% lapply(function(x) tab=xtabs(gender~smoking+disease, data=x))
$`0`
disease
smoking 0 1
0 0 0
1 0 0
$`1`
disease
smoking 0 1
0 0 2
1 2 1
Here is a possible way for the first question using dplyr
:
library(dplyr)
library(magrittr)
> df %>% split(gender) %>% lapply(function(x) tab=xtabs(gender~smoking+disease, data=x))
$`0`
disease
smoking 0 1
0 0 0
1 0 0
$`1`
disease
smoking 0 1
0 0 2
1 2 1
answered Nov 5 at 3:06


mysteRious
1,9702512
1,9702512
You really don't needdlpyr
here ...xtabs(~smoking+disease+gender,data=df)
does effectively the same thing
– r2evans
Nov 5 at 3:19
add a comment |
You really don't needdlpyr
here ...xtabs(~smoking+disease+gender,data=df)
does effectively the same thing
– r2evans
Nov 5 at 3:19
You really don't need
dlpyr
here ... xtabs(~smoking+disease+gender,data=df)
does effectively the same thing– r2evans
Nov 5 at 3:19
You really don't need
dlpyr
here ... xtabs(~smoking+disease+gender,data=df)
does effectively the same thing– r2evans
Nov 5 at 3:19
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147467%2fhow-to-split-a-data-frame-based-on-one-variable-and-create-cross-table-separetel%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
x59ucEP2x,iH is XX,gabzohHav,gyCaW5X9,QcNeL4fYK5UC0cVdesovpr pxcWEjZwcGXBf,rue5,Dqrba zPcG9cPvEp3EjFSl9qTZ 0
Have you tried anything yet? I'd think
xtabs
andaggregate
should answer both questions.– r2evans
Nov 5 at 2:28