[R] Adding negative values with mean of positive values
arun
smartpink111 at yahoo.com
Sun Aug 11 20:10:54 CEST 2013
Hi,
You may try this:
vec1<-unique(data1[,1])
res<-do.call(rbind,lapply(seq_along(vec1),function(i) {x1<-data1[data1[,1]%in%vec1[-i],]; x2<-mean(x1$value[x1$value>0]); x3<- data1[data1[,1]%in% vec1[i],]; x3$value[x3$value<0]<-x3$value[x3$value<0]+x2; x3}))
res1<-res[match(row.names(dat1),row.names(res)),]
A.K.
My example dataset is below.
data1<-structure(list(Group = c("c", "c", "b", "e", "b", "b", "e", "b",
"b", "c", "b", "c", "d", "e", "a", "c", "e", "e", "b", "c", "e",
"a", "b", "c", "a", "d", "c", "a", "c", "d"), value = c(1.65766756554083,
-1.00159810943707, 0.828016940090355, -0.0942360302636258, 0.110184789532348,
0.501357929769482, -0.843610465892682, 0.112544248337201, 0.83530947271874,
1.54896291279315, -0.668032450079063, -0.328748306664628, -0.15429551998755,
1.58892084606551, -0.45892429914377, -2.15596722591948, 0.898111157005135,
-0.287228845053069, -1.54706640415041, -0.780803473387221, 1.54955826561218,
-0.506014575029359, -1.66808530086564, -0.845467401060001, 1.92688611565505,
0.46643424929481, 0.293821259924534, 0.151536430922866, -1.62429048404794,
0.444694790051071)), .Names = c("Group", "value"), row.names = c(NA,
-30L), class = "data.frame")
Here, i need to change the negative values by adding with the
mean of the positive values. The condition is that the mean of the
positive values should exclude the positive values on the group on which
this is carried out. For example, the 'a' group negative values should
be added with the mean from the positive values from groups
'c","d',"e","b". Similarly for other groups.
Thanks.
More information about the R-help
mailing list