recode

Replace a value in a data frame based on a conditional (`if`) statement

拈花ヽ惹草 提交于 2019-11-26 01:42:32
问题 In the R data frame coded for below, I would like to replace all of the times that 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 a for and if statements like so: for(i in junk$nm) if(i %in% \"B\") junk$nm <- \"b\" but as I am sure you can see, this replaces ALL of the values of junk$nm with b

Replace a value in a data frame based on a conditional (`if`) statement

a 夏天 提交于 2019-11-25 23:42:47
In the R data frame coded for below, I would like to replace all of the times that 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 a for and if statements like so: for(i in junk$nm) if(i %in% "B") junk$nm <- "b" but as I am sure you can see, this replaces ALL of the values of junk$nm with b . I can see why this is doing this but I can't seem to get it to replace only those cases of junk$nm where the