How to mutate rows of data frame - replacing one value with another

Posted by rhh on Stack Overflow See other posts from Stack Overflow or by rhh
Published on 2010-05-12T21:57:49Z Indexed on 2010/05/12 22:04 UTC
Read the original article Hit count: 294

Filed under:

I'm having trouble with what I think is a basic R task.

Here's my sample dataframe named 'b'

Winner Color Size
Tom Yellow Med
Jerry Yellow Lar
Jane Blue Med

where items in the Winner column are factors.

I'm trying to change "Tom" in the dataframe to "Tom LLC" and I can't get it done.

Here's what I tried:

Simple way: b$winner[b$winner=='Tom'] = as.factor('Tom LLC')

but that failed with "invalid factor level, NAs generated"

Next I tried a more advanced route:

name_reset = function (x, y, z) {
if (x$winner == y) {x$winner = z}
}

b = adply(b,1,name_reset,'Tom','Tom LLC')

but that failed with "Error in list_to_dataframe(res, attr(.data, "split_labels")) : Results are not equal lengths"

I feel I'm missing something basic. Can someone redirect me or offer suggestions on the code I wrote above? Thank you very much

© Stack Overflow or respective owner

Related posts about r