jpm miao
2016-Apr-26 21:21 UTC
[R] table , exclude - count the frequency in a data frame but exclude one value
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]]
William Dunlap
2016-Apr-26 21:43 UTC
[R] table , exclude - count the frequency in a data frame but exclude one value
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]]
Seemingly Similar Threads
- How to print the frequency table (produced by the command "table" to Excel
- How to print the frequency table (produced by the command "table" to Excel
- Why can't R understand if(num!=NA)?
- Definition of "lag" is opposite in ts and xts objects!
- How can I tabulate time series data (in RStudio or any other R editor)?