I have following problem: I have a data.frame with two columns. I will 'find the mean of y per grouped value of x'[formulation from hrbrmstr] . Example:
x <- c(1,4,4,2,1,3,4,2,3,4)
y <- c(2.23,5.56,5.53,3.32,2.32,4.21,5.60,3.43,4.32,5.59)
my.data <- data.frame(x,y)
> my.data
x y
1 1 2.23
2 4 5.56
3 4 5.53
4 2 3.32
5 1 2.32
6 3 4.21
7 4 5.60
8 2 3.43
9 3 4.32
10 4 5.59
If I calculate it manually it is:
xx <- c(1,2,3,4)
yy <- c(mean(c(2.23,2.32)),mean(c(3.32,3.43)),mean(c(4.21,4.32)),mean(c(5.56,5.53,5.60,5.59)))
my.data.corr <- data.frame(xx,yy)
The result data.frame should be:
> my.data.corr
xx yy
1 1 2.275
2 2 3.375
3 3 4.265
4 4 5.570
The order of xx in the result doesn't matter.
Thanks for your help!
If you're trying to find the mean of y
per grouped value of x
your example output is wrong.
library(dplyr)
my.data %>%
group_by(x) %>%
summarise(yy=mean(y))
## x yy
## 1 1 2.275
## 2 2 3.375
## 3 3 4.265
## 4 4 5.570
If you're trying to do something else, please re-phrase your question.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments