Hey guys, sorry for the inconvenience (this might be a hundred times answered question), but I have been searching a while and gave up about the following: I have the following, table and data: table <- seq(255, 0, by=-1) data <- c(1,8,...) <--- doesn't matter what's in here Which would be the most efficient way to replace each data value, v_i, by table[v_i + 1] ? And, maybe more important, why is that more efficient? I used this, but I think is not the best option: for (i in 1:length(data)){ data[i] <- table[data[i]+1] } Then used mapply, that seems to work better: f<-function(x){ 255-x } mapply(f, data) Thanks guys in advance, Walter
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Walter Alini > Sent: Tuesday, October 23, 2007 1:18 PM > To: r-help at r-project.org > Subject: [R] Replace values on seq > > Hey guys, sorry for the inconvenience (this might be a hundred times > answered question), but I have been searching a while and gave up > about the following: > > I have the following, table and data: > > table <- seq(255, 0, by=-1) > data <- c(1,8,...) <--- doesn't matter what's in here > > Which would be the most efficient way to replace each data value, v_i, > by table[v_i + 1] ? > And, maybe more important, why is that more efficient? > > I used this, but I think is not the best option: > > for (i in 1:length(data)){ > data[i] <- table[data[i]+1] > } > > Then used mapply, that seems to work better: > > f<-function(x){ > 255-x > } > > mapply(f, data) > > Thanks guys in advance, > WalterHow about something like data<-table[data+1] Hope this is helpful, Dan Daniel J. Nordlund Research and Data Analysis Washington State Department of Social and Health Services Olympia, WA 98504-5204
Is this what you want to do?> table <- seq(255, 0, by=-1) > data <- c(1,8,34,100) > data <- table[data + 1] > data[1] 254 247 221 155 On 10/23/07, Walter Alini <walteralini at gmail.com> wrote:> Hey guys, sorry for the inconvenience (this might be a hundred times > answered question), but I have been searching a while and gave up > about the following: > > I have the following, table and data: > > table <- seq(255, 0, by=-1) > data <- c(1,8,...) <--- doesn't matter what's in here > > Which would be the most efficient way to replace each data value, v_i, > by table[v_i + 1] ? > And, maybe more important, why is that more efficient? > > I used this, but I think is not the best option: > > for (i in 1:length(data)){ > data[i] <- table[data[i]+1] > } > > Then used mapply, that seems to work better: > > f<-function(x){ > 255-x > } > > mapply(f, data) > > Thanks guys in advance, > Walter > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
> Hope this is helpful,Certainly! Thanks Dan and Jim! Now, I wonder where I can dig in to realize why this is better. Is C code for this function better than others? Is C guilty on this? Maybe I am too newbie. Thanks again, guys! Walter
> I will report results then,OK, its better this way, but no so much. Before: 99.79 (a) vs 0.21 (b) Now: 99.77 (a) vs 0.23 (b) But these results continue to be surprising for me. Ok. Thanks you guys again, I will continue bothering you sometime. Walter