简体   繁体   中英

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. 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:

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. 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...

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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