[R] question about getting things out of an lapply

Joshua Wiley jwiley.psych at gmail.com
Wed Jul 6 22:44:10 CEST 2011


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){
> colorgrad01<-color.scale(seq(0,1,by=0.01), extremes=c("red","blue"))
> 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)
> 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
>
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list