[R] question about getting things out of an lapply

Annemarie Verkerk annemarie.verkerk at mpi.nl
Wed Jul 6 21:31:51 CEST 2011


Dear R-help subscribers,

I have a quite stupid question about using lapply. I have the following 
function:

create.gradient <- function(i){
colorgrad01<-color.scale(seq(0,1,by=0.01), extremes=c("red","blue"))
tree1$edge[i,1] -> x
tree1$edge[i,2] -> y
print(x)
print(y)
all2[x] -> z
all2[y] -> z2
round(z, digits = 2) -> z
round(z2, digits = 2) -> z2
z*100 -> z
z2*100 -> z2
print(z)
print(z2)
colorgrad<-colorgrad01[z:z2]
colorgrad
}

Basically, I want to pick a partial gradient out of a bigger gradient 
(colorgrad01) for values that are on row i, from a matrix called tree1.

when I use lapply:

lapply(tree1$edge, create.gradient)

I get the following error message:

Error in FUN(X[[27L]], ...) : subscript out of bounds

I'm not sure what's wrong: it could be either fact that 'colorgrad' is a 
character string; i.e. consisting of multiple characters and not just 
one, or because 'i' doesn't come back in the object 'colorgrad' that it 
has to return. Or it could be something else entirely...

In any case, what I prefer as output is a vector with all the different 
'colorgrad's it generates with each run.

Thanks a lot for any help you might be able to offer!
Annemarie

-- 
Annemarie Verkerk, MA
Evolutionary Processes in Language and Culture (PhD student)
Max Planck Institute for Psycholinguistics
P.O. Box 310, 6500AH Nijmegen, The Netherlands
+31 (0)24 3521 185
http://www.mpi.nl/research/research-projects/evolutionary-processes



More information about the R-help mailing list