简体   繁体   English

在 R 中使用应用函数而不是 for 循环

[英]Using apply functions instead of for loops in R

I have been trying to replace a for loop in my code with an apply function, and i attempted to do it in all the possible ways, using sapply and lapply and apply and mapply, always seems to not work out, the original function looks like this我一直在尝试用 apply 函数替换我代码中的 for 循环,我尝试以所有可能的方式来做到这一点,使用 sapply 和 lapply 以及 apply 和 mapply,似乎总是不起作用,原始函数看起来像这个

ds1 <- data.frame(col1 = c(NA, 2), col2 = c("A", "B"))
ds2 <- data.frame(colA = c("A", "B"), colB = c(90, 110))

for(i in 1:nrow(ds1)){
  if(is.na(ds1$col1[i])){
    ds1$col1[i] <- ds2[ds2[,"colA"] == ds1$col2[i], "colB"]
  }
}

My latest attempt with the apply family looks like this我最近对申请家庭的尝试看起来像这样

ds1 <- data.frame(col1 = c(NA, 2), col2 = c("A", "B"))
ds2 <- data.frame(colA = c("A", "B"), colB = c(90, 110))

sFunc <- function(x, y, z){
  if(is.na(x)){
    return(z[z[,"colA"] == y, "colB"])
  } else {
    return(x)
  }
}

ds1$col1 <- sapply(ds1$col1, sFunc, ds1$col2, ds2)

Which returns ds2$colB for each row, can someone explain to me what I got wrong about this?它为每一行返回ds2$colB ,有人可以向我解释我在这方面做错了什么吗?

sapply only iterates over the first vector you pass. sapply仅迭代您传递的第一个向量。 The other arguments you pass will be treated as whole vectors in each loop.您传递的其他参数将被视为每个循环中的整个向量。 To iterate over m ultiple vectors you need m ultivariate apply, which is m apply.遍历在M ultiple载体需要ultivariate应用,这为m适用。

sFunc <- function(x, y){
  if(is.na(x)){
    return(ds2[ds2[,"colA"] == y, "colB"])
  } else {
    return(x)
  }
}

mapply(sFunc, ds1$col1, ds1$col2)
#> [1] 90  2

A join would be useful here.连接在这里很有用。 You can do it in base R :你可以在基础 R 中做到这一点:

transform(merge(ds1, ds2, by.x = "col2", by.y = "colA"), 
          col1 = ifelse(is.na(col1), colB, col1))[names(ds1)]

#  col1 col2
#1   90    A
#2    2    B

Or with dplyr或者用dplyr

library(dplyr)

inner_join(ds1, ds2, by = c("col2" = "colA")) %>%
    mutate(col1 = coalesce(col1, colB)) %>%
    select(names(ds1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM