r - Quickly find value from data.frame in list with matching element names -


i have list xnumber of named elements, each element containing series of numbers.

i have data.frame containing 2 columns:

  • column1: names matching of list elements (though not in order)
  • column2: vector of numbers

i want determine location of value of each row of data.frame in list given list element equals each given data.frame row's value in data.frame's name column.

the end goal produce vector containing list value of each appropriate element preceding value i've matched each row of data.frame.

my data has 200,000 rows i'm trying optimize process.


example

i have list , data.frame:

a = 1:5; b = 6:10; c = 4:8; l1 <- list(a,b,c) # list d1 <- data.frame(name = c('c','a','b'), val = c(7,3,8)) #a data.frame 

so first want know each value occurs in list (such element matches name same row in data.frame) :

where <- ????  >where [1] 4 3 3     # 7 = 4th number in c, 3 = 3rd # in a, , 8 = 3rd # in b 

but want output show me value in element preceding 1 i've matched:

which <- ????  >which [1] 6 2 7 

to have list named items, can use syntax:

l1 <- list(a=a,b=b,c=c) 

then can use mapply() test each item:

mapply(function(n,v) which(l1[[n]]==v) , d1$name,d1$val) [1] 4 3 3 

then mapply() again values:

mapply(function(n,i) l1[[ n]][i] , d1$name,     mapply(function(n,v) which(l1[[n]]==v)-1 , d1$name,d1$val)) [1] 6 2 7 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -