基于查找表替换数据帧中的值
table
.
> table# P1 P2 P3# 1 cat lizard parrot# 2 lizard parrot cat# 3 parrot cat lizard
lookUp
.
> lookUp# pet class# 1 cat mammal# 2 lizard reptile# 3 parrot bird
new
table
class
lookUp
lapply
new <- as.data.frame(lapply(table, function(x) { gsub('.*', lookUp[match(x, lookUp$pet) ,2], x)}), stringsAsFactors = FALSE)Warning messages:1: In gsub(".*", lookUp[match(x, lookUp$pet), 2], x) : argument 'replacement' has length > 1 and only the first element will be used2: In gsub(".*", lookUp[match(x, lookUp$pet), 2], x) : argument 'replacement' has length > 1 and only the first element will be used3: In gsub(".*", lookUp[match(x, lookUp$pet), 2], x) : argument 'replacement' has length > 1 and only the first element will be used
幕布斯7119047