乘以多列,并找到多个值的每列的和 [英] multiply multiple column and find sum of each column for multiple values

查看:123
本文介绍了乘以多列,并找到多个值的每列的和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试增加列并获取其名称。
我有一个数据框架:

  v1 v2 v3 v4 v5 
0 1 1 1 1
0 1 1 0 1
1 0 1 1 0

我试图将每列与其他列相乘,如下所示:

  v1v2 
v1v3
v1v4
v1v5


v2v3
v2v4
v2v5



等,和

  v1v2v3 
v1v2v4
v1v2v5
v2v3v4
v2v3v5

4组合和5组合...如果有n列然后n组合。



我尝试在while循环中使用以下代码,但不起作用:

  i< -1 
while(i <= ncol(data)
{
results< -data.frame()
v< i
结果< - t(apply(data,1,function(x)combn(x,v,prod)))
comb< - combn(colnames(data),v)
colnames(results)< - apply(comb,v,function(x)paste(x [1],x [2],sep =*))
results&l t; - colSums(结果)
}

但它不工作。 >

sample out put ..



如果n = 3

  v1v2 v1v3 v2v3 
0 0 1
0 0 1
0 1 0

和colsum

  v1v2 v1v3 v2v3 
0 1 2

然后

  v1v2 = 0 
v1v3 = 1
v2v3 = 2

一个是我在尝试?

解决方案

尝试这个:

  df<  -  read.table(text =v1 v2 v3 v4 v5 
0 1 1 1 1
0 1 1 0 1
1 0 1 1 0,skip = 1)

df

ll < - vector(mode =list,length = ncol(df)-1)

ll < - lapply(2:ncol(df),function(ncols){
tmp < - t(apply(df,1,function(rows)combn(x = rows,m = ncols,prod)))
if(ncols< ncol(df)){
tmp< - colSums(tmp)
}
else {
tmp< - sum(tmp)
}
name1< - t(combn(x = colnames(df),m = ncols))
名称(tmp)< - apply(names1,1,function(rows)paste0(rows,collapse = )
ll [[ncols]]< - tmp
})

ll

#[[1]]
#V1V2 V1V3 V1V4 V1V5 V2V3 V2V4 V2V5 V3V4 V3V5 V4V5
#0 1 1 0 2 1 2 2 2 1

#[[2]]
#V1V2V3 V1V2V4 V1V2V5 V1V3V4 V1V3V5 V1V4V5 V2V3V4 V2V3V5 V2V4V5 V3V4V5
#0 0 0 1 0 0 1 2 1 1

#[[3]]
#V1V2V3V4 V1V2V3V5 V1V2V4V5 V1V3V4V5 V2V3V4V5
#0 0 0 0 1

#[[4]]
#V1V2V3V4V5
#0

编辑以下注释
然后可以通过索引(子集化)列表来访问不同列组合的结果。例如。要访问2组合,请选择列表的第一个元素,以访问第三个组合,选择列表的第二个元素等。

  ll [[1]] 
#V1V2 V1V3 V1V4 V1V5 V2V3 V2V4 V2V5 V3V4 V3V5 V4V5
#0 1 1 0 2 1 2 2 2 1


I'm trying to multiply column and get its names. I have a data frame:

v1 v2 v3 v4 v5  
 0  1  1  1  1
 0  1  1  0  1
 1  0  1  1  0

I'm trying to multiplying each column with other, like:

v1v2 
v1v3
v1v4
v1v5

and v2v3 v2v4 v2v5

etc, and

v1v2v3 
v1v2v4
v1v2v5
v2v3v4
v2v3v5

4 combination and 5 combination...if there is n column then n combination.

I'm try to use following code in while loop, but it is not working:

i<-1 
while(i<=ncol(data)
{  
  results<-data.frame() 
  v<-i
  results<- t(apply(data,1,function(x) combn(x,v,prod))) 
  comb <- combn(colnames(data),v)
  colnames(results) <- apply(comb,v,function(x) paste(x[1],x[2],sep="*"))
  results <- colSums(results)
}

but it is not working.

sample out put..

if n=3

v1v2  v1v3 v2v3
  0      0    1    
  0      0    1  
  0      1    0

and colsum

v1v2  v1v3 v2v3
   0     1    2

then

v1v2=0
v1v3=1
v2v3=2

this one is I'm trying?

解决方案

Try this:

df <- read.table(text = "v1 v2 v3 v4 v5  
 0  1  1  1  1
 0  1  1  0  1
 1  0  1  1  0", skip = 1)

df

ll <- vector(mode = "list", length = ncol(df)-1)

ll <- lapply(2:ncol(df), function(ncols){
  tmp <- t(apply(df, 1, function(rows) combn(x = rows, m = ncols, prod)))
  if(ncols < ncol(df)){
  tmp <- colSums(tmp)
  }
  else{
    tmp <- sum(tmp)
  }
  names1 <- t(combn(x = colnames(df), m = ncols))
  names(tmp) <- apply(names1, 1, function(rows) paste0(rows, collapse = ""))
  ll[[ncols]] <- tmp
})

ll

# [[1]]
# V1V2 V1V3 V1V4 V1V5 V2V3 V2V4 V2V5 V3V4 V3V5 V4V5 
#    0    1    1    0    2    1    2    2    2    1 
# 
# [[2]]
# V1V2V3 V1V2V4 V1V2V5 V1V3V4 V1V3V5 V1V4V5 V2V3V4 V2V3V5 V2V4V5 V3V4V5 
#      0      0      0      1      0      0      1      2      1      1 
# 
# [[3]]
# V1V2V3V4 V1V2V3V5 V1V2V4V5 V1V3V4V5 V2V3V4V5 
#        0        0        0        0        1 
# 
# [[4]]
# V1V2V3V4V5 
#          0

Edit following comment The results of the different set of column combinations can then be accessed by indexing (subsetting) the list. E.g. to access the "2 combinations", select the first element of the list, to access the "3rd combination", select the second element of the list, et c.

ll[[1]]
# V1V2 V1V3 V1V4 V1V5 V2V3 V2V4 V2V5 V3V4 V3V5 V4V5 
#    0    1    1    0    2    1    2    2    2    1

这篇关于乘以多列,并找到多个值的每列的和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆