[英]finding the mean and replacing the values in the same dataframe in R
I have this data set:I want to find the average Viability for any row that wt.Df and founder are the same. 我有这个数据集:我想找到wt.Df和创始人相同的任何行的平均可行性。 I then want to replace those values in the data set 然后我想要替换数据集中的那些值
Store;
founder wt.Df Replicate Block Food_Source Viability
1 A4 5905 1 1 Regular 0.9523810
2 A4 24834 1 1 Regular 0.8095238
3 A4 24834 2 1 Regular 0.8571429
4 A4 27861 1 1 Regular 0.8095238
5 A4 27861 2 1 Regular 0.9230769
12 A3 5905 1 1 Regular 0.9473684
13 A3 24834 1 1 Regular 0.9047619
14 A3 27861 1 1 Regular 0.8571429
I know this piece of code will find the average between like points, but I dont know how to replace in the data set 我知道这段代码会找到相似点之间的平均值,但我不知道如何替换数据集
tmp<- with(Store, mean(Viability[wt.Df == 27861 & founder == "A4"]))
Wanted output: 通缉输出:
founder wt.Df Replicate Block Food_Source Viability
1 A4 5905 1 1 Regular 0.9523810
2 A4 24834 1 1 Regular 0.8333333
4 A4 27861 1 1 Regular 0.8663004
12 A3 5905 1 1 Regular 0.9473684
13 A3 24834 1 1 Regular 0.9047619
14 A3 27861 1 1 Regular 0.8571429
There's a couple of good options that spring to mind. 我想到了几个不错的选择。 Firstly, plain old aggregate
from the base
package: 首先,来自base
包的普通旧aggregate
:
aggregate( Viability ~ wt.Df + founder , FUN = mean , data = store )
# wt.Df founder Viability
#1 5905 A3 0.9473684
#2 24834 A3 0.9047619
#3 27861 A3 0.8571429
#4 5905 A4 0.9523810
#5 24834 A4 0.8333333
#6 27861 A4 0.8663003
Another good option is to use the data.table package and aggregate by grouping variables. 另一个好的选择是使用data.table包并通过分组变量进行聚合。 I also take the first record of each group for the remaining columns eg Block = Block[1]
as that is what you have in your results... 我还为剩下的列获取每组的第一条记录,例如Block = Block[1]
因为这就是你在结果中所拥有的......
require( data.table )
store <- data.table( store )
store[ , list( Viability = mean(Viability) , Block = Block[1], Replicate = Replicate[1] ) , by = list( wt.Df , founder ) ]
# wt.Df founder Viability Block Replicate
#1: 5905 A4 0.9523810 1 1
#2: 24834 A4 0.8333333 1 1
#3: 27861 A4 0.8663003 1 1
#4: 5905 A3 0.9473684 1 1
#5: 24834 A3 0.9047619 1 1
#6: 27861 A3 0.8571429 1 1
I would try generating a summary data set and then merging them. 我会尝试生成摘要数据集,然后合并它们。
library(gdata)
library(plyr)
avg_summary <- ddply(Store, .(wt.DF, founder), summary, viability1 = mean(Viability))
Store <- join(Store, avg_summary)
# delete original Viability column
Store$Viability <- NULL
# rename viability1 -> Viability
Store <- rename.vars(Store, 'viability1', 'Viability')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.