R-多个循环计数器变量 [英] R - multiple loop counter variables

查看:66
本文介绍了R-多个循环计数器变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写循环,其中每个迭代都有一组唯一的变量值.这不是一个嵌套循环对于每个i(每个j)"类型的问题.下面仅是一个示例问题,循环的作用并不重要,它可以为循环的每次迭代使用多个变量.

I need to write loops where each iteration has a unique set of variable values. It is not a nested loop "for each i do (for each j do)" type problem. Below is just an example problem, what the loop is doing is not important, it is being able to use multiple variables for each iteration for the loop.

简单循环:

df <- data.frame(num = 1:5)
lookup <- data.frame(colors = c("red", "green", "blue"), 
                     pets = c("cat", "dog", "rabbit"),
                     stringsAsFactors = FALSE)
for (color in lookup$colors) {
  df[, color] <- 1
}

我想做什么(伪代码):

what I want to do (pseudo code):

for (color, pet in lookup$colors, lookup$pets) {
  df[, color] <- pet
}

下面是我想出的最好方法,但是附加的r ["]使代码更难阅读:

The best approach I have come up with is below, but the additional r[" "] makes the code harder to read:

for (i in 1:nrow(lookup)) {
  r <- unlist(lookup[i, ])
  df[, r["colors"]] <- r["pets"]
}

df
  num red green   blue
1   1 cat   dog rabbit
2   2 cat   dog rabbit
3   3 cat   dog rabbit
4   4 cat   dog rabbit
5   5 cat   dog rabbit

我想知道什么是解决此类问题的最佳方法.在许多情况下,您可以将循环替换为要为每个变量集调用的函数,但在某些情况下该函数不适用.

I would like to know what the best generalisable approach to this kind of problem is. In many cases, you could replace the loop with a function to be called for each set of variables, but functions aren't suitable in certain cases.

推荐答案

对于您的特定示例,您将以正确的心态进行操作.要对其进行一些清理并减少发生错误的机会,可以将循环重写为:

For your specific example, you're going at it with the right mindset. To clean it up a little and reduce the chance of bugs, you could rewrite the loop as:

for (i in seq_len(nrow(lookup))) {
  color_i <- lookup[i, "colors"]
  pet_i <- lookup[i, "pets"]
  df[[color_i]] <- pet_i
}

没有什么不同,但是 seq_len 避免了 lookup 具有零行的问题.在这种情况下, 1:nrow(lookup)返回 c(1,0).而我的循环胆量是否更易于阅读可能是主观的.

Not that different, but seq_len avoids problems where lookup has zero rows. In that case, 1:nrow(lookup) returns c(1, 0). And whether or not my loop's guts are easier to read is probably subjective.

如果有帮助,针对您的特定问题的单线解决方案是:

In case it helps, a one-line solution to your specific problem is this:

df[, lookup[["colors"]]] <- lapply(lookup[["pets"]], rep, nrow(df))

切线相关

我会说您的示例是R中的一种特例,在该示例中,您要迭代地修改现有对象.大多数时候,人们只想迭代多个向量并将结果存储在新向量中.例如:

Tangentially related

I will say your example is a special case in R where you're iteratively modifying an existing object. Most of the time, people just want to iterate over multiple vectors and store the results in a new vector. For example:

results <- list()

for (i in seq_len(nrow(lookup))) {
  color_i <- lookup[i, "colors"]
  pet_i <- lookup[i, "pets"]
  results[[i]] <- do_something(color_i, pet_i)
}

更好的方法是使用 mapply :

results <- mapply(FUN = do_something, lookup[["colors"]], lookup[["pets"]])

这篇关于R-多个循环计数器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆