Re: [R] Issues with the way Apply handled NA's

2016-11-16 Thread S Ellison


> -Original Message-
> 
> You can check for an empty vector as follows:
> ...
> vals <- apply(plabor[c("colA","colB","colC")],1,function(x) 
> length(na.omit(x)))
> vals # [1] 3 0 3 2 
> <- ifelse(vals>0, plabor$colD, NA) 
> 
plabor

A slightly more compact variant that avoids the intermediate 'vals' variable is 
to apply an anonymous function that does the check internally:

plabor$colD <- apply(plabor, 1, function(x) if(all(is.na(x))) NA else prod(x, 
na.rm=TRUE))

S Ellison




***
This email and any attachments are confidential. Any use, copying or
disclosure other than by the intended recipient is unauthorised. If 
you have received this message in error, please notify the sender 
immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com 
and delete this message and any copies from your computer and network. 
LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
__
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] Issues with the way Apply handled NA's

2016-11-14 Thread David L Carlson
This behavior is documented in the manual page:

> prod(NULL)
[1] 1

You can check for an empty vector as follows:

plabor <- structure(list(colA = c(6, NA, 3, 4), colB = c(25, NA, 2, 7), 
colC = c(3, NA, 19, NA)), .Names = c("colA", "colB", "colC"),
class = "data.frame", row.names = c(NA, -4L))
# Use dput() to send data to the list

plabor$colD = apply(plabor[c("colA","colB","colC")], 1, prod, na.rm=TRUE)
# Use TRUE and FALSE since the abbreviations T and F are not reserved and
# could be redefined

vals <- apply(plabor[c("colA","colB","colC")],1,function(x) length(na.omit(x)))
vals
# [1] 3 0 3 2
plabor$colD <- ifelse(vals>0, plabor$colD, NA)
plabor

#   colA colB colC colD
# 16   253  450
# 2   NA   NA   NA   NA
# 332   19  114
# 447   NA   28

-
David L Carlson
Department of Anthropology
Texas A University
College Station, TX 77840-4352


-Original Message-
From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Olu Ola via 
R-help
Sent: Monday, November 14, 2016 2:52 PM
To: R-help Mailing List
Subject: [R] Issues with the way Apply handled NA's

 Hello,I have a data set called plabor and have the following format:

| ColA | ColB | Colc |
| 6 | 25 | 3 |
| NA | NA | NA |
| 3 | 2 | 19 |
| 4 | 7 | NA |


I wanted to find the product of the three columns for each of the rows and I 
used the apply function follows:
plabor$colD = apply(plabor[c("colA","colB","colc")],1,prod,na.rm=T)
The result are as follows:

| ColA | ColB | Colc | colD |
| 6 | 25 | 3 | 450 |
| NA | NA | NA | 1 |
| 3 | 2 | 19 | 114 |
| 4 | 7 | NA | 28 |


The second row results is 1 instead of being ignored.
How do I deal with this issue because I do not want to exclude these data 
points with all NA's?
Regards

[[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.

[R] Issues with the way Apply handled NA's

2016-11-14 Thread Olu Ola via R-help
 Hello,I have a data set called plabor and have the following format:

| ColA | ColB | Colc |
| 6 | 25 | 3 |
| NA | NA | NA |
| 3 | 2 | 19 |
| 4 | 7 | NA |


I wanted to find the product of the three columns for each of the rows and I 
used the apply function follows:
plabor$colD = apply(plabor[c("colA","colB","colc")],1,prod,na.rm=T)
The result are as follows:

| ColA | ColB | Colc | colD |
| 6 | 25 | 3 | 450 |
| NA | NA | NA | 1 |
| 3 | 2 | 19 | 114 |
| 4 | 7 | NA | 28 |


The second row results is 1 instead of being ignored.
How do I deal with this issue because I do not want to exclude these data 
points with all NA's?
Regards

[[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.