[R] preserve class in apply function

Gabor Grothendieck ggrothend|eck @end|ng |rom gm@||@com
Thu Feb 9 03:47:26 CET 2023


Also try

apply(Filter(is.numeric, mydf), 1, sum)

On Tue, Feb 7, 2023 at 8:42 AM PIKAL Petr <petr.pikal using precheza.cz> wrote:
>
> Hi Naresh
>
> If you wanted to automate the function a bit you can use sapply to find
> numeric columns
> ind <- sapply(mydf, is.numeric)
>
> and use it in apply construct
> apply(mydf[,ind], 1, function(row) sum(row))
>  [1]  2.13002569  0.63305300  1.48420429  0.13523859  1.17515873 -0.98531131
>  [7]  0.47044467  0.23914494  0.26504430  0.02037657
>
> Cheers
> Petr
>
> > -----Original Message-----
> > From: R-help <r-help-bounces using r-project.org> On Behalf Of Naresh Gurbuxani
> > Sent: Tuesday, February 7, 2023 1:52 PM
> > To: r-help using r-project.org
> > Subject: [R] preserve class in apply function
> >
> >
> > > Consider a data.frame whose different columns have numeric, character,
> > > and factor data.  In apply function, R seems to pass all elements of a
> > > row as character.  Is it possible to preserve numeric class?
> > >
> > >> mydf <- data.frame(x = rnorm(10), y = runif(10))
> > >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> > 0.31351909
> > > [7] -0.63575990  0.22670658  0.55696314  0.39587314
> > >> mydf[, "z"] <- sample(letters[1:3], 10, replace = TRUE)
> > >> apply(mydf, 1, function(row) {row["x"] + row["y"]})
> > > Error in row["x"] + row["y"] (from #1) : non-numeric argument to binary
> > operator
> > >> apply(mydf, 1, function(row) {as.numeric(row["x"]) +
> > as.numeric(row["y"])})
> > > [1]  0.60150194 -0.74201826  0.80476394 -0.59729282 -0.02980338
> > 0.31351912
> > > [7] -0.63575991  0.22670663  0.55696309  0.39587311
> > >> apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]})
> > > [1]  0.60150197 -0.74201827  0.80476392 -0.59729280 -0.02980335
> > 0.31351909
> > > [7] -0.63575990  0.22670658  0.55696314  0.39587314
> >
> > ______________________________________________
> > 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.
> ______________________________________________
> 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.



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list