Could also do it in one apply i realized.. apply(m,1:2,FUN=function(x) {y<-as.numeric(strsplit(x,split=",")[[1]]);y[which(y <= 3)]<-0;paste(y,collapse=",")})
On 06.07.2012, at 10:18, Jessica Streicher wrote: > sap<-sapply(strsplit(m,","),as.numeric) > sap[which(sap <= 3)]<-0 > mNew<-matrix(apply(sap,2,FUN=function(x){paste(x,collapse=",")}),ncol=4) > > works? > > On 06.07.2012, at 08:47, Sarah Auburn wrote: > >> Hi, >> I am trying to format some data (example matrix "m" below) for which each >> data point has 2 associated values separated by a comma. >> I want to replace values <3 with "0" to give the example output below. >> I have got as far as: >> out<-lapply(strsplit(m,","),as.numeric) >> Failed to identify anything along the lines of "[out<3]<-0" that works... >> Thank you for any help! >> Sarah >> >> m<-matrix(c("1,6", "0,12", "130,12", "0,0", "123,4", "2,2","3,7","9,6", >> "0,0", "12,2", "1,2", "0,3"), ncol=4) >> >> required output: >> [,1] [,2] [,3] [,4] >> [1,] "0,6" "0,0" "0,7" "12,0" >> [2,] "0,12" "123,4" "9,6" "0,0" >> [3,] "130,12" "0,0" "0,0" "0,0" >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> 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. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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.