Hiesenberg
Hiesenberg

Reputation: 425

calculating standard deviation in R on a dataframe

I have a dataframe in R.

 index seq change change1 change2 change3 change4 change5 change6 
    1      1   0.12  0.34     1.2     1.7     4.5     2.5    3.4
    2      2   1.12  2.54     1.1     0.56    0.87    2.6    3.2
    3      3   1.86  3.23     1.6     0.23    3.4    0.75    11.2
    ...   ...  ...   ...      ...     ...     ...    ...     ...

The name of the dataframe is just FUllData. I can access each column of the FullData using the code:

FullData[2] for 'change'
FullData[3] for 'change1' 
FullData[4] for 'change3'
...
...

Now, I wish to calculate the standard deviation of values in first row of first four columns and so on for all the columns

standarddeviation = sd ( 0.12  0.34     1.2     1.7 )
then 
standarddeviation = sd ( 0.34     1.2     1.7   4.5 )

Above has to be for all rows. so basically I want to calulate sd row wise and the data is stored sort of column wise is it possible to do this. How can I access the row of the data frame with using a for loop on index or seq variable ? How can I do this in R ? is there any better way ?

Upvotes: 1

Views: 2990

Answers (1)

I guess you're looking for something like this.

st.dev=numeric()
for(i in 1:dim(FUllData)[1])
{
   for(j in 1:dim(FUllData)[2])
   {
      st.dev=cbind(st.dev,sd(FUllData[i:dim(FUllData)[1],j:dim(FUllData)[2]]))
   }
}

Upvotes: 0

Related Questions