[R] Nested functions.

Paul Hiemstra p.hiemstra at geo.uu.nl
Thu Aug 30 17:46:02 CEST 2007


nalluri pratap schreef:
> Hi All,
>    
>   I have two variables X, Y. The question is "if the value of X is equal to one, then the values in Y have to be reversed other wise it should not perfom any action. I think this should be done using lapply function?
>    
>   Example
>    
>   Y values : 1 2 3  NA
>   X      Y (ORIGINAL)     Y (REVERSED)
>   1             NA                       1
>   0             ----                      ---
>   1               2                         3
>   1               1                         4
>   1               3                         2
>   ...............
>    
>   Can anyone provide solution to this?
>    
>   Thanks,
>   Pratap                  
>    
>
>        
> ---------------------------------
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>   
Dear Pratap,

You could try something like this:

x = c(1,0,1,1)
y = c(1,2,3,NA)
y_rev = rev(y)
ifelse(x == 1, y_rev, y)

hope this helps,

Paul


-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone: 	+31302535773
Fax:	+31302531145
http://intamap.geo.uu.nl/~paul



More information about the R-help mailing list