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 -- Frank G. Zoellner AG Angewandte Informatik Technische Fakult"at Universit"at Bielefeld phone: +49(0)521-106-2951 fax: +49(0)521-106-2992 email: fzoellne at techfak.uni-bielefeld.de
Hello Frank, does the following example, what you want? Thomas P. ## some test data x <- data.frame(matrix(rnorm(72*10, mean=50, sd=20), ncol=10)) me <- colMeans(x) sd <- apply(x, 2, sd) coff <- me + 2*sd # see ?t and ?pmax x2 <- t(pmin(t(x),coff)) # test it x-x2
Hello Frank, does the following example, what you want? Thomas P. ## some test data x <- data.frame(matrix(rnorm(72*10, mean=50, sd=20), ncol=10)) me <- colMeans(x) sd <- apply(x, 2, sd) coff <- me + 2*sd # see ?t and ?pmax x2 <- t(pmin(t(x),coff)) # test it x-x2
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