Dan Bolser wrote:> Hi, I have designed the following function to extract count frequencies > from an array of integers. For example... > > # Tipical array > x <- cbind(1,1,1,1,1,2,2,2,2,3,3,3,3,4,5,6,7,22) > > # Define the frequency function > frequency <- > function(x){ > max <- max(x) > j <- c() > for(i in 1:max){ > j[i] <- length(x[x==i]) > } > return(j) > } > > fre <- frequency(x) > plot(fre) > > How can I ... > > 1) Make this a general function so my array could be of the form > > # eats! > x <- cbind( "egg","egg","egg","egg","ham","ham","ham","ham","chicken" ) > > fre <- frequency(x) > plot(fre) > > 2) Make frequency return an object which I can call plot on (allowing the > prob=TRUE option).See ?table: table(x) plot(table(x)) plot(table(x) / sum(table(x))) Uwe Ligges> Cheers, > Dan. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
I think: frequency <- function(x) table(as.factor(x)) should work generally. Not sure what you mean by the plotting. -roger Dan Bolser wrote:> Hi, I have designed the following function to extract count frequencies > from an array of integers. For example... > > # Tipical array > x <- cbind(1,1,1,1,1,2,2,2,2,3,3,3,3,4,5,6,7,22) > > # Define the frequency function > frequency <- > function(x){ > max <- max(x) > j <- c() > for(i in 1:max){ > j[i] <- length(x[x==i]) > } > return(j) > } > > fre <- frequency(x) > plot(fre) > > How can I ... > > 1) Make this a general function so my array could be of the form > > # eats! > x <- cbind( "egg","egg","egg","egg","ham","ham","ham","ham","chicken" ) > > fre <- frequency(x) > plot(fre) > > 2) Make frequency return an object which I can call plot on (allowing the > prob=TRUE option). > > Cheers, > Dan. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Hi, I have designed the following function to extract count frequencies from an array of integers. For example... # Tipical array x <- cbind(1,1,1,1,1,2,2,2,2,3,3,3,3,4,5,6,7,22) # Define the frequency function frequency <- function(x){ max <- max(x) j <- c() for(i in 1:max){ j[i] <- length(x[x==i]) } return(j) } fre <- frequency(x) plot(fre) How can I ... 1) Make this a general function so my array could be of the form # eats! x <- cbind( "egg","egg","egg","egg","ham","ham","ham","ham","chicken" ) fre <- frequency(x) plot(fre) 2) Make frequency return an object which I can call plot on (allowing the prob=TRUE option). Cheers, Dan.