[英]Reverse lookup for loop in R
I have a set of numbers / string that makes other number / string. 我有一组数字/字符串,使其他数字/字符串。 I need to create a function that gives me a list of the all the numbers / string needed to create that number / string. 我需要创建一个函数,该函数为我提供创建该数字/字符串所需的所有数字/字符串的列表。
Consider the following dataset 考虑以下数据集
ingredients <- c('N/A', 'cat', 'bird')
product <- c('cat', 'bird', 'dog')
data <- data.frame(ingredients, product)
head(data)
If I input function(dog), I would like a list that returns bird and then cat. 如果我输入function(dog),我想要一个先返回bird然后再返回cat的列表。 The function knows when to stop when ingredients = N/A (there's nothing more to look up). 该功能知道当配料= N / A时什么时候停止(没有什么可查找的)。
It seems like some of sort of for loop that appends is the right approach. 似乎附加某种for循环是正确的方法。
needed <- list()
for (product in list){
needed[[product]]<-df
}
df <- dplyr::bind_rows(product)
I appended your initial code to make N/A
simply equal to NA
so I could use the is.na
function in my R code. 我附加了您的初始代码,以使N/A
仅等于NA
因此可以在R代码中使用is.na
函数。 Now the sample data is 现在样本数据是
ingredients <- c(NA, 'cat', 'bird')
product <- c('cat', 'bird', 'dog')
data <- data.frame(ingredients, product)
Code is below: 代码如下:
ReverseLookup <- function (input) {
ans <- list()
while (input %in% data$product) {
if (!is.na(as.character(data[which(data$product == input),]$ingredients))) {
ans <- append(ans, as.character(data[which(data$product == input),]$ingredients))
input <- as.character(data[which(data$product == input),]$ingredients)
}
else {
break
}
}
print(ans)
}
I create an empty list and then create a while
loop that just checks if the input
exists in the product
column. 我创建一个空列表,然后创建一个while
循环,该循环仅检查product
列中是否存在input
。 If so, it then checks to see if the corresponding ingredient
to the product
input is a non-NA value. 如果是这样,那么它将检查以查看product
输入的相应ingredient
是否为非NA值。 If that's the case, the ingredient
will be appended to ans
and will become the new input
. 如果是这种情况,该ingredient
将被附加到ans
并将成为新的input
。 I also added a break
statement to get out of the while
loop when you reach an NA
. 我还添加了break
语句,以在到达NA
时退出while
循环。
I did a quick test on the case where there is no NA
in your dataframe and it appears to be working fine. 我对您的数据框中没有NA
且看起来工作正常的情况进行了快速测试。 Maybe someone else here can figure out a more concise way to write this, but it should work for you. 也许这里的其他人可以找到一种更简洁的方式编写此代码,但是它应该对您有用。
You can likely find a way to use a tree of some type to work through nodes. 您可能会找到一种使用某种类型的树遍历节点的方法。 But, using a recursive function in base R, I have come up with this. 但是,在基数R中使用递归函数,我想到了这一点。
I have also changed the 'N/A' to NA
to make life easier. 我也将“ N / A”更改为NA
以使生活更轻松。 Also, I have added in stringsAsFactors = F
to the data frame. 另外,我在数据帧中添加了stringsAsFactors = F
。
ingredients <- c(NA, 'cat', 'bird')
product <- c('cat', 'bird', 'dog')
data <- data.frame(ingredients, product, stringsAsFactors = F)
reverse_lookup <- function(data, x, last_result = NULL) {
if (! is.null(last_result)) {
x <- data[data$product == last_result[length(last_result)], "ingredients"]
}
if (! is.na(x)) {
last_result <- reverse_lookup(data, x, c(last_result, x))
}
last_result
}
This returns the input as well, which you can always drop off as the first element of the vector. 这也会返回输入,您可以随时将其作为向量的第一个元素删除。
> reverse_lookup(data, "dog")
[1] "dog" "bird" "cat"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.