uaca@alumni.uv.es
2003-Oct-27 16:42 UTC
[R] assign a constant to a different column for each row
Hi all I want to assign a constant to a different column for each row eg: m[1,2] <- 0; m[2,3] <- 0; m[3,1] <- 0; m[4,2] <- 0; m[5,1] <- 0; ... etc... i've tried apply/tapply with no luck and also the following coefs <- rtt.abs[,5:8]; coefs.i <- coefs[] == 1; coefs[coefs.i] <- 0; wich results in "matrix subscripts not allowed in replacement" error message, for wich I have not found any workaround a trivial for() loop is not fast enough any help would be greatly appreciated, (maybe a gun too) I'm disperated :-\ Thanks in advance Ulisses Debian GNU/Linux: a dream come true ----------------------------------------------------------------------------- "Computers are useless. They can only give answers." Pablo Picasso ---> Visita http://www.valux.org/ para saber acerca de la <--- ---> Asociaci?n Valenciana de Usuarios de Linux <---
Prof Brian Ripley
2003-Oct-27 17:15 UTC
[R] assign a constant to a different column for each row
m[1:5 + nrow(m)*c(2,3,1,2,1)] <- 0 if m is a matrix. Remember you can index a matrix as a vector. If m is a data frame (you didn't say what it is) I would loop over columns (not rows) explicitly, since the code is going to do that implicitly. On Mon, 27 Oct 2003 uaca at alumni.uv.es wrote:> > Hi all > > I want to assign a constant to a different column for each row > > eg: > > m[1,2] <- 0; > m[2,3] <- 0; > m[3,1] <- 0; > m[4,2] <- 0; > m[5,1] <- 0; > ... > > etc... > > i've tried apply/tapply with no luck > > and also the following > > coefs <- rtt.abs[,5:8]; > coefs.i <- coefs[] == 1; > coefs[coefs.i] <- 0; > > wich results in > > "matrix subscripts not allowed in replacement" > > error message, for wich I have not found any workaround > > a trivial for() loop is not fast enough > > any help would be greatly appreciated, (maybe a gun too) I'm disperated :-\-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
uaca@alumni.uv.es
2003-Oct-27 17:18 UTC
[R] Re: assign a constant to a different column for each row
Hi again Thanks all for your fast! reply regards Ulisses Debian GNU/Linux: a dream come true ----------------------------------------------------------------------------- "Computers are useless. They can only give answers." Pablo Picasso ---> Visita http://www.valux.org/ para saber acerca de la <--- ---> Asociaci?n Valenciana de Usuarios de Linux <---
uaca at alumni.uv.es wrote:> Hi all > > I want to assign a constant to a different column for each row > > eg: > > m[1,2] <- 0; > m[2,3] <- 0; > m[3,1] <- 0; > m[4,2] <- 0; > m[5,1] <- 0; > ... > > etc... > > i've tried apply/tapply with no luck > > and also the following > > coefs <- rtt.abs[,5:8]; > coefs.i <- coefs[] == 1; > coefs[coefs.i] <- 0; > > wich results in > > "matrix subscripts not allowed in replacement" > > error message, for wich I have not found any workaround > > a trivial for() loop is not fast enough > > any help would be greatly appreciated, (maybe a gun too) I'm disperated :-\ > > Thanks in advance > > Ulisses > > > Debian GNU/Linux: a dream come true > ----------------------------------------------------------------------------- > "Computers are useless. They can only give answers." Pablo Picasso > > ---> Visita http://www.valux.org/ para saber acerca de la <--- > ---> Asociaci?n Valenciana de Usuarios de Linux <--- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >> cbind(c(1,2,3),c(4,5,6),c(6,7,8))->l > l [,1] [,2] [,3] [1,] 1 4 6 [2,] 2 5 7 [3,] 3 6 8 > l[1:2,1:3]<-4 > l [,1] [,2] [,3] [1,] 4 4 4 [2,] 4 4 4 [3,] 3 6 8 >