I've got a numeric vector with values ranging from 1 to 5, I would like to catagorize these values like this: 1 becomes catagory 1 3 becomes catagory 3 And everything else in catagory 2. The simple function I wrote beneath works for single numeric data, but for some reason I am unable to feed it vectors. Any help would be appreciated, as I'm fairly new to R. -- View this message in context: http://r.789695.n4.nabble.com/function-to-replace-values-doesn-t-work-on-vectors-tp4292235p4292235.html Sent from the R help mailing list archive at Nabble.com.
I don't see any signs of a simple function, but I'd use ifelse(). y <- ifelse(x == 1, 1, ifelse(x == 3, 3, 2)) or some such. (Lack of reproducible example means lack of actual testing.) Sarah On Fri, Jan 13, 2012 at 9:11 AM, WoutH <w.denhollander at lumc.nl> wrote:> ?I've got a numeric vector with values ranging from 1 to 5, I would like to > catagorize these values like this: > > 1 becomes catagory 1 > 3 becomes catagory 3 > And everything else in catagory 2. The simple function I wrote beneath works > for single numeric data, but for some reason I am unable to feed it vectors. > Any help would be appreciated, as I'm fairly new to R. >-- Sarah Goslee http://www.functionaldiversity.org
Berend Hasselman
2012-Jan-13 16:32 UTC
[R] function to replace values doesn't work on vectors
WoutH wrote> > I've got a numeric vector with values ranging from 1 to 5, I would like to > catagorize these values like this: > > 1 becomes catagory 1 > 3 becomes catagory 3 > And everything else in catagory 2. The simple function I wrote beneath > works for single numeric data, but for some reason I am unable to feed it > vectors. Any help would be appreciated, as I'm fairly new to R. > > function.123 <- function(x) > { > x1 <- ifelse(x == 1 | x == 3, return(x) ,return(2)) > return(x1) > } >Don't use the return() in the ifelse since return() returns immediately from the function. Just x1 <- ifelse(x==1 | x==3, x, 2) will do. Berend -- View this message in context: http://r.789695.n4.nabble.com/function-to-replace-values-doesn-t-work-on-vectors-tp4292235p4292691.html Sent from the R help mailing list archive at Nabble.com.
Reasonably Related Threads
- Nabble? Was Re: function to replace values doesn't work on vectors
- Barplot - Can't figure it out
- Samba wins eWeek and PC Magazine "Innovation in Infrastructure" (i3) award for best Enterprise Software !
- Samba wins eWeek and PC Magazine "Innovation in Infrastructure" (i3) award for best Enterprise Software !
- Find By ID