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". a is column Company ID
and b is column max_di_Flex
. Can anyone help me out with the for loop? 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))))
CodePudding user response:
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]))
CodePudding user response:
Tidyverse solution using map2_dbl
(when you have a function that takes 2 inputs and returns a number):
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