In the R data frame encoded below, I would like to replace all the times when B appears with B
junk <- data.frame(x <- rep(LETTERS[1:4], 3), y <- letters[1:12]) colnames(junk) <- c("nm", "val")
this provides:
nm val 1 A a 2 B b 3 C c 4 D d 5 A e 6 B f 7 C g 8 D h 9 A i 10 B j 11 C k 12 D l
My initial attempt was to use the for and if :
for(i in junk$nm) if(i %in% "B") junk$nm <- "b"
but as I am sure you can see, this replaces ALL junk$nm values ββwith B I can understand why this is done, but I can not get it to replace only those cases of garbage $ nm, where the original value was B
Thank.
NOTE. I managed to solve the problem with gsub , but in the interest of learning R, I would still like to know how to get my original approach to work (if possible)
r
KennyPeanuts Apr 28 '11 at 19:53 2011-04-28 19:53
source share