[R] how can i calculate the mean of my data which is only bigger than 75?
R. Michael Weylandt
michael.weylandt at gmail.com
Sat Feb 4 01:37:41 CET 2012
If I understand you correctly, this is what I would do:
rowsWeWant <- apply(dataframename, 1, function(x) any(x[, c("u", "v",
"w1","w2")] > 75])
newDataFrame <- dataframename[rowsWeWant, ]
colMeans(abs(newDataFrame))
I don't really use the subset function, but I imagine you could
replace the first two lines with subset(dataframename, any(u > 75, v >
75, w1 > 75, w2 > 75), x:Z2)
For your other question, I'd do something like subset(dataframename, x
== max(x)) to select the rows with the max values for x. If you want
to do it with the apply() function family, it's not much harder.
Michael
On Thu, Feb 2, 2012 at 4:19 PM, Yakamu Yakamu <iam_yakamu at yahoo.com> wrote:
>
> Hi michael, thanks, but here is more explanations of my questions to have more help, (also pls have a look at the data below):
>
>
>
> Three questions to give more concrete help:
>
> i) Is your data set stored as a matrix or a data.frame
>
> My data is in a data frame
> ii) What are you trying to get the mean of -- all variables pooled or of each variable independently?
>
> Actually I would like to have all the data for either u, v w1 or w2 that are bigger than75, which then there we’ll have new data frame with either u,v, w1 or w2 bigger than 75, doesn’t matter x,y,z1 and z2, they will just follow what ever the results would be (in this new data frame we should still have x,y,z1,z1,u, v,w1 and w2, but only those with the values of u or v or w1 or w2 that are bigger than 75.
>
>
> iii) When you say >=75 for all variables, do you mean only use a row
> if it's >=75 for each element or just only use the >=75 elements for
> each calculation independently.
>
> after we have the new data frame, then I would like to have the mean for x, y, z1 and z2 (the absolute number, without taking consideration the negative signs). If possible, Itshould have all the results altogether (mean of x=.., y=… z1=.. and z2= …)and not one by one.
>
>
>
> Another question, if I would like to create a new data frame with only the maximum data of x (for example if I have 0.456; -0.456; and many more of this values as the maximum values of x ,How can I do it ? (withput taking consideration of the negative signs)
>
> I hope my questions are clear now.
>
> Thanks in advance,
>
> Yakamu
> Michael
>
>
>
> x
>
> y
>
> Z1
>
> Z2
>
> u
>
> v
>
> W1
>
> W2
>
> -0.0077
>
> -0.4665
>
> -0.0048
>
> -0.1302
>
> 70
>
> 26
>
> 59
>
> 54
>
> -0.0028
>
> -0.0055
>
> 0.0026
>
> -0.001
>
> 62
>
> 42
>
> 82
>
> 62
>
> -0.0123
>
> 0.006
>
> -0.003
>
> 0.0029
>
> 74
>
> 18
>
> 83
>
> 78
>
> 0.0232
>
> 0.0367
>
> 0.0028
>
> 0.0027
>
> 65
>
> 34
>
> 74
>
> 78
>
> -0.0075
>
> 0.1141
>
> -0.0018
>
> 0.0363
>
> 63
>
> 0
>
> 77
>
> 69
>
> 0.004
>
> -0.0032
>
> 0.0036
>
> -0.0156
>
> 14
>
> 40
>
> 70
>
> 64
>
> -0.003
>
> -0.0392
>
> -0.006
>
> -0.0212
>
> 55
>
> 42
>
> 63
>
> 69
>
> -0.0116
>
> -0.0028
>
> 0.0031
>
> 0.0209
>
> 59
>
> 23
>
> 69
>
> 35
>
> 0.0171
>
> -0.0496
>
> -0.0055
>
> 0.0118
>
> 35
>
> 57
>
> 73
>
> 42
>
> -0.0135
>
> -0.0324
>
> 0.0001
>
> 0.0004
>
> 55
>
> 45
>
> 57
>
> 55
>
> 0.0345
>
> 0.004
>
> 0.0041
>
> 0.0079
>
> 77
>
> 38
>
> 57
>
> 71
>
> -0.0206
>
> -0.0152
>
> 0.003
>
> 0.0104
>
> 55
>
> 30
>
> 56
>
> 81
>
> -0.0044
>
> 0.0343
>
> 0.0059
>
> 0.0105
>
> 74
>
> 52
>
> 58
>
> 75
>
> 0.0138
>
> -0.065
>
> 0.0016
>
> -0.0064
>
> 68
>
> 64
>
> 70
>
> 56
>
> -0.0303
>
> 0.0012
>
> -0.009
>
> 0.0025
>
> 66
>
> 32
>
> 42
>
> 52
>
> -0.0231
>
> 0.0379
>
> -0.0006
>
> 0.0116
>
> 70
>
> 49
>
> 61
>
> 34
>
> 0.0305
>
> 0.078
>
> -0.0081
>
> -0.0082
>
> 83
>
> 45
>
> 22
>
> 18
>
> -0.03
>
> 0.0978
>
> 0.0118
>
> 0.0103
>
> 88
>
> 25
>
> 31
>
> 68
>
> 0.0072
>
> -0.0019
>
> 0.0049
>
> 0.0055
>
> 79
>
> 50
>
> 67
>
> 71
>
>
>
> --- On Wed, 2/1/12, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:
>
>
> From: R. Michael Weylandt <michael.weylandt at gmail.com>
> Subject: Re: [R] how can i calculate the mean of my data which is only bigger than 75?
> To: "Yakamu Yakamu" <iam_yakamu at yahoo.com>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Date: Wednesday, February 1, 2012, 12:47 PM
>
> I'm not entirely sure what you mean, but it's likely one of these:
>
> apply(data, 2, function(x) mean(x[x>75]))
> mean(data[ apply(data,1, function(x) all(x > 75), ])
> mean(data[data>75])
>
> Three questions to give more concrete help:
>
> i) Is your data set stored as a matrix or a data.frame
> ii) What are you trying to get the mean of -- all variables pooled or
> of each variable independently?
> iii) When you say >=75 for all variables, do you mean only use a row
> if it's >=75 for each element or just only use the >=75 elements for
> each calculation independently.
>
> Michael
>
More information about the R-help
mailing list