[R] Multiply List by a Numeric
Peter Ehlers
ehlers at ucalgary.ca
Mon Aug 24 18:16:32 CEST 2009
Try
lapply(abc, function(x) x*3)
Peter Ehlers
Brigid Mooney wrote:
> I apologize for what seems like it should be a straighforward query.
>
> I am trying to multiply a list by a numeric and thought there would be a
> straightforward way to do this, but the best solution I found so far has a
> for loop.
> Everything else I try seems to throw an error "non-numeric argument to
> binary operator"
>
> Consider the example:
>
> a <- 1
> b <- 1:2
> c <- 1:3
> abc <- list(a,b,c)
> To multiply every element of abc by a numeric, say 3, I wrote a for-loop:
>
> for (i in 1:length(abc))
> {
> abc[[i]] <- 3*abc[[i]]
> }
>
> Is this really the simplest way or am I missing something?
>
> Thanks!
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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