[R] lapply and paste
R. Michael Weylandt <michael.weylandt@gmail.com>
michael.weylandt at gmail.com
Wed Mar 28 21:02:33 CEST 2012
Yes, there are non-vectorized functions e.g, integrate(), or you can use lapply() to apply a vectorized function to each element of a list (which is not what suff was) individually:
x <- list(1:3, 1:4, 1:5, 2:7)
mean(x) # bad
lapply(x, mean) #good
Michael
On Mar 28, 2012, at 2:44 PM, Ed Siefker <ebs15242 at gmail.com> wrote:
> Thank you, I was confused about that. What exactly is lapply for then,
> if R handles this kind of thing automatically? Are there functions that are
> not "vectorized"?
>
>
> On Wed, Mar 28, 2012 at 1:37 PM, R. Michael Weylandt
> <michael.weylandt at gmail.com> wrote:
>> I think you're confused about the need for lapply -- paste is
>> vectorized so this
>>
>> paste("filename_", suff, ".ext", sep = "")
>>
>> will work. But if you want to use lapply (for whatever reason) try this:
>>
>> lapply(suff, function(x) paste("filename_", x, ".ext", sep = "")
>>
>> Michael
>>
>> On Wed, Mar 28, 2012 at 2:31 PM, Ed Siefker <ebs15242 at gmail.com> wrote:
>>> I have a list of suffixes I want to turn into file names with extensions.
>>>
>>> suff<- c("C1", "C2", "C3")
>>> paste("filename_", suff[[1]], ".ext", sep="")
>>> [1] "filename_C1.ext"
>>>
>>> How do I use lapply() on that call to paste()?
>>> What's the right way to do this:
>>>
>>> filenames <- lapply(suff, paste, ...)
>>>
>>> ?
>>>
>>> Can I have lapply() reorder the arguments to FUN?
>>>
>>> ______________________________________________
>>> 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.
More information about the R-help
mailing list