简体   繁体   中英

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.

You can try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM