繁体   English   中英

有没有办法在 R 中的两个数据帧上应用具有多个 arguments 的 function?

[英]Is there a way to apply a function with multiple arguments over two data frames in R?

我想写一个 function 用“其他”数据帧中的相应值替换“值”数据帧中的“+”值。

values <- data.frame(A = c("banana", "orange", "apple", "pear", "+"),
                 B = c("apple", "+", "banana", "melon", "orange"))

others <- data.frame(A = c("", "", "", "", "apple"),
                     B = c("", "pear", "", "", ""))

names <- c("A", "B")

#function to replace values of "+" with corresponding value in other data.frame
replace_with_other <- function(x, y) {
  ifelse(x == "+", y, x)
}

这个 function 像这样工作,但我不知道如何迭代“名称”中的所有值。

#this works and gives the desired output
replace_with_other(values$A, others$A)

#but when I try to iterate over all the names, I get an error message.
map(names, replace_with_other(values, others))

对于名字“A”,我正在寻找的 output 是

"banana" "orange" "apple"  "pear"   "apple" 

有人有想法吗?

这对你有用吗?

> Map(replace_with_other, values[names], others[names])
$A
[1] "banana" "orange" "apple"  "pear"   "apple"

$B
[1] "apple"  "pear"   "banana" "melon"  "orange"```

我们可以在dplyr本身内执行此操作。 由于列名相同,我们可以遍历 'values' 中的across列,使用cur_column()从 'others' 中提取相应的列(返回 cross 中across列名),将""替换为NA ( na_if )并使用coalesce ,以便它将替换为第一个非 NA 元素

library(dplyr)
values %>% 
  mutate(across(everything(), ~ 
       coalesce(na_if(others[[cur_column()]], ""), .)))
#    A      B
#1 banana  apple
#2 orange   pear
#3  apple banana
#4   pear  melon
#5  apple orange

或者可以使用map2

library(purrr)
map2(values[names], others[names], replace_with_other)

你也可以这样做:

replace_with_other <- function(x, y, name) {
  ifelse(x[[name]] == "+", y[[name]], x[[name]])
}

purrr::map(names, ~replace_with_other(values, others, .x))
# [[1]]
# [1] "banana" "orange" "apple"  "pear"   "apple" 
# 
# [[2]]
# [1] "apple"  "pear"   "banana" "melon"  "orange"

我想提出一些不同的建议,也许它对您的所有数据都有帮助:

# function that replace + with corrispondent values in another df
func <- function(x, y){

# convert as matrix the two imputs
a <- as.matrix(x)
b <- as.matrix(y)

# paste0 them i.e. merge in one matrix
ab <- matrix(paste0(a, b), nrow = nrow(a))

# replace the + with nothing and convert as df
ab <- data.frame(gsub("\\+", "", ab))

# colnames from the first input
colnames(ab) <- colnames(x)

# print the output
print(ab)}


func(values, others)

       A      B
1 banana  apple
2 orange   pear
3  apple banana
4   pear  melon
5  apple orange

找到 position , others '+'出现在values中,并将其替换为 other 的相应值。

mat <- values == '+'
values[mat] <- others[mat]
values

#       A      B
#1 banana  apple
#2 orange   pear
#3  apple banana
#4   pear  melon
#5  apple orange

这要求两个数据框的列名的顺序与示例数据中共享的顺序相同。 如果不是,您可以重新排列列。

values <- values[names]
others <- others[names]

暂无
暂无

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

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