简体   繁体   English

如何计算R中未对齐的数据帧列的平均值

[英]How to compute the mean of data frames columns that are not aligned in R

I have two data frames that have the same column names x and y. 我有两个具有相同列名称x和y的数据框。

Both store data y = f(x) but the x values are not aligned. 两者都存储数据y = f(x),但x值未对齐。

What I want is to compute a third data frame that would provide the mean values of both data frames (where they intersect). 我想要的是计算第三个数据帧,该数据帧将提供两个数据帧(它们相交的位置)的平均值。

Is there a way to do that in R? 有没有办法在R中做到这一点?

Here is a sample code with two sin curves. 这是带有两个正弦曲线的示例代码。 What I'm looking for is the "curve of the mean values", which in this example should be sin(x) + 0.1. 我正在寻找的是“平均值曲线”,在此示例中应为sin(x)+ 0.1。 In real life, I want to perform the operation on real data though. 在现实生活中,我想对实际数据执行操作。

x1 <- seq(1, 10, by = 0.1)
x2 <- seq(0.5, 9.5, by = 0.11)
y1 <- sin(x1) + 0.2
y2 <- sin(x2)
df1 <- data.frame(x = x1, y = y1)
df2 <- data.frame(x = x2, y = y2)

df.combined <- rbind(df1, df2)
df.combined$id <- factor(rep(1:2, c(length(x1), length(x2))))

ggplot(data = df.combined, aes(x, y, colour = id)) + geom_line()

With real data I would build a model for both the curves (with a loess method for example), computing the values of two models at all the points and then compute the mean. 对于真实数据,我将为两条曲线建立模型(例如,采用loess法),计算所有点上两个模型的值,然后计算均值。

df<-merge(df1,df2,by="x",all = TRUE) 

mod1<-loess(y1 ~ x1,span=0.4)
mod2<-loess(y2 ~ x2,span=0.4)
df$fitmod1<-predict(mod1,df$x)
df$fitmod2<-predict(mod2,df$x)
df$mid<-(df$fitmod1+df$fitmod2)/2

ggplot(df,aes(x=x))+geom_line(aes(y=fitmod1),color="red")+
geom_line(aes(y=fitmod2),color="green")+geom_line(aes(y=mid),color="black")

Of course using a loess or something else is strongly dependent on the data.. 当然,使用loess或其他物质在很大程度上取决于数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM