[R] if else
Don MacQueen
macq at llnl.gov
Mon Jun 8 21:36:36 CEST 2009
I think one of the other good suggestions might have had a typo in it.
And, I would like to append an alternate approach
that can be generalized to more columns.
In my opinion, nested ifelse() expressions are
difficult to read and understand, and therefore
difficult to get right.
Easier to write one expression for each of your
criteria. But do the last one first
## X1+X2 if X1 and X2 are not NA
data$X3 <- with(data, X1+X2)
## X1, if X2=NA
data$X3[is.na(data$X2)] <- data$X1[is.na(data$X2)]
## X2, if X1=NA
data$X3[is.na(data$X1)] <- data$X2[is.na(data$X1)]
But, what if you had three columns, X1, X2, X3,
and wanted X4 to be the sum of the others,
excluding NA values (which is essentially what
you're doing)
data$X4 <- apply(data[,c('X1','X2','X3')] ,
1 ,
function(xr) {if (any(!is.na(xr))) sum(xr,na.rm=TRUE) else NA}
)
Example:
firm<-c(rep(1:3,4))
year<-c(rep(2001:2003,4))
X1<-rep(c(10,NA),6)
X2<-rep(c(5,NA,2),4)
X3 <- c(NA,NA,rep(1,9),NA)
data<-data.frame(firm, year,X1,X2,X3)
data$X4 <- apply(data[,c('X1','X2','X3')] ,
1 ,
function(xr) {if (any(!is.na(xr))) sum(xr,na.rm=TRUE) else NA}
)
print(data)
Or, for your case, with just the two columns:
data$X3 <- apply(data[,c('X1','X2')] ,
1 ,
function(xr) {if (any(!is.na(xr))) sum(xr,na.rm=TRUE) else NA}
)
should do it.
-Don
At 6:48 PM +0100 6/8/09, Cecilia Carmo wrote:
>Hi R-helpers!
>
>I have the following dataframe:
>firm<-c(rep(1:3,4))
>year<-c(rep(2001:2003,4))
>X1<-rep(c(10,NA),6)
>X2<-rep(c(5,NA,2),4)
>data<-data.frame(firm, year,X1,X2)
>data
>
>So I want to obtain the same dataframe with a variable X3 that is:
>X1, if X2=NA
>X2, if X1=NA
>X1+X2 if X1 and X2 are not NA
>
>So my final data is
>X3<-c(15,NA,12,5,10,2,15,NA,12,5,10,2)
>finaldata<-data.frame(firm, year,X1,X2,X3)
>
>I've tried this
>finaldata<-ifelse(data$X1==NA,ifelse(data$X2==NA,NA,X2),ifelse(data$varvendas==NA,X1,X1+X2))
>But I got just NA in X3.
>Anyone could help me with this?
>
>Thanks in advance,
>
>Cecília (Universidade de Aveiro - Portugal)
>
>______________________________________________
>R-help at r-project.org mailing list
>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.
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062
More information about the R-help
mailing list