0

I want to scan the column 'var2' for elements that appear in an other column 'var1' and, where they match, find the proportion 'value2'/'value1' depending on the levels of the column 'type'. The desired output would be a list or a data frame.

var1<- c(8, 1, 1, 2, 3, 1, 4, 5, 2, 6, 1, 3, 7)
var2 <- c(NA,8,NA,NA,NA,3,NA,NA,5,NA,6,6,NA)
value1 <- c(4340,   NA, 3740, 3825, 3845,   NA, 4005, 2660,   NA, 3055,   NA,   NA, 5800)
value2 <- c(NA, 30, NA, NA, NA,  5, NA, NA, 15, NA,  1, 20, NA)
type <- c('Fish','Crab','Fish','Fish','Fish','Bird','Fish','Fish','Bird','Fish','Bird','Crab','Fish')
df <- data.frame(var1,var2,value1,value2,type)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Valtyr
  • 123
  • 1
  • 12
  • 1
    what would be your desired output ? – etienne Oct 25 '16 at 10:23
  • Either a list or a data frame (thinking of summarize with ddply) – Valtyr Oct 25 '16 at 10:29
  • For example: 'var1' is a predator and var2 is prey. The number 6 appears once in var1 (i=10) and twice in var2 (i=11 and i=12). I want to calculate what the proportion 'value2' (when i=11 and i=12) is of 'value1 (when i=10) – Valtyr Oct 25 '16 at 11:06
  • I edited 'type' so that there are different levels of 'type' when 'var1' and 'var2' ==6 – Valtyr Oct 25 '16 at 11:23
  • what should be the proportion for var1 = 6 ? – Veerendra Gadekar Oct 25 '16 at 11:48
  • 1
    Its not clear what output is expected and no we don't mean format of output (list,data.frame) but the resultant values i.e demonstrate calculation for one step, if `var1` and `var2` == 6, what is the result of value2/value1 contingent on type. Please edit you post to reflect this and a good standard is [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Silence Dogood Oct 25 '16 at 11:51
  • @Veerendra Gadekar it would be 1/3055 and 20/3055 – Valtyr Oct 25 '16 at 11:51
  • 1
    considering your data is a `data.frame` you could try to work on this `lapply(split(df, df$var2), function(x) c(x$value2/df$var1[df$var1 == unique(x$var2)]))` – Veerendra Gadekar Oct 25 '16 at 11:56
  • 1
    @Veerendra Gadekar I made a minor modification and this works perfectly. Thank you. lapply(split(df, df$var2), function(x) c(x$value2/df$value1[df$var1 == unique(x$var2)])) – Valtyr Oct 25 '16 at 13:36

1 Answers1

1

Thanks to @Veerendra Gadekar who came up with the solution in a comment.

lapply(split(df, df$var2), 
  function(x) 
   c(x$value2/df$value1[df$var1 == unique(x$var2) & !is.na(df$value1)])
 )

#$`3`
#[1] 0.00130039
#
#$`5`
#[1] 0.005639098
#
#$`6`
#[1] 0.0003273322 0.0065466448
#
#$`8`
#[1] 0.006912442
Veerendra Gadekar
  • 4,452
  • 19
  • 24
Valtyr
  • 123
  • 1
  • 12