我在R A data.frame,像這樣:在數據幀替換列值,不包括在列表中
fruits
X1 X2 X3
aa kiwi 15
ba orange 25
cc lemon 23
ba apple 17
cc lemon 19
cc orange 18
cc orange 21
ba banana 17
我想替換在列X2的所有值除「橙色」和「檸檬「與」其他「。如何在R中做到這一點?
實施例的數據:
fruits <- structure(list(X1 = structure(c(1L, 2L, 3L, 2L, 3L, 3L, 3L, 2L
), .Label = c("aa", "ba", "cc"), class = "factor"), X2 = structure(c(3L,
5L, 4L, 1L, 4L, 5L, 5L, 2L), .Label = c("apple", "banana", "kiwi",
"lemon", "orange"), class = "factor"), X3 = c(15L, 25L, 23L,
17L, 19L, 18L, 21L, 17L)), .Names = c("X1", "X2", "X3"), class = "data.frame", row.names = c(NA,
-8L))