[R] replacing values in Array

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Tue Mar 13 12:59:45 CET 2012


Hello, "uday",

e.g.,

(data - 1) %/% 3 + 1

would do the job in your very specific situation, but take a look at

?findInterval

for possibly more interesting (because more general) solutions.

Hth  --  Gerrit

On Tue, 13 Mar 2012, uday wrote:

> I want to replace some values in my Array
> e.g
> data<- c(1,2,3,4,5,6,7,8,9,10,11,12)
> I would like to replace 1,2,3 by 1 , 4,5,6 by 2, 7,8,9 by 3 and 10,11,12 by
> 4
> I am expecting out put
> data<- 1 1 1  2 2 2 3 3 3 4 4 4
>
> I have tried replace function
> replace(data, (data==1 |data==2| data==3),1)
> replace(data, (data==4|data==5| data==6),2)
> replace(data, (data==7|data==8| data==9),3)
> replace(data, (data==10 |data==11| data==12),4)
> but it changes only for individual operation it does not apply to whole
> array.
> So how I should replace values of whole array ?
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/replacing-values-in-Array-tp4468739p4468739.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list