# [R] question about getting things out of an lapply

Annemarie Verkerk annemarie.verkerk at mpi.nl
Thu Jul 7 13:25:16 CEST 2011

```Dear Josh,

thanks for pointing this out - the idea behind writing this function is
plotting gradients on branches of phylogenetic trees - 'tree' refers to
a phylogenetic tree. It's easy to create a random phylogenetic tree in R:

library(ape)
library(plotrix)

rtree(15) -> tree

This gives you a tree with 15 taxa. You can plot it with plot() if you
want to take a look.

then the data - you can create a fake data set:

rnorm(15, mean = 0.5, sd = 0.15) -> data

for the data which the function needs, you also need:

ace(data, tree) -> results

data <- append(data,results\$ace)

names(data) <- NULL

I also tried with the following updated code I still got the same error
message:

tree\$edge[i,1] -> x
tree\$edge[i,2] -> y
print(x)
print(y)
data[x] -> z
data[y] -> z2
round(z, digits = 2) -> z
round(z2, digits = 2) -> z2
z*100 -> z
z2*100 -> z2
print(z)
print(z2)
}

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

I hope this help and you can replicate the problem too.

Thanks!
Annemarie

Joshua Wiley wrote:
> Dear Annemarie,
>
> Can you replicate the problem using a madeup dataset or one of the
> ones built into R?  It strikes me as odd to pass tree1\$edge directly
> to lapply, when it is also hardcoded into the function, but I do not
> have a sense exactly for what you are doing and without data it is
> hard to play around.
>
> Cheers,
>
> Josh
>
> On Wed, Jul 6, 2011 at 12:31 PM, Annemarie Verkerk
> <annemarie.verkerk at mpi.nl> wrote:
>
>> Dear R-help subscribers,
>>
>> I have a quite stupid question about using lapply. I have the following
>> function:
>>
>> create.gradient <- function(i){
>> tree1\$edge[i,1] -> x
>>
>
> this works, but it would typically be written:
>
> x <- tree1\$edge[i, 1]
>
> flipping back and forth can be a smidge (about 5 pinches under an
> iota) confusing.
>
>
>> 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)
>> }
>>
>> 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:
>>
>>
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>
>
>
>

--
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

```