2017-06-14 18 views
0

我傳遞兩個列表和一個恆定的數據幀從父功能的子功能在R.封裝和傳遞列表&常量通過MAPPLY

如果創建的數據幀中的所有功能外具有的問題,該函數可以訪問外部數據框(「lookup_frame」)並執行相應的操作。見Code Block One。

如果數據框以兩個列表的常量傳遞,則該函數不起作用。見代碼塊二。

如果在父函數中創建數據框,則子功能無法訪問數據框(「lookup_frame_two」)。見代碼塊三。

的什麼,我試圖完成如下所示的近似代碼塊:

for(i = 1; i < n; i++) { 
    result[i] <- function(list_one[i], list_two[i], data_frame) 
} 

代碼塊一個

#runs fine 
df_one <- data.frame(a = numeric(3), b = numeric(3)) 
df_one$a <- c(1:3) 
df_one$b <- c(2:4) 

lookup_frame <- data.frame(value_one = numeric(3), value_two = character(3)) 
lookup_frame$value_one <- c(10:12) 
lookup_frame$value_two <- c(1:3) 

test_function <- function(x,y){ 
    return_frame <- data.frame(value = numeric(1), lookup_value_one = numeric(1), lookup_value_two = numeric(1)) 
    random_test <- x^2+y^2 
    return_frame$value <- random_test 

    if(random_test < 6){ 
    return_frame$lookup_value_one <- lookup_frame$value_one[1] 
    return_frame$lookup_value_two <- lookup_frame$value_two[1] 
    } 

    if(random_test >6){ 
    if(random_test < 14){ 
     return_frame$lookup_value_one <- lookup_frame$value_one[2] 
     return_frame$lookup_value_two <- lookup_frame$value_two[2] 
    } 
    } 

    if(random_test > 14){ 
    return_frame$lookup_value_one <- lookup_frame$value_one[3] 
    return_frame$lookup_value_two <- lookup_frame$value_two[3] 
    } 
    return(return_frame) 
} 

test_output <- do.call(rbind, mapply(test_function, df_one$a, df_one$b, SIMPLIFY = FALSE)) 
View(test_output) 

代碼塊兩

#cannot pass the data frame in as a constant when the other two inputs are lists 
test_function_two <- function(x, y, z = data.frame()){ 
    return_frame <- data.frame(value = numeric(1), lookup_value_one = numeric(1), lookup_value_two = numeric(1)) 
    random_test <- x^2+y^2 
    return_frame$value <- random_test 

    if(random_test < 6){ 
    return_frame$lookup_value_one <- z$value_one[1] 
    return_frame$lookup_value_two <- z$value_two[1] 
    } 

    if(random_test >6){ 
    if(random_test < 14){ 
     return_frame$lookup_value_one <- z$value_one[2] 
     return_frame$lookup_value_two <- z$value_two[2] 
    } 
    } 

    if(random_test > 14){ 
    return_frame$lookup_value_one <- z$value_one[3] 
    return_frame$lookup_value_two <- z$value_two[3] 
    } 
    return(return_frame) 
} 
#does not work 
test_output_two <- do.call(rbind, mapply(test_function_two, df_one$a, df_one$b, lookup_frame, SIMPLIFY = FALSE)) 

代碼塊三

#cannot access the data frame in the parent function which calls the subfunction 
test_function_three <- function(x,y){ 
    return_frame <- data.frame(value = numeric(1), lookup_value_one = numeric(1), lookup_value_two = numeric(1)) 
    random_test <- x^2+y^2 
    return_frame$value <- random_test 

    if(random_test < 6){ 
    return_frame$lookup_value_one <- lookup_frame_two$value_one[1] 
    return_frame$lookup_value_two <- lookup_frame_two$value_two[1] 
    } 

    if(random_test >6){ 
    if(random_test < 14){ 
     return_frame$lookup_value_one <- lookup_frame_two$value_one[2] 
     return_frame$lookup_value_two <- lookup_frame_two$value_two[2] 
    } 
    } 

    if(random_test > 14){ 
    return_frame$lookup_value_one <- lookup_frame_two$value_one[3] 
    return_frame$lookup_value_two <- lookup_frame_two$value_two[3] 
    } 
    return(return_frame) 
} 

test_function_four <- function(){ 
    lookup_frame_two <- data.frame(value_one = numeric(3), value_two = character(3)) 
    lookup_frame_two$value_one <- c(10:12) 
    lookup_frame_two$value_two <- c(1:3) 

    test_output_two <- do.call(rbind, mapply(test_function_three, df_one$a, df_one$b, SIMPLIFY = FALSE)) 
    return(test_output_two) 
} 
#does not work 
test_if_four_works <- test_function_four() 

回答

0

mapplyMoreArgs參數需要list

test_output_two <- do.call(rbind,mapply(test_function_two, df_one$a, df_one$b, MoreArgs=list(z=lookup_frame),SIMPLIFY = FALSE)) 

上的代碼塊中的三個失敗,因爲功能test_function_three關閉,預計不包含defenition爲lookup_frame_two

:如果您更改最後一行 代碼塊兩將工作