0

要考虑的一些示例 R 代码:

df = data.frame(x=letters[1:4], y=letters[5:8])

find.key <- function(x, li, default=NA) {
  ret <- rep.int(default, length(x))
  for (key in names(li)) {
    ret[x %in% li[[key]]] <- key
  }
  return(ret)
}

x2 = list("Alpha" = "a", 
          "Beta"  = "b", 
          "Other" = c("c","d"))

y2 = list("Epi"    = "e", 
          "OtherY" = c("f", "g", "h"))

# This is the code in question, imagine many variables and calls to find.key()
df$NewX2 = find.key(df$x, x2)
df$Newy2 = find.key(df$y, y2)

# df
#   x y NewX2  Newy2
# 1 a e Alpha    Epi
# 2 b f  Beta OtherY
# 3 c g Other OtherY
# 4 d h Other OtherY

所以这个要点是我想通过find.key 函数基于查找表(关联数组/列表)添加新变量(NewX2,Newy2) 。

有什么方法可以让我的代码保持干燥吗?特别是这里:

df$NewX2 = find.key(df$x, x2)
df$Newy2 = find.key(df$y, y2)

我不确定sapplylapply可以提供帮助吗?或者也许像这里%=%看到的那样。

我想要这样的东西......(希望这是有道理的):

c(df$NewX2, df$Newy2) = find.key(c(df$x, df$y), c(x2, y2))
4

1 回答 1

3

[对左侧的 data.frame使用提取而不是$提取:

df[,c('NewX2','NewY2')] <- mapply(find.key, 
                                  list(df$x, df$y), 
                                  list(x2, y2), 
                                  SIMPLIFY=FALSE)
# df
#   x y NewX2  NewY2
# 1 a e Alpha    Epi
# 2 b f  Beta OtherY
# 3 c g Other OtherY
# 4 d h Other OtherY

或者,如果你不喜欢写作mapply,你可以使用Vectorize,它将mapply为你创建一个基于 - 的函数来获得相同的结果:

find.keys <- Vectorize(find.key, c("x","li"), SIMPLIFY=FALSE)
df[,c('NewX2','NewY2')] <- find.keys(list(df$x, df$y), list(x2, y2))
df
#   x y NewX2  NewY2
# 1 a e Alpha    Epi
# 2 b f  Beta OtherY
# 3 c g Other OtherY
# 4 d h Other OtherY
于 2014-09-16T14:28:19.997 回答