Re: [R] preserve class in apply function

2023-02-08 Thread Gabor Grothendieck
Also try apply(Filter(is.numeric, mydf), 1, sum) On Tue, Feb 7, 2023 at 8:42 AM PIKAL Petr 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],

Re: [R] preserve class in apply function

2023-02-08 Thread avi.e.gross
one row at a time when an R paradigm is to be able to operate on all rows sort of at once. -Original Message- From: R-help On Behalf Of Jorgen Harmse via R-help Sent: Wednesday, February 8, 2023 11:10 AM To: r-help@r-project.org; naresh_gurbux...@hotmail.com Subject: Re: [R] preserve

Re: [R] preserve class in apply function

2023-02-08 Thread avi.e.gross
with multiple columns, sometimes your function may need to use a ... to receive them. -Original Message- From: R-help On Behalf Of Naresh Gurbuxani Sent: Tuesday, February 7, 2023 3:29 PM To: PIKAL Petr Cc: r-help@r-project.org Subject: Re: [R] preserve class in apply function Thanks

Re: [R] preserve class in apply function

2023-02-08 Thread Jorgen Harmse via R-help
What are you trying to do? Why use apply when there is already a vector addition operation? df$x+df$y or as.numeric(df$x)+as.numeric(df$y) or rowSums(as.numeric(df[c('x','y')])). As noted in other answers, apply will coerce your data frame to a matrix, and all entries of a matrix must have the

Re: [R] preserve class in apply function

2023-02-07 Thread Naresh Gurbuxani
Thanks for all the responses. I need to use some text columns to determine method applied to numeric columns. Split seems to be the way to go. Sent from my iPhone > On Feb 7, 2023, at 8:31 AM, PIKAL Petr wrote: > > Hi Naresh > > If you wanted to automate the function a bit you can use

Re: [R] preserve class in apply function

2023-02-07 Thread PIKAL Petr
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]

Re: [R] preserve class in apply function

2023-02-07 Thread Rui Barradas
Às 12:51 de 07/02/2023, Naresh Gurbuxani escreveu: 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 =

Re: [R] preserve class in apply function

2023-02-07 Thread Andrew Simmons
It is not possible, apply() converts its argument to an array. You might be able to use split() and lapply() to solve your problem. On Tue, Feb 7, 2023, 07:52 Naresh Gurbuxani wrote: > > > Consider a data.frame whose different columns have numeric, character, > > and factor data. In apply