[R] table , exclude - count the frequency in a data frame but exclude one value

William Dunlap wdunlap at tibco.com
Tue Apr 26 23:43:42 CEST 2016


table converts its non-factor arguments to factors using the exclude
argument that you supply.  If you want the arguments to be handled
differently, then convert them to factors
yourself, in the way you want.  E.g.,

> with(df, table(x=factor(x, exclude=1), y))
   y
x   1 2 3
  2 0 1 0
  3 1 0 0
> with(df, table(x=factor(x, exclude=1), y=factor(y, levels=3:1)))
   y
x   3 2 1
  2 0 1 0
  3 0 0 1



Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Tue, Apr 26, 2016 at 2:21 PM, jpm miao <miaojpm at gmail.com> wrote:

> Hi,
>
>    I have a data frame with two variables x, y, both of which take values
> in the set {1,2,3}. I'd like to count the frequency by the command "table",
> but exclude the value "1" in variable x, but keep "1" in variable y. Is it
> possible?  When I use "exclude", value 1 in both x and y are excluded.
> Thanks,
>
>
> > df <- data.frame(x = 1:3, y = 3:1, z = letters[1:3])
> > table(df[,c("y","x")])
>    x
> y   1 2 3
>   1 0 0 1
>   2 0 1 0
>   3 1 0 0
> > table(df[,c("y","x")], exclude = 1)
>    x
> y   2 3
>   2 1 0
>   3 0 0
> > table(df[,c("y","x")], exclude = c(NULL, 1))
>    x
> y   2 3
>   2 1 0
>   3 0 0
> > table(df[,c("y","x")], exclude = c(1, NULL))
>    x
> y   2 3
>   2 1 0
>   3 0 0
> > table(df[,c("y","x")], exclude = c(1, 0))
>    x
> y   2 3
>   2 1 0
>   3 0 0
> > table(df[,c("y","x")], exclude = list(1 , NULL))
> Error in as.vector(exclude, typeof(x)) :
>   (list) object cannot be coerced to type 'integer'
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list