similar to: help with element-by-element sum with NA

Displaying 20 results from an estimated 20000 matches similar to: "help with element-by-element sum with NA"

2008 Jan 29
2
add/subtract matrices, ignoring NA or missing values
Hi, For example, given two 2x2 matrices m1 and m2. I would like to add/subtract element by element > m1 [,1] [,2] [1,] NA NA [2,] 1 2 > m2 [,1] [,2] [1,] 1 NA [2,] NA 2 > m1 + m2 [,1] [,2] [1,] NA NA [2,] NA 4 How can I ignore the NA, and get this ? Hope the solution can be extended to subtract and modulo also. [,1] [,2]
2002 Apr 25
4
sum() with na.rm=TRUE, again
Hi: I remember a post several days ago by Jon Baron, concerning the behavior of sum() when one sets na.rm=TRUE: the result will be a zero sum for a vector of all NA's, as here, for the second row: > ss<- data.frame(x=c(1,NA,3,4),y=c(2,NA,4,NA)) > ss x y 1 1 2 2 NA NA 3 3 4 4 4 NA > apply(ss,1,sum,na.rm=TRUE) 1 2 3 4 3 0 7 4 I am rather alarmed by that zero, because
2005 Jun 29
3
return NA
A<-c(1,2,NA,7,5) B<-c(3,4,1,4,1) C<-c(6,5,6,NA,9) D<-c(8,7,4,6,2) df1<-cbind(A,B,C,D) for(i in seq(1,ncol(df1)-1, by=2)) { ifelse(df1[,i]=="NA",df1[,i+1]=="NA",df1[,] ) } Tried several variations but none worked. I wish to find any NA's in column's 1 or 3 and change the numerical value to the right of the " NA"'s . In this case I
2017 Nov 04
1
ans[nas] <- NA in 'ifelse' (was: ifelse() woes ... can we agree on a ifelse2() ?)
Removal of ans[nas] <- NA from the code of function 'ifelse' in R is not committed (yet). Why? -------------------------------------------- On Mon, 28/11/16, Martin Maechler <maechler at stat.math.ethz.ch> wrote: Subject: Re: [Rd] ifelse() woes ... can we agree on a ifelse2() ? Cc: R-devel at r-project.org, maechler at stat.math.ethz.ch Date: Monday, 28 November, 2016, 10:00
2012 Nov 15
4
using ifelse to remove NA's from specific columns of a data frame containing strings and numbers
Hi everyone, I have a data frame one of whose columns is a character vector and the rest are numeric, and in debugging a script, I noticed that an ifelse call seems to be coercing the character column to a numeric column, and producing unintended values as a result. Roughly, here's what I tried to do: df: a data frame with, say, the first column as a character column and the second and
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
Should not the result be NULL if you have removed the NA with na.rm=TRUE ? B. > On Mar 21, 2018, at 11:44 AM, Stefano Sofia <stefano.sofia at regione.marche.it> wrote: > > Dear list users, > let me ask you this trivial question. I worked on that for a long time, by now. > Suppose to have a data frame with NAs and to sum some columns with rowSums: > > df <-
2018 Mar 21
5
Sum of columns of a data frame equal to NA when all the elements are NA
Dear list users, let me ask you this trivial question. I worked on that for a long time, by now. Suppose to have a data frame with NAs and to sum some columns with rowSums: df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10)) df[1, ] <- NA rowSums(df[ , which(names(df) %in% c("A","B"))], na.rm=T) If all the elements of the selected columns are NA, rowSums
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
On 21/03/2018 11:44 AM, Stefano Sofia wrote: > Dear list users, > let me ask you this trivial question. I worked on that for a long time, by now. > Suppose to have a data frame with NAs and to sum some columns with rowSums: > > df <- data.frame(A = runif(10), B = runif(10), C = rnorm(10)) > df[1, ] <- NA > rowSums(df[ , which(names(df) %in%
2012 Jan 29
2
How do I turn NA's to zeroes when a combination lacks one element?
Hi all, I am very new to R. I am taking a course and am trying to complete my first assignment. For the assignment I have to get the program to generate different color combinations possible for a sample size of 55 with the probabilities of each color being chosen as: .24, .24, .16, .20, .13, .14. Here is what I've come up with... sample.size<- 55 MM.probability<- c(.24, .24, .16,
2018 Mar 21
3
Sum of columns of a data frame equal to NA when all the elements are NA
What do you mean by "should not"? NULL means "missing object" in R. The result of the sum function is always expected to be numeric... so NA_real or NA_integer could make sense as possible return values. But you cannot compute on NULL so no, that doesn't work. See the note under the "Value" section of ?sum as to why zero is returned when all inputs are removed.
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
Surely the result of summation of non-existent values is not defined, is it not? And since the NA values have been _removed_, there's nothing left to sum over. In fact, pretending the the result in that case is zero would appear audacious, no? Cheers, Boris > On Mar 21, 2018, at 12:58 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote: > > What do you mean by
2006 Jun 04
2
surprising dates
I wonder if this is an intentional feature or an oversight. in some column summaries or in ifelse operations, apparently I am losing the date property of my vector. > a <- c(198012, 198101, 198102) > b <- a*100+31 > c <- as.Date( as.character(b), "%Y%m%d" ) > summary(c) Min. 1st Qu. Median Mean 3rd Qu. Max.
2006 May 01
5
Adding elements in an array where I have missing data.
This is a simple question but I cannot seem to find the answer. I have two vectors but with missing data and I want to add them together with the NA's being ignored. Clearly I need to get the NA ignored. na.action? I have done some searching and cannot get na.action to help. This must be a common enough issue that the answer is staring me in the face but I just don't see it. Simple
2007 Jul 31
1
aggregate.data.frame - prevent conversion to factors? show statistics for NA values of "by" variable?
I have a two question regarding the "aggregate.data.frame" method of the "aggregate" function. My situation: a. My "x" variable is a data.frame ("mydf") with two columns, both columns of type/format "numeric". b. My "by" variable is a data.frame("mybys") with two columns, both columns of type/format "character". c.
2018 Mar 21
2
Sum of columns of a data frame equal to NA when all the elements are NA
No. The empty sum is zero. Adding it to another sum should not change it. Nothing audacious about that. This is consistent; other definitions just cause trouble. -pd > On 21 Mar 2018, at 18:05 , Boris Steipe <boris.steipe at utoronto.ca> wrote: > > Surely the result of summation of non-existent values is not defined, is it not? And since the NA values have been _removed_,
2018 Mar 21
0
Sum of columns of a data frame equal to NA when all the elements are NA
I see: consistency with additive identity. That makes sense. Thanks. B. > On Mar 21, 2018, at 1:22 PM, peter dalgaard <pdalgd at gmail.com> wrote: > > No. The empty sum is zero. Adding it to another sum should not change it. Nothing audacious about that. This is consistent; other definitions just cause trouble. > > -pd > >> On 21 Mar 2018, at 18:05 , Boris
2018 Mar 21
1
Sum of columns of a data frame equal to NA when all the elements are NA
"I see: consistency with additive identity. " Ummm, well: > 1+NULL numeric(0) > sum(1,NULL) [1] 1 Of course, there could well be something here I don't get, but that doesn't look very consistent to me. However, as I said privately, so long as the corner case behavior is documented, which it is, I don't care. Cheers, Bert Bert Gunter "The trouble with
2011 Apr 03
1
Function for finding NA's
Quick question, I tried to find a function in available packages to find NA's for an entire data set (or single variables) and report the row of missing values (NA's for each column). I searched the typical routes through the blogs and the help manuals for 15 minutes. Rather than spend any more time searching I created my own function to do this (probably in less time than it would
2009 Jul 07
6
how to count number of elements in a vector that are not NA ?
Hi, is there a simpler way to count the number of elements in a vector that are not NA than this: countN <- function (v) { return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) } ? - Godmar
2012 Aug 21
1
About matrix manipulation
Dear list, I'm trying to create a matrix by combining the sites that species occur in a new matrix with species as rows and sites as columns. The main matrix is "mat": mat <- as.data.frame(cbind(sp1=c(rep(0, 5), rep(1, 5)),sp2=sample(c(rep(0, 6),rep(1, 4))), fac=c(rep("a", 3), rep("b", 3),rep("c", 4)))) The first two columns are species and the