简体   繁体   English

在数据帧的每一行上运行函数,将结果存储在新列 R

[英]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

Edit编辑

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.

相关问题 R将函数应用于数据框的每一行,将结果存储在同一数据框的新列中 - R apply function to each row of dataframe, store result in new column of same dataframe 将函数应用于R中数据框中每一行的特定列 - Apply a function to a specific column for every row in a dataframe in R 将函数应用于数据框中的每一行并将结果存储在新列中 - Applying function to each row in dataframe and storing result in new column 使用行值作为 R 函数中的变量在数据框中创建新列 - Creating a new column in dataframe using row values as variables in a function in R 在 R 中,如何将行中的第 n 个最大数和 output 结果返回到新列,每行重复? - In R, how to return the nth largest number in a row and output result to a new column, repeated for every row? 行中到R中新列的所有其他因子 - Every other factor in row to new column in R 每隔一行添加到新列 - R - Add Every Other Row to new Column - R 如何从 dataframe in.log 文件(在 R 中)在每 49 列创建新行? - How to create new row at every 49th column from the dataframe in .log file (in R)? 对于列中 = 1 的每个值,将其下方第 3 行的值提取到新的数据框 (R) - For every value within a column that = 1, extract the the value from the 3rd row below it to a new dataframe (R) 在 R dataframe 中使用行总和创建一个新列 - Create a new column in R dataframe with row sum
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM