Re: [R] How to recode variables using base R

2010-03-30 Thread johannes rara
Thanks, you're a lifesaver. -J 2010/3/30 Henrique Dallazuanna : > Using lapply: > > as.data.frame(lapply(df, cut, breaks = c(-Inf, 3, 8, 16), labels = > c('x', 'y', 'z'))) > > > On Tue, Mar 30, 2010 at 10:14 AM, johannes rara > wrote: >> Thanks John and Henrique, my intention is to do this for

Re: [R] How to recode variables using base R

2010-03-30 Thread Henrique Dallazuanna
Using lapply: as.data.frame(lapply(df, cut, breaks = c(-Inf, 3, 8, 16), labels = c('x', 'y', 'z'))) On Tue, Mar 30, 2010 at 10:14 AM, johannes rara wrote: > Thanks John and Henrique, my intention is to do this for A, B and C > (all at once), so I'll have to wrap your solution into lapply or for

Re: [R] How to recode variables using base R

2010-03-30 Thread johannes rara
Thanks John and Henrique, my intention is to do this for A, B and C (all at once), so I'll have to wrap your solution into lapply or for loop? -J 2010/3/30 Henrique Dallazuanna : > You could try this also: > > cut(df$A, c(-Inf, 3, 8), labels = c('x', 'y')) > > On Tue, Mar 30, 2010 at 8:30 AM, joh

Re: [R] How to recode variables using base R

2010-03-30 Thread Henrique Dallazuanna
You could try this also: cut(df$A, c(-Inf, 3, 8), labels = c('x', 'y')) On Tue, Mar 30, 2010 at 8:30 AM, johannes rara wrote: > Hi, > > Is there an efficient way recoding variables in a data.frame using > base R? My purpose is to create > new variables and attach them into old data.frame. The ba

Re: [R] How to recode variables using base R

2010-03-30 Thread John Fox
Dear Johannes, You can use cascading ifelse()s: > df$A <- with(df, ifelse(A <= 3, "x", ifelse(A > 3 & A <= 8, "y", "z"))) > df$A [1] "x" "x" "x" "y" "y" This command assumes that you want all values that don't map into "x"s and "y"s to be "z"s, but you could adapt it if that's not what you want