删除未使用的分类值boxplot - R [英] Remove Unused categorical values boxplot - R

查看:125
本文介绍了删除未使用的分类值boxplot - R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  x = rnorm(30,1,1)
c = c rep(x1,10),rep(x2,10),rep(x3,10))
df = dataframe(x,c)
boxplot(x〜c,data = df)

它的效果很好。但是,如果我决定我不再有兴趣看到x3,删除它,并重新打印:

  dfMod = subset(df,c %c(x1,x2))
boxplot(x〜c,data = dfMod)

boxplot仍显示x3的列。





Ive尝试给boxplot提示使用

  boxplot(x〜c,data = dfMod,names = c(x1,x2))

但这会抛出名称大小不正确的错误。感谢您的帮助

解决方案

在<$ c后使用 $ c>子集

  dfMod<  -  subset(df,c%in%c x1,x2))
dfMod $ c < - 等级(dfMod $ c)
boxplot(x〜c,data = dfMod)
/ pre>

您还可以使用更改因子字符并在 boxplot 中调用



<$ p $ (c)c(x1,x2), ),data = df)


I have the following code:

x = rnorm(30, 1, 1)
c = c(rep("x1",10), rep("x2",10), rep("x3",10))
df = dataframe(x,c)
boxplot(x ~ c, data=df)

It works great. But if I decide I am no longer interested in seeing x3, remove it, and replot:

dfMod = subset(df, c %in% c("x1", "x2"))
boxplot(x ~ c,data=dfMod)

The boxplot still shows a column for x3.

Ive tried giving boxplot a hint using

boxplot(x~c,data=dfMod, names = c("x1", "x2"))

but this throws the error that names size is not right. Thanks in advance for the help

解决方案

Use droplevels after subset

dfMod <- subset(df, c %in% c("x1", "x2"))    
dfMod$c <- droplevels(dfMod$c)
boxplot(x ~ c,data=dfMod)

You can also use class to change factor to character and subsetting inside boxplot call

class(df) <- c("numeric", "character")
boxplot(x ~ c, subset=c %in% c("x1", "x2"),  data=df)

这篇关于删除未使用的分类值boxplot - R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆