Dear R-users, I am sorry if I ask for something that has been asked before, however, I still could not solve my little problem by consulting the previous thread on this topic: I would like to replace several values in a data frame, such as in: colorful subject response 1 me black 2 me brown 3 you red 4 me black 5 you brown read in with read.table() I would like to replace both "black" and "brown" by "dark". What I have tried was: ToBeReplaced=c("black","brown") Replacement="dark" test1<-replace(colorful,ToBeReplaced,Replacement) which adds two columns: test1 subject response black brown 1 me black dark dark 2 me brown dark dark .... Replacing one value at a time as in: test2<-replace(colorful$response,colorful$response=="black","dark") gives this warning message: In test$response : $ operator is invalid for atomic vectors, returning NULL and returns all values for $response:> test2[1] black brown red black ..... I have tried many other things but nothing helps. I would be very grateful for your help!!! Thanks a lot, Silvia
Try this: colorful$response <- factor(colorful$response) levels(colorful$response)[levels(colorful$response) %in% ToBeReplaced] <- Replacement On 29/02/2008, Silvia Lipski <sillispiral at yahoo.com> wrote:> Dear R-users, > > I am sorry if I ask for something that has been asked > before, however, I still could not solve my little > problem by consulting the previous thread on this > topic: > > I would like to replace several values in a data > frame, such as in: > > colorful > subject response > 1 me black > 2 me brown > 3 you red > 4 me black > 5 you brown > > read in with read.table() > > I would like to replace both "black" and "brown" by > "dark". > > What I have tried was: > > ToBeReplaced=c("black","brown") > Replacement="dark" > test1<-replace(colorful,ToBeReplaced,Replacement) > > which adds two columns: > > test1 > subject response black brown > 1 me black dark dark > 2 me brown dark dark > .... > > Replacing one value at a time as in: > test2<-replace(colorful$response,colorful$response=="black","dark") > > gives this warning message: > In test$response : > $ operator is invalid for atomic vectors, returning > NULL > > and returns all values for $response: > > test2 > [1] black brown red black ..... > > I have tried many other things but nothing helps. > I would be very grateful for your help!!! > > Thanks a lot, > Silvia > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
See ?recode in the car package. On Fri, Feb 29, 2008 at 8:50 AM, Silvia Lipski <sillispiral at yahoo.com> wrote:> Dear R-users, > > I am sorry if I ask for something that has been asked > before, however, I still could not solve my little > problem by consulting the previous thread on this > topic: > > I would like to replace several values in a data > frame, such as in: > > colorful > subject response > 1 me black > 2 me brown > 3 you red > 4 me black > 5 you brown > > read in with read.table() > > I would like to replace both "black" and "brown" by > "dark". > > What I have tried was: > > ToBeReplaced=c("black","brown") > Replacement="dark" > test1<-replace(colorful,ToBeReplaced,Replacement) > > which adds two columns: > > test1 > subject response black brown > 1 me black dark dark > 2 me brown dark dark > .... > > Replacing one value at a time as in: > test2<-replace(colorful$response,colorful$response=="black","dark") > > gives this warning message: > In test$response : > $ operator is invalid for atomic vectors, returning > NULL > > and returns all values for $response: > > test2 > [1] black brown red black ..... > > I have tried many other things but nothing helps. > I would be very grateful for your help!!! > > Thanks a lot, > Silvia > > ______________________________________________ > 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. >
On Friday 29 February 2008 (14:50:53), Silvia Lipski wrote:> Dear R-users, > > I am sorry if I ask for something that has been asked > before, however, I still could not solve my little > problem by consulting the previous thread on this > topic: > > I would like to replace several values in a data > frame, such as in: > > colorful > ? subject response > 1 ? ? ?me ? ?black ? > 2 ? ? ?me ? ?brown ? > 3 ? ? you ? ? ?red ? > 4 ? ? ?me ? ?black ? > 5 ? ? you ? ?brown ? > > read in with read.table() > > I would like to replace both "black" and "brown" by > "dark".What about: colorful <- within(colorful, test1 <- replace(response, c("black","brown"), "dark" ) ) or colorful <- within(colorful, test1 <- response test1[test1 %in% c("black","brown")] <- "dark" )