[英]run function on every row of dataframe, store result in new column, R
I would like to run the function sum_differences on every row of a dataframe and store the result in a new column "di_Flex".我想在数据帧的每一行上运行函数 sum_differences 并将结果存储在新列“di_Flex”中。 a is column Company ID
and b is column max_di_Flex
. a 是列Company ID
,b 是列max_di_Flex
。 Can anyone help me out with the for loop?谁能帮我解决 for 循环? Thank you!谢谢!
sum_differences <- function(a,b) {
a <- unique(a)
new_list <- c()
for (i in a) {
for (j in a) {
if(i != j) {
new_list <- c(new_list, abs(i-j))
}
}
}
outcome <- round((sum(new_list) / length(a)), 2)
percent <- outcome/b
return(percent)
}
data <- data.frame(structure(list(`Company ID` = c(0, 0, 0, 0, 0, 1, 2, 3, 4, 5,
5, 5, 6, 6, 6, 6, 6, 6, 6, 6), Flexibility_Thinking = c(7, 2,
5, 1, 6, 3, 6, 6, 7, 5, 7, 7, 4, 7, 5, 2, 3, 3, 3, 3), max_di_Flex = c(12.8,
12.8, 12.8, 12.8, 12.8, 0, 0, 0, 0, 8, 8, 8, 16, 16, 16, 16,
16, 16, 16, 16))))
Try:尝试:
data$di_Flex <- apply(data, 1, function(x) sum_differences(x[1], x[3]))
Or:或者:
data$di_Flex <- apply(as.data.frame(data), 1, function(x) sum_differences(x[1], x[3]))
Tidyverse solution using map2_dbl
(when you have a function that takes 2 inputs and returns a number):使用map2_dbl
Tidyverse 解决方案(当您有一个接受 2 个输入并返回一个数字的函数时):
df <- as.data.frame(data)
library(tidyverse)
df %>%
mutate(diFlex = map2_dbl(Company.ID, max_di_Flex, sum_differences))
#> Company.ID Flexibility_Thinking max_di_Flex diFlex
#> 1 0 7 12.8 0
#> 2 0 2 12.8 0
#> 3 0 5 12.8 0
#> 4 0 1 12.8 0
#> 5 0 6 12.8 0
#> 6 1 3 0.0 NaN
#> 7 2 6 0.0 NaN
#> 8 3 6 0.0 NaN
#> 9 4 7 0.0 NaN
#> 10 5 5 8.0 0
#> 11 5 7 8.0 0
#> 12 5 7 8.0 0
#> 13 6 4 16.0 0
#> 14 6 7 16.0 0
#> 15 6 5 16.0 0
#> 16 6 2 16.0 0
#> 17 6 3 16.0 0
#> 18 6 3 16.0 0
#> 19 6 3 16.0 0
#> 20 6 3 16.0 0
From the comments I saw on the other answer, I realized the function is made to take vectors instead of individual values.从我在另一个答案上看到的评论中,我意识到该函数是采用向量而不是单个值。 That means you don't need a loop, and should just call the function directly on the vectors:这意味着您不需要循环,而应该直接在向量上调用该函数:
df %>%
mutate(diFlex = sum_differences(df$Company.ID, df$max_di_Flex))
#> Company.ID Flexibility_Thinking max_di_Flex diFlex
#> 1 0 7 12.8 1.25
#> 2 0 2 12.8 1.25
#> 3 0 5 12.8 1.25
#> 4 0 1 12.8 1.25
#> 5 0 6 12.8 1.25
#> 6 1 3 0.0 Inf
#> 7 2 6 0.0 Inf
#> 8 3 6 0.0 Inf
#> 9 4 7 0.0 Inf
#> 10 5 5 8.0 2.00
#> 11 5 7 8.0 2.00
#> 12 5 7 8.0 2.00
#> 13 6 4 16.0 1.00
#> 14 6 7 16.0 1.00
#> 15 6 5 16.0 1.00
#> 16 6 2 16.0 1.00
#> 17 6 3 16.0 1.00
#> 18 6 3 16.0 1.00
#> 19 6 3 16.0 1.00
#> 20 6 3 16.0 1.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.