[R] Changingvalues in data frame
Peter Wolf
s-plus at wiwi.uni-bielefeld.de
Thu Mar 11 13:10:42 CET 2004
Frank Gerrit Zoellner wrote:
>Hi!
>
>I have a question concerning data frames and changing particular values in it.
>
>I have set up a data frame containing n rows of observations od dimension m, so in each row there is a vector of size m. Now I want to introduce a cut off to tha data. Therfore I caluclated the mean and variance within each column using apply:
>
>me<-apply(daten[1:72],2,mean)
>st<-apply(daten[1:72],2,var)
>
>Now I want use the mean and the var to form a cut off value, and apply it to each row of the data frame.
>
>I tried the following
>
>cutoff<-function(x){
> if(x>me+2*st){
> x<-me+2*st
> }
> }
>
>cutd<-apply(daten[1:72],1,cutoff)
>
>but I have to supply somehow the column index to me and st.
>Any Ideas ?
>Thanks,
>Frank
>
>
what about:
x<-as.data.frame(matrix(rnorm(100),20,5))
apply(x,2,function(x){limit<-mean(x)+1*sqrt(var(x));
ifelse(x>limit,1000*limit,x)})
or
apply(x,2,function(x){limit<-mean(x)+2*sqrt(var(x));
ifelse(x>limit,limit,x)})
Peter
More information about the R-help
mailing list