-1

I used these CMDs:

x <- read.csv("path"header=TRUE)

d153 = x$MG.153.m4

range(d153,na.rm=TRUE)

breaks = seq(0,1900, by=100) #1850 is max, 11 is min

d153.cut  = cut(d153,breaks,right=FALSE)

d153.freq = table(d153.cut)

cbind(d153.freq)

This gives only one of 96 column in "x". How can I run all these CMD for each column at the same time and arrange them in the same table?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2966591
  • 93
  • 1
  • 9
  • I understand that cbind(d153.freq, x, y, x) etc. places all in one table. But still, I need a CMD to handle all columns at one time. :) – user2966591 Nov 23 '13 at 13:57
  • 1
    You are much more likely to receive a helpful answer if you provide a [_minimal, reproducible_ example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) together with the code you have tried. Thanks! – Henrik Nov 23 '13 at 15:55

1 Answers1

0

Write a function that will construct a dataframe with counts and labels for the breakpoints to be applied to each column of your dataframe (or at least the ones with numeric values.) It's not clear whether the range is the same for all columns and I am going to assume they are different so the cut function will be handed the responsibility of determining ranges and (20) breakpoints:

tbl.to.col <- function(z) {col = deparse(substitute(z))
               res <-cbind( dataframe(counts=table(cut(z, 20)), 
                                      labels=names(table(cut(z, 20))) )
               names(res) <- paste(col, names(res), sep="_")
               res}
dflist <- lapply(x[ , is.numeric(x)] , tbl.to.col)
big.dfrm <- do.call(cbind, dflist) # will have 2*number of numeric cols

(Untested in the absence of data.)

IRTFM
  • 258,963
  • 21
  • 364
  • 487