Skip to content
Prev 277610 / 398506 Next

Replacing values in matrix/dataframe according to changing criteria

This is a little ugly, but I think it should work pretty robustly:

T <- table(unlist(df[,-1])) # Take a close look at this to see how it
works -- it's the key to the whole thing and basically creates
something we will roughly use like a hash-table (if that term is
familiar to you) (or more accurately, like a python dictionary)

T[names(T) == "0"] <- 0 # Set zero back to zero if desired

df[,-1] <- T[match(unlist(df[,-1]), names(T))]

A heads up -- if your data are floating point numbers rather than
integers, you might run into FAQ 7.31 trouble -- that's a much tougher
question to work around, but hopefully still possible

Michael

On Wed, Nov 16, 2011 at 4:25 AM, Frederik Lyngsaa Lang
<frederiklang at gmail.com> wrote: