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

I am having problems with what I consider the main task of R.

Here's my example framework named 'b'

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

      

where the items in the Winner column are factors.

I am trying to change "Tom" in dataframe to "Tom LLC" and I cannot do it.

Here's what I've tried:

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

but it failed with "invalid factor level generated by NA"

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 with the error "Error in list_to_dataframe (res, attr (.data," split_labels ")): Results are not equal in length

I feel like I am missing something basic. Can someone redirect me or suggest suggestions for the code I wrote above? Many thanks

+2


a source to share


2 answers


What you want to do is change the values ​​across the levels. The levels give you access to the labels in the odds. Calling it on a factor shows the labels, and assigning a level function overwrites the labels for the factor.

Once you start working with the levels function, you can change the values ​​you want. I think gsub is probably the simplest.

Try the following:



levels(b$Winner) <- gsub("Tom", "Tom LLC", levels(b$Winner))

      

-mcpeterson

+9


a source


I made your dataframe and then used dput()

to make it in a format that would allow people to easily copy / paste it from the web:

b <- structure(list(Winner = c("Tom", "Jerry", "Jane"), Color = c("Yellow", 
"Yellow", "Blue"), Size = c("Med", "Lar", "Med")), .Names = c("Winner", 
"Color", "Size"), row.names = c(NA, -3L), class = "data.frame")

      

I'm not sure what exactly means as.factor()

in your code. as.factor

converts vectors of values ​​to factors - it doesn't really do anything meaningful for a single value. If b $ Winner is a character vector, this works:

b$Winner[dat$Winner %in% "Tom"] <- "Tom LLC"

      



If b $ Winner is a factor, then "Tom LLC" must be one of the levels for you to insert into the factor. If b $ Winner is a factor, I would probably do the following:

levels(b$Winner) <- c("Tom LLC", "Jerry", "Jane")

      

which simply says R should replace the possible winner values ​​(i.e. b $ Winner levels). Some of the advanced R users here suggest setting strAsAsFactors to FALSE ... and the more I use R, the more I agree. It is much easier to manipulate simple string vectors and then insert them into the coefficient as needed.

+3


a source







All Articles