根据查找表替换DataFrame中的值 [英] Replacing Values in a DataFrame Based on Lookup Table
问题描述
我在R数据帧中替换值有些麻烦。我想替换基于单独表的值。以下是我想要做的一个例子。
I am having some trouble replacing values in a R dataframe. I would like to replace values based on a separate table. Below is an example of what I am trying to do.
我有一张桌子,每一行都是一个客户,每一列都是他们购买的动物。让我们称这个数据框表
。
I have a table where every row is a customer and every column is an animal they purchased. Lets call this dataframe table
.
> table
# P1 P2 P3
# 1 cat lizard parrot
# 2 lizard parrot cat
# 3 parrot cat lizard
我还有一个表,我将引用调用 lookUp
。
I also have a table that I will reference called lookUp
.
> lookUp
# pet class
# 1 cat mammal
# 2 lizard reptile
# 3 parrot bird
我想做的是创建一个名为的新表,新的
与一个函数替换表中的所有值
与 class 列
lookUp
。我自己尝试使用 lapply
功能,但是我收到以下警告。
What I want to do is create a new table called new
with a function replaces all values in table
with the class
column in lookUp
. I tried this myself using an lapply
function, but I got the following warnings.
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 used
2: In gsub(".*", lookUp[match(x, lookUp$pet), 2], x) :
argument 'replacement' has length > 1 and only the first element will be used
3: In gsub(".*", lookUp[match(x, lookUp$pet), 2], x) :
argument 'replacement' has length > 1 and only the first element will be used
任何想法如何使这项工作?谢谢!
Any ideas on how to make this work? Thank you!
推荐答案
你在你的问题中发布了一个不错的方法。这是一个陌生的方法:
You posted an approach in your question which was not bad. Here's a smiliar approach:
new <- df # create a copy of df
# using lapply, loop over columns and match values to the look up table. store in "new".
new[] <- lapply(df, function(x) look$class[match(x, look$pet)])
另一种更快的方法是:
new <- df
new[] <- look$class[match(unlist(df), look$pet)]
请注意,在这两种情况下,我使用空括号( []
)将新结构
保持为它是(一个data.frame)。
Note that I use empty brackets ([]
) in both cases to keep the structure of new
as it was (a data.frame).
(我使用 df
而不是和
查找
而不是查找
(I'm using df
instead of table
and look
instead of lookup
in my answer)
这篇关于根据查找表替换DataFrame中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!