有没有办法在 R 中的两个数据帧上应用具有多个参数的函数?
Is there a way to apply a function with multiple arguments over two data frames in R?
我想编写一个函数来将“值”data.frame中“+”的值替换为“其他”data.frame中的相应值。
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)
}
这个函数是这样工作的,但我不知道如何遍历“names”中的所有值。
#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”,我正在寻找的输出是
"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"```
你也可以这样做:
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"
我们可以在 dplyr
内部做到这一点。由于列名相同,我们可以循环 across
'values' 中的所有列,使用 cur_column()
从 'others' 中提取相应的列(returns 列across
内的名称),将 ""
替换为 NA
(na_if
) 并使用 coalesce
以便它将替换为第一个非 NA elementwise
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)
我想提出一些不同的建议,也许对您的所有数据都有帮助:
# 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
找到'+'
在values
中的位置,并用others
中的对应值替换它。
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]
我想编写一个函数来将“值”data.frame中“+”的值替换为“其他”data.frame中的相应值。
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)
}
这个函数是这样工作的,但我不知道如何遍历“names”中的所有值。
#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”,我正在寻找的输出是
"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"```
你也可以这样做:
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"
我们可以在 dplyr
内部做到这一点。由于列名相同,我们可以循环 across
'values' 中的所有列,使用 cur_column()
从 'others' 中提取相应的列(returns 列across
内的名称),将 ""
替换为 NA
(na_if
) 并使用 coalesce
以便它将替换为第一个非 NA elementwise
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)
我想提出一些不同的建议,也许对您的所有数据都有帮助:
# 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
找到'+'
在values
中的位置,并用others
中的对应值替换它。
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]