0

I am using apply for the first time. I want to count the number of occourences of values in my dataset. I have 10 variables/columns.

This works:

apply(data, 2, function(x) {count(x)}

However instead of getting the results prointed in the console window, I want to print the result of the apply function of each column in separate datatables something like this:

apply(data, 2, function(x) data_x<-{count(x)}

It is not working unfortunately.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • can you give an example of your data... – DarrenRhodes Oct 17 '16 at 13:41
  • 2
    Welcome to Stack Overflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Oct 17 '16 at 13:42
  • The thing with the `apply` family of functions is that `*apply` functions have no side effects in terms of `global assignment`. If you really want to assign the outcome of `count(x)` to something globally, then you may want to do: `apply(data, 2, function(x) {data_x<<-count(x)}`. Just keep in mind that `data_x` gets overwritten at every iteration. – Abdou Oct 17 '16 at 13:51
  • 1
    You may want to move *data_x* outside the apply: `data_x <- apply(data, 2, function(x) {count(x)})` but this returns a vector/list not data frame/matrix. – Parfait Oct 17 '16 at 13:54
  • I moved it data_x outside and improved my lapply skills. I have never worked with the apply family. – qwert.maria Oct 19 '16 at 09:49

0 Answers0