Re: [R] preserve class in apply function
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], 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 On Behalf Of Naresh Gurbuxani > > Sent: Tuesday, February 7, 2023 1:52 PM > > To: r-help@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@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@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 __ R-help@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.
Re: [R] preserve class in apply function
Jorgen is correct that for many purposes, viewing a data.frame as a collection of vectors of the same length allows you to code fairly complex logic using whichever vectors you want and result in a vector answer, either externally or as a new column. Text columns used to make some decisions in the function are also usable using vectorized functions like ifelse(cond, when_true, when_false). And, although much can be done in base R, people often use the dplyr/tidyverse function of mutate() to do such calculations in a slightly less wordy way. You may be looking at apply() as a way to operate 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 class in apply function 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 same type. Regards, Jorgen Harmse. Message: 1 Date: Tue, 7 Feb 2023 07:51:50 -0500 From: Naresh Gurbuxani To: "r-help@r-project.org" Subject: [R] preserve class in apply function Message-ID: Content-Type: text/plain; charset="us-ascii" > 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 [[alternative HTML version deleted]] __ R-help@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@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.
Re: [R] preserve class in apply function
Naresh, This is a common case where the answer to a question is to ask the right question. Your question was how to make apply work. My question is how can you get the functionality you want done in some version of R. Apply is a tool and it is only one of many tools and may be the wrong one for your task. For a data.frame there can be lots of tools you may investigate both in vase R and add-on packages like dplyr in the tidyverse. As has been pointed out, a side-effect of apply is to make a matrix and R automagically figures out what the most specific kind of data type it can handle. So solutions range from not including any columns that are not numeric, if that makes sense, to accepting they are all going to be of type character and in the function you apply, convert them individually back to what you want. One straightforward solution is to make a loop indexed to the number of rows in your data.frame and process the variables in each row using [] notation. Not fast, but you see what you have. Another is functions like pmap() in the purr package. Yet another might be the rowwise() function in the dplyr package. It depends on what you want to do. Note 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 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 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 On Behalf Of Naresh >> Gurbuxani >> Sent: Tuesday, February 7, 2023 1:52 PM >> To: r-help@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@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@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@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.
Re: [R] preserve class in apply function
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 same type. Regards, Jorgen Harmse. Message: 1 Date: Tue, 7 Feb 2023 07:51:50 -0500 From: Naresh Gurbuxani To: "r-help@r-project.org" Subject: [R] preserve class in apply function Message-ID: Content-Type: text/plain; charset="us-ascii" > 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 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] preserve class in apply function
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 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 On Behalf Of Naresh Gurbuxani >> Sent: Tuesday, February 7, 2023 1:52 PM >> To: r-help@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@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@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.
Re: [R] preserve class in apply function
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 On Behalf Of Naresh Gurbuxani > Sent: Tuesday, February 7, 2023 1:52 PM > To: r-help@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@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@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.
Re: [R] preserve class in apply function
À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 = 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@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. Hello, The last form, apply(mydf[,c("x", "y")], 1, function(row) {row["x"] + row["y"]}) is the right one. If your data has columns of a mix of classes, then the rows which are vectors are coerced to the greatest common denominator class. From ?c: Details The output type is determined from the highest type of the components in the hierarchy NULL < raw < logical < integer < double < complex < character < list < expression. Also, since you have a data.frame the following is another possible way: apply(mydf[c("x", "y")], 1, function(row) {row["x"] + row["y"]}) This doesn't work with matrices. Hope this helps, Rui Barradas __ R-help@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.
Re: [R] preserve class in apply function
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 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@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@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] 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@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.