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