[R] how to do inverse log of every value in every column in data frame
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Thu Oct 14 21:53:13 CEST 2021
This was already clear from Rich Heiberger's reply. But my point was not
that the as.matrix() coercion was necessary, but that it would be wise, as
operations with matrices are generally (often much) more efficient than
with data frames. Of course, other considerations may exist, but that was
my point, which I should evidently have made clearer.
Cheers,
Bert
On Thu, Oct 14, 2021 at 11:19 AM Rui Barradas <ruipbarradas using sapo.pt> wrote:
> Hello,
>
> The answer is given but there is no need to coerce to matrix first, as
> long as the columns are numeric.
> From ?exp, right at the beginning of section Details:
>
> Details
> All except logb are generic functions: methods can be defined for them
> individually or via the Math group generic.
>
>
> Follow the link Math:
>
>
> Details
> There are four groups for which S3 methods can be written, namely the
> "Math", "Ops", "Summary" and "Complex" groups. These are not R objects
> in base R, but methods can be supplied for them and base R contains
> factor, data.frame and difftime methods for the first three groups.
>
>
> And exp is the group "Math", 2nd bullet.
>
> class(mtcars)
> #[1] "data.frame"
> exp(mtcars)
> # output omitted
>
>
> But if a column is not numeric the method Math.data.frame throws an error.
>
>
> exp(iris)
> #Error in Math.data.frame(iris) :
> # non-numeric-alike variable(s) in data frame: Species
>
> exp(iris[-5]) # remove the offending column
> # output omitted
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> Às 18:23 de 14/10/21, Ana Marija escreveu:
> > Thank you so much!
> >
> > On Thu, Oct 14, 2021 at 12:17 PM Bert Gunter <bgunter.4567 using gmail.com>
> wrote:
> >
> >> As all of your columns are numeric, you should probably convert your df
> to
> >> a matrix. Then use exp() on that, of course:
> >> exp(as.matrix(b))
> >>
> >> see ?exp
> >>
> >> Bert Gunter
> >>
> >> "The trouble with having an open mind is that people keep coming along
> and
> >> sticking things into it."
> >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >>
> >>
> >> On Thu, Oct 14, 2021 at 10:10 AM Ana Marija <
> sokovic.anamarija using gmail.com>
> >> wrote:
> >>
> >>> Hi All,
> >>>
> >>> I have a data frame like this:
> >>>
> >>>> head(b)
> >>> LRET02 LRET04 LRET06 LRET08 LRET10 LRET12
> LRET14
> >>> 1 0 0.6931472 . 1.0986123 1.0986123 1.0986123 0.6931472
> >>> 2 2.1972246 2.4849066 2.4849066 . 2.5649494 2.6390573 2.6390573
> >>> 3 1.6094379 1.7917595 1.6094379 1.7917595 2.0794415 1.9459101 2.0794415
> >>> 4 0 0 0 0 0 0 0
> >>> 5 0.6931472 0 1.0986123 1.0986123 0.6931472 0.6931472 0.6931472
> >>> 6 1.0986123 1.0986123 1.0986123 0.6931472 1.0986123 1.3862944 1.0986123
> >>>
> >>> All values in this data frame are product of natural log. I have to do
> >>> inverse of it.
> >>> So for example do do inverse of 0.6931472 I would do:
> >>>> 2.718281828^0.6931472
> >>> [1] 2
> >>>
> >>> How do I perform this operation for every single value in this data
> frame?
> >>>
> >>> The original data frame is this dimension:
> >>>> dim(b)
> >>> [1] 1441 18
> >>>
> >>> Thanks
> >>> Ana
> >>>
> >>> [[alternative HTML version deleted]]
> >>>
> >>> ______________________________________________
> >>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> 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.
> >>>
> >>
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list