[R] transform

Jim Lemon drjimlemon at gmail.com
Sat Oct 1 09:50:47 CEST 2016


Hi Val,
Perhaps like this?

valdat<-read.table(text="obs, Year, bb, kk,  y
1,  2001, 25 ,100, 12.6
2,  2001,  15 ,111, 24.7
3,  2001,  53, 110, 13.8
4,  2001,  50, 75,  9.6
5, 2001,  125, 101, 31.5
6,  2001,  205, 407, 65.7
7,  2001,  250, 75, 69.1",sep=",",header=TRUE)

selectval<-valdat$bb > 75 & valdat$kk > 100
valdat$w<-NA
meanselecty<-mean(valdat$y[selectval],na.rm=TRUE)
sdselecty<-sd(valdat$y[selectval],na.rm=TRUE)
valdat$w<-((valdat$y-48.6)/24.183)*3 +10
valdat

Jim


On Sat, Oct 1, 2016 at 10:23 AM, Val <valkremk at gmail.com> wrote:
> Hi all,
> I want to standardize  a variable based on certain condition
> Here is my sample of data
>
> obs, Year, bb, kk,  y
> 1,  2001, 25 ,100, 12.6
> 2,  2001,  15 ,111, 24.7
> 3,  2001,  53, 110, 13.8
> 4,  2001,  50, 75,  9.6
> 5, 2001,  125, 101, 31.5
> 6,  2001,  205, 407, 65.7
> 7,  2001,  250, 75, 69.1
>
> If the value of "bb" is  greater than 75 and the value of "kk" is
> greater than 100 then I want get  the mean and STD values of "y".  In
> this example obs 5 and 6 satisfy the condition.  The mean is 48.6 and
> STDEV is 24.18
> Then I want to transform the "y" values  with  mean = 10 and SDTEV of 3.
>  w= ((y- 48.6)/24.183)*3 +10
>
> My question, how can i pas those mean  and STDEV values  to create the
> new variable (w)?
>
> The result looks as follows
>
> obs, Year, bb, kk,  y,     w
> 1, 2001, 25 ,100 , 12.6,  5.534062435
> 2, 2001, 15, 111,  24.7,   7.035113672
> 3, 2001, 53, 110, 13.8,   5.68292702
> 4, 2001, 50, 75,   9.6,    7.642977396
> 5, 2001, 125, 101, 31.5,   7.878679656
> 6, 2001, 205, 407, 65.7,  12.12132034
> 7, 2001, 250, 75 , 69.1, 12.54310334
>
> Thank you in advance
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list