简体   繁体   English

如何在向量的所有元素上循环函数,除了一个元素,并将结果存储在数据框的单独列中

[英]How to loop a function over all elements of a vector except one and store the result in separate columns of a data frame

I have a data frame with several columns.我有一个包含几列的数据框。 I want to run a function [pmax() in this case] over all columns whose name is stored in a vector except one, and store the result in new separate columns.我想在名称存储在向量中的所有列上运行一个函数 [pmax() 在这种情况下],并将结果存储在新的单独列中。 At the end, I would also like to store the names of all new columns in a separate vector.最后,我还想将所有新列的名称存储在一个单独的向量中。 A minimal example would be:一个最小的例子是:

Name <- c("Case 1", "Case 2", "Case 3", "Case 4", "Case 5")
C1 <- c(1, 0, 1, 1, 0)
C2 <- c(0, 1, 1, 1, 0)
C3 <- c(0, 1, 0, 0, 0)
C4 <- c(1, 1, 0, 1, 0)
Data <- data.frame(Name, C1, C2, C3, C4)

var.min <- function(data, col.names){
                    new.df <- data

                    # This is how I would do it outside a function and without loop:
                    new.df$max.def.col.exc.1 <- pmax(new.df$C2, new.df$C3)
                    new.df$max.def.col.exc.2 <- pmax(new.df$C1, new.df$C3)
                    new.df$max.def.col.exc.3 <- pmax(new.df$C1, new.df$C2)
                    
                    new.columns <- c("max.def.col.exc.1", "max.def.col.exc.2", "max.def.col.exc.3")

                    return(new.df)
}

new.df <- var.min(Data,
                  col.names= c("C1", "C2", "C3"))

The result should look like:结果应如下所示:

    Name C1 C2 C3 C4 max.def.col.exc.1 max.def.col.exc.2 max.def.col.exc.3
1 Case 1  1  0  0  1                 0                 1                 1
2 Case 2  0  1  1  1                 1                 1                 1
3 Case 3  1  1  0  0                 1                 1                 1
4 Case 4  1  1  0  1                 1                 1                 1
5 Case 5  0  0  0  0                 0                 0                 0

Anyone with an idea?有人有想法吗? Many thanks in advance!提前谢谢了!

Here is a base R solution with combn .这是带有combn的基本 R 解决方案。 It gets all pairwise combinations of the column names and calls a function computing pmax .它获取列名的所有成对组合并调用函数计算pmax

Note that the order of the expected output columns is the same as the one output by the code below.请注意,预期输出列的顺序与下面代码的输出顺序相同。 If the columns vector is c("C1", "C2", "C3") , the order will be different.如果列向量是c("C1", "C2", "C3") ,则顺序会有所不同。

Note also that the function is now a one-liner and accepts combinations of any number of columns, 2, 3 or more.另请注意,该函数现在是一个单行并接受任意数量的列(2、3 或更多)的组合。

var.min <- function(cols, data) Reduce(pmax, data[cols])

cols <- c("C3", "C2", "C1")
combn(cols, 2, var.min, data = Data)
#     [,1] [,2] [,3]
#[1,]    0    1    1
#[2,]    1    1    1
#[3,]    1    1    1
#[4,]    1    1    1
#[5,]    0    0    0

Now it's just a matter of assigning column names and cbind ing with the input data.现在只需要分配列名并使用输入数据进行cbind

tmp <- combn(cols, 2, var.min, data = Data)
colnames(tmp) <- paste0("max.def.col.exc.", seq_along(cols))
Data <- cbind(Data, tmp)
rm(tmp)    # final clean-up

暂无
暂无

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

相关问题 如何在数据框的所有列上运行 for 循环并将结果作为单独的数据框或矩阵返回 - How do I run a for loop over all columns of a data frame and return the result as a separate data frame or matrix 如何合并 r 中两个数据集的两列,并包括一个数据帧中的所有元素,除非它们是 NA? - How do I merge two columns from two datasets in r and include all the elements from one data frame except when they are NA? 在除数据列之外的所有列中查找数据框中的重复组 - Find groups of duplicates in data frame by all columns except one 将函数内部循环的输出存储到向量或data.frame中 - Store the output of a loop inside a function into a vector or data.frame 将data.frame的所有列减少为一个长向量 - Reduce all columns of data.frame to one long vector 将data.frame的所有列合并为一个(向量串联) - Combine all columns of a data.frame in a single one (vector concatenation) 如何将条件应用于 R 中向量中指定的数据框的所有列 - How to apply a condition to all columns of a data frame specified in a vector in R 应用于data.frame中除一列之外的所有列,并替换R中的数据 - Lapply to all columns in a data.frame except one and replace the data in R 如何在 R 中循环遍历带有列表的数据框列? - How to loop through data frame columns with lists as a result in R? Purrr 将函数映射到除一个之外的所有列 - Purrr map a function to all columns except for one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM