0

I am trying to get the standard deviation over specific columns in R. Unfortunately there doesnt seem to be a rowmeans type function so I am a bit lost. The dataset is as follows

chr      leftPos strand  JWA JWB JWC JWD OE33_F
chr1    100202137   +     2   0   1   0    0
chr1    100260304   -     141 62  75  55   20
chr1    100724039   -     0   1   0   0    0

and I would like to get the standard deviations for column headers JWA, JWB and JWD as follows. Please note that I dont think this question has been asked before as I am trying to get a simple standard deviation per row rather than as a column standard deviation. Also this is a dataframe which seems to make getting st dev from it a lot trickier than if it was a matrix.

 chr    leftPos   strand  JWA JWB JWC JWD OE33_F stdev
chr1    100202137   +     2   0   1   0    0     x
chr1    100260304   -     141 62  75  55   20    y
chr1    100724039   -     0   1   0   0    0     z

What can I use instead of rowmeans and how can I specify which columns I am interested in

Thanks in advance

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125
  • Does [this](http://stackoverflow.com/questions/25099825/row-wise-variance-of-a-matrix-in-r/25100036#25100036) answer helps you? – David Arenburg Aug 05 '14 at 16:16
  • No as this is a per row st dev im looking for and not per column. Also, getting standard deviations for a dataframe seems to be difficult (as opposed to a matrix). – Sebastian Zeki Aug 05 '14 at 16:18
  • The function there gives you per row variance. This is why it called `RowVar`.... – David Arenburg Aug 05 '14 at 16:19
  • 2
    Try to run `sqrt(RowVar(df[, c("JWA", "JWB", "JWD")]))`. Where `RowVar` is the function I provided a link to, and `df` is your data frame. It will give exactly what you need. – David Arenburg Aug 05 '14 at 16:29
  • You state specifically, "I am trying to get the standard deviation over specific columns in R." and "... I would like to get the standard deviations for column headers JWA, JWB and JWD as follows." And then you say you want it per row, Please clarify what you want. – Lance Roberts Aug 05 '14 at 16:36
  • Just to clarify. The question asks to get the standard deviation per row for specific columns rather than for every value in the row – Sebastian Zeki Aug 05 '14 at 18:13
  • @user3632206, are you kidding me? Did you even try to run the code I've provided? It gives sd per row for the specific columns you want. – David Arenburg Aug 05 '14 at 19:11
  • I was just answering Lance Roberts and hadn't got home yet to try your code David Arenburg- apologies for the confusion. I have run your code as follows > RowVar <- function(x) { + rowSums((x - rowMeans(x))^2)/(dim(x)[2] - 1) + } > sqrt(RowVar(df[, c("JWA", "JWB", "JWD")])) Show Traceback Rerun with Debug Error in df[, c("JWA", "JWB", "JWD")] : object of type 'closure' is not subsettable – Sebastian Zeki Aug 05 '14 at 20:37
  • Mr Arenburg- my apologies. It did in fact work a treat. The error I was getting was because I had defined my dataframe in a previous session and although it was displaying it it wasnt keeping it in memory. I tip my hat to your R wizadry. Thank you – Sebastian Zeki Aug 05 '14 at 20:48

0 Answers0