-7

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?

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
vct
  • 73
  • 1
  • 10
  • sapply gives entire multiplication value but i couldn't access column name from it – vct Nov 29 '13 at 16:55
  • 1
    Kindly be specific on exact requirement, like a sample output. Thanks. – vamosrafa Nov 29 '13 at 17:05
  • @vamosrafa .i done it – vct Nov 29 '13 at 17:44
  • Did you also post a highly similar question with the user name @user3050374 [**here**](http://stackoverflow.com/questions/20291599/how-paste-function-working-in-r)? Cross posting is **not** considered good practice. – Henrik Nov 29 '13 at 19:35
  • @Henrik i cound't get the single column value from ll.i select var<-ll[1].i want a column name its value from it.i used var[1];but its not working.also column name funtion not working – vct Nov 30 '13 at 08:41

1 Answers1

2

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
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • thanks for coding. this code is good. but i need step by step results.thats why i'm using "while" loop. (i need 2 combination and after that only need the 3rd combination..) sapply also gives same results but not column name. – vct Nov 29 '13 at 20:13
  • The results for the different set of combinations (2 columns, 3 columns etc) are stored in a list (here `ll`). Use standard list indexing to access the results for each set of combinations. – Henrik Nov 29 '13 at 20:16
  • i already understood that. actually my data frame contain more than 1 lakh entry. so if i initially multiply with this code it ll take lot of time for multiplication alone – vct Nov 29 '13 at 20:23
  • @vct, I have answered **your original question** where no such conditions were specified. I am sorry, but my mind-reading skills are poor, and I think I share this trait with most people trying to help on SO. – Henrik Nov 29 '13 at 20:28
  • no dear your code is very good in quality..really i'm sorry for not saying about my data frame. but i mean while loop used only for step by step answer.. – vct Nov 29 '13 at 20:33
  • I am sorry, I give up. I have no idea what you mean by "step by step" answer. And "after that only need the 3rd combination" vs. "4 combination and 5 combination...if there is n column then n combination.". Please read [**this**](http://meta.stackoverflow.com/help/how-to-ask) carefully before next question. – Henrik Nov 29 '13 at 20:36
  • k.im following @chargaff code.that way only i ll get my answer – vct Nov 29 '13 at 20:47
  • i cound't get the single column value.that means V1V2 ,V1V2..cloumn value,value.i coudn't acess single column value i get ll[1]; From that V1V2 ,V1V2 – vct Nov 30 '13 at 08:08
  • that means V1V2 ,V1V2..cloumn value,value.i coudn't acess single column value .i get ll[1]; From that V1V2 ,V1V2 – vct Nov 30 '13 at 08:44