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))))
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):
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.