ah bon
ah bon

Reputation: 10051

Boxplots of dependant variable with each character variables in R

I have a dataframe which has the following columns:

   ks3score    ks4score      ethnic      gender    SECshort    hiquamum   singlepar       house 
  "numeric"   "numeric" "character" "character" "character" "character" "character" "character" 
        fsm      parasp    computer     tuition      pupasp    homework    attitude         asc 
"character" "character" "character" "character" "character" "character" "character" "character" 
        sen     truancy      absent     IDACI_n     FSMband 
"character" "character" "character"   "numeric" "character" 

I want to draw boxplot for each character variable and ks4score, now I have used the code below, but not concise enough:

boxplot(ks4score ~ ethnic, df)
boxplot(ks4score ~ gender, df)
...

Just wonder if there is a way improve it? Thanks.

Upvotes: 0

Views: 56

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389175

You can try :

cols <- names(df)[sapply(df, is.character)]
all_plots <- lapply(cols, function(x) boxplot(reformulate(x, 'ks4score'), df))

Upvotes: 1

Related Questions