简体   繁体   English

将行作为参数传递给R dplyr mutate中的函数

[英]Passing row as an argument to a function in R dplyr mutate

I'm writing a program that calculates the difference between an element of a dataset and the rest of elements. 我正在编写一个程序,该程序计算数据集的元素与其余元素之间的差异。 I'm using dplyr mutate and I need to pass the entire row as an argument to a function which calculates the difference. 我正在使用dplyr mutate,我需要将整行作为参数传递给计算差异的函数。 Using iris as a example: 以鸢尾花为例:

#Difference function
diff_func <- function (e1, e2) {
  return(sum(e1-e2))
}

chosenElement <- iris[1,1:4] # Chosen element
elements <- iris[10:50,1:4] # Elements to compare to

elements %>% 
  rowwise() %>% 
  mutate(difference=diff_func(chosenElement, c(Petal.Width, Petal.Length, Sepal.Width, Sepal.Length)))

This works, but as I use the entire row, I would like to use something like "this" or "row" instead of specifying all the columns of the row: 这可行,但是当我使用整个行时,我想使用“ this”或“ row”之类的东西,而不是指定该行的所有列:

elements %>% 
  rowwise() %>% 
  mutate(difference=diff_func(chosenElement, row))

Does anyone know if this can be done? 有谁知道这可以做到吗?

We can do this very easily in base R by replicating the chosenElement to make the dimensions same 通过复制selectedElement以使尺寸相同,我们可以在base R非常轻松地完成此操作

elementsNew <- elements - chosenElement[col(elements)]

Note that mutate is for changing/transforming the values of a single column/multiple columns -> a single column. 请注意, mutate用于更改/转换单列/多列->单列的值。 Of course, we can place other types of objects in a list . 当然,我们可以将其他类型的对象放在list Assuming that the 'difference' should be for each column of 'elements' with that of corresponding element of 'chosenElement', the mutate is not doing that with the diff_func 假设“差异”应与“ chosenElement”的对应元素对应,并与“ chosenElement”的对应元素对应,则mutate不会使用diff_func

Update 更新

Based on the clarification it seems we need get the difference between the columns with the corresponding chosenElement (here we replicated) and then do the rowSums 根据说明,似乎我们需要获取具有对应的selectedElement的列之间的差(此处已复制),然后执行rowSums

elements %>%
        mutate(difference = rowSums(. - chosenElement[col(.)]))

purrr base组合:

do.call(cbind,purrr::map2(elements,chosenElement,function(x,y) x-y))

Since (a - d) + (b - e) + (c - f) == (a + b + c) - (d + e + f) , it's just a difference between row sums of the elements and sum of chosenElements , which you can do within base R: 由于(a - d) + (b - e) + (c - f) == (a + b + c) - (d + e + f) ,因此这只是elements行总和与chosenElements之和之间的chosenElements ,您可以在基数R中执行以下操作:

elements$dfrnce <- rowSums(elements) - sum(chosenElement)

Or, in dplyr : 或者,在dplyr

elements %>%
  mutate(dfrnce = rowSums(.) - sum(chosenElement))

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

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