Suppose we have the following data:
0.1876798 0.5425627
0.0000000 0.4680425
0.2812897 0.2763695
-0.2048694 0.4441289
0.0000000 0.5694079
The data have two columns as above, the first one is beta1 hat the second one is for beta 2 hat.
I want R codes to get
0.1876798 0.5425627
0.2812897 0.2763695
-0.2048694 0.4441289
I want R codes to delete the rows that have zeros, and find the
average for the first column and second column
Note, I want to apply the code for 800 observations.
beta1=c(0.1876798,0.0000000,0.2812897,-0.2048694,0.0000000)
> beta2=c(0.5425627,0.4680425,0.2763695,0.4441289
,0.5694079)
> dd = data.frame(beta1,beta2)
>
> ##Go through each row and determine if a value is zero
> row_sub = apply(dd, 1, function(row) all(row !=0 ))
> ##Subset as usual
> new=dd[row_sub,]
> new
beta1 beta2
1 0.1876798 0.5425627
3 0.2812897 0.2763695
4 -0.2048694 0.4441289
> mean_values=apply(new, 2, mean)
> mean_values
beta1 beta2
0.08803337 0.42102037
Get Answers For Free
Most questions answered within 1 hours.