Re: [R] recode the same subset of variables in several list elements

2015-04-07 Thread Jim Lemon
Hi Simon, Let's see. If I wrap the code into a function: reverse.df.elements<-function(df,pattern="i",newrange=c(3,1)) { revlist<-grep(pattern,names(df),fixed=TRUE) df[,revlist]<-sapply(df[,revlist],rescale,newrange) return(df) } Then this might do the trick: lapply(list1,reverse.df.elements,

Re: [R] recode the same subset of variables in several list elements

2015-04-06 Thread Simon Kiss
Hi Jim, So that does the rescale part very efficiently. But I’d like to know how to do that on each list element using lapply or llply. I have about 4 data frames and a few other recodes to do so automating would be nice, rather than applying your code to each individual list element. simon > O

Re: [R] recode the same subset of variables in several list elements

2015-04-02 Thread Jim Lemon
Hi Simon, How about this? library(plotrix) revlist<-grep("i",names(df),fixed=TRUE) df[,revlist]<-sapply(df[,revlist],rescale,c(3,1)) Jim On Fri, Apr 3, 2015 at 6:30 AM, Simon Kiss wrote: > Hi there: I have a list of data frames with identical variable names. > I’d like to reverse scale the s

[R] recode the same subset of variables in several list elements

2015-04-02 Thread Simon Kiss
Hi there: I have a list of data frames with identical variable names. I’d like to reverse scale the same variables in each data.frame. I’d appreciate any one’s suggestions as to how to accomplish this. Right now, I’m working with the code at the very bottom of my sample data. Thanks, Simon K

Re: [R] recode the ID with sequential order of a dataset

2014-06-04 Thread arun
Hi York, Using "dat" (assuming that the data is ordered by "ID") dat$NEWID <- cumsum(c(TRUE,with(dat, ID[-1]!= ID[-length(ID)]))) A.K. Hi arun, Thanks for your reply. I think you misunderstood my question, for the new ID, I would like to have the following pattern, ID TIME NEWID 1254 0  

Re: [R] recode the ID with sequential order of a dataset

2014-05-23 Thread arun
Hi, May be this helps: #if the data is ordered for the "TIME" column as in the example dat <- read.table(text="ID TIME 1254 0 1254 1 1254 3 1254 5 1254 14 3236 0 3236 36 3236 93 1598 0 1598 0.5 1598 1 1598 2 1598 3 1598 12 1598 36 1598 75 1598 95 1598 120",sep="",header=TRUE)  dat$NewID <- with(

Re: [R] Recode values

2013-11-22 Thread peter dalgaard
On 22 Nov 2013, at 11:13 , lillosdos wrote: > Hi I'm Pasquale, > I need to recode variables (columns) of a dataframe (call it X). The > observations (rows) are coded as numeric 0,1,2 and NA. I managed to use the > lapply() function with recode() as FUN and for() loop but I failed. > *My problem

[R] Recode values

2013-11-22 Thread lillosdos
Hi I'm Pasquale, I need to recode variables (columns) of a dataframe (call it X). The observations (rows) are coded as numeric 0,1,2 and NA. I managed to use the lapply() function with recode() as FUN and for() loop but I failed. *My problem is that for each columns the recoding system is different

Re: [R] Recode values

2013-11-22 Thread arun
Hi, May be this helps: set.seed(49) dat1 <- as.data.frame(matrix(sample(c(NA,0:2),20,replace=TRUE),ncol=2)) dat2 <- dat1  lst1 <- list(letters[1:3],letters[26:24]) library(plyr)  dat1[] <-lapply(seq_len(ncol(dat1)),function(i) {x1 <-dat1[,i]; x2 <- lst1[[i]]; mapvalues(x1,c(0,1,2),x2)}) #Or dat

Re: [R] recode: how to avoid nested ifelse

2013-06-10 Thread Paul Johnson
Thanks, guys. On Sat, Jun 8, 2013 at 2:17 PM, Neal Fultz wrote: > rowSums and Reduce will have the same problems with bad data you alluded > to earlier, eg > cg = 1, hs = 0 > > But that's something to check for with crosstabs anyway. > > This "wrong data" thing is a distraction here. I guess I

Re: [R] recode: how to avoid nested ifelse

2013-06-08 Thread Neal Fultz
rowSums and Reduce will have the same problems with bad data you alluded to earlier, eg cg = 1, hs = 0 But that's something to check for with crosstabs anyway. Side note: you should check out the microbenchmark pkg, it's quite handy. R>require(microbenchmark) R>microbenchmark( + f1(cg,hs,es

Re: [R] recode: how to avoid nested ifelse

2013-06-07 Thread Joshua Wiley
I still argue for na.rm=FALSE, but that is cute, also substantially faster f1 <- function(x1, x2, x3) do.call(paste0, list(x1, x2, x3)) f2 <- function(x1, x2, x3) pmax(3*x3, 2*x2, es, 0, na.rm=FALSE) f3 <- function(x1, x2, x3) Reduce(`+`, list(x1, x2, x3)) f4 <- function(x1, x2, x3) rowSums(cbind(

Re: [R] recode: how to avoid nested ifelse

2013-06-07 Thread Neal Fultz
I would do this to get the highest non-missing level: x <- pmax(3*cg, 2*hs, es, 0, na.rm=TRUE) rock chalk... -nfultz On Fri, Jun 07, 2013 at 06:24:50PM -0700, Joshua Wiley wrote: > Hi Paul, > > Unless you have truly offended the data generating oracle*, the > pattern: NA, 1, NA, should be a da

Re: [R] recode: how to avoid nested ifelse

2013-06-07 Thread Joshua Wiley
Hi Paul, Unless you have truly offended the data generating oracle*, the pattern: NA, 1, NA, should be a data entry error --- graduating HS implies graduating ES, no? I would argue fringe cases like that should be corrected in the data, not through coding work arounds. Then you can just do: x <-

[R] recode: how to avoid nested ifelse

2013-06-07 Thread Paul Johnson
In our Summer Stats Institute, I was asked a question that amounts to reversing the effect of the contrasts function (reconstruct an ordinal predictor from a set of binary columns). The best I could think of was to link together several ifelse functions, and I don't think I want to do this if the e

Re: [R] recode categorial vars into binary data

2013-05-16 Thread Alain D.
Hi David, [This is a late reaction to a question I posted some time ago...] thanks to your suggestion I found another solution which does carry out the random assignment. df[,2] <- as.numeric( rank(df[,2],ties.method="random") > length(df[,2])/2 ) Maybe this will be of interest to the R-List (a

Re: [R] recode categorial vars into binary data

2013-05-07 Thread Chris Stubben
First off, stop using cbind() when it is not needed. You will not see the reason when the columns are all numeric but you will start experiencing pain and puzzlement when the arguments are of mixed classes. The data.frame function will do what you want. (Where do people pick up this practice a

Re: [R] recode categorial vars into binary data

2013-05-07 Thread David Winsemius
On May 7, 2013, at 9:20 AM, D. Alain wrote: > Dear R-List, > > I would like to recode categorial variables into binary data, so that all > values above median are coded 1 and all values below 0, separating each var > into two equally large groups (e.g. good performers = 0 vs. bad performers

Re: [R] recode categorial vars into binary data

2013-05-07 Thread Rui Barradas
Hello, First of all, you don't need as.data.frame(cbind(...)). It's much better to simply do data.frame(...). As for the conversion, the following function doesn't use randomness but gets the job done df <- data.frame(snr=c(1,2,3,4,5,6,7,8,9,10), k1=c(1,1,4,2,3,2,2,5,2,2), k

[R] recode categorial vars into binary data

2013-05-07 Thread D. Alain
Dear R-List, I would like to recode categorial variables into binary data, so that all values above median are coded 1 and all values below 0, separating each var into two equally large groups (e.g. good performers = 0 vs. bad performers =1). I have not succeeded so far in finding a nice solut

Re: [R] recode data according to quantile breaks

2013-02-19 Thread arun
rom: D. Alain To: Mailinglist R-Project Cc: Sent: Tuesday, February 19, 2013 5:01 AM Subject: [R] recode data according to quantile breaks Dear R-List, I would like to recode my data according to quantile breaks, i.e. all data within the range of 0%-25% should get a 1, >25%-50% a 2 etc. Is

Re: [R] recode data according to quantile breaks

2013-02-19 Thread Jorge I Velez
Hi Alain, The following should get you started: apply(df[,-1], 2, function(x) cut(x, breaks = quantile(x), include.lowest = TRUE, labels = 1:4)) Check ?cut and ?apply for more information. HTH, Jorge.- On Tue, Feb 19, 2013 at 9:01 PM, D. Alain <> wrote: > Dear R-List, > > I would like to re

[R] recode data according to quantile breaks

2013-02-19 Thread D. Alain
Dear R-List, I would like to recode my data according to quantile breaks, i.e. all data within the range of 0%-25% should get a 1, >25%-50% a 2 etc. Is there a nice way to do this with all columns in a dataframe. e.g. df<- f<-data.frame(id=c("x01","x02","x03","x04","x05","x06"),a=c(1,2,3,4,5,

Re: [R] Recode function car package erases previous values

2012-10-24 Thread arun
; pharm #[1] "no resp" "No"  "No"  "Yes" "Yes" NA    table(pharm) #pharm   #   No no resp Yes     #  2   1   2 A.K. ----- Original Message - From: Pancho Mulongeni To: arun Cc: R help Sent: Wednesday

Re: [R] Recode function car package erases previous values

2012-10-24 Thread John Fox
Dear Pancho, I'm not going to respond to the subsequent messages in this thread, since you appear to have solved your problem, just explain that what you did originally was to recode the variable B20_C1, creating the new variable pharm. Then you recoded another variable, nr.B20C, replacing the

Re: [R] Recode function car package erases previous values

2012-10-24 Thread Pancho Mulongeni
bject: Re: [R] Recode function car package erases previous values Hi Pancho, I tried ur method: pharm<-as.factor(recode(dat1$B20_C1,"1='Yes';0='No'"))  pharm #[1] No   No   Yes  Yes  #Levels: No Yes pharm[dat1$nrB20C==1]<-'no resp' #Warning message:

Re: [R] Recode function car package erases previous values

2012-10-24 Thread arun
uot;no resp") :  # invalid factor level, NAs generated  pharm #[1] No   No   Yes  Yes  #Levels: No Yes Not sure how you got the result. A.K.   - Original Message - From: Pancho Mulongeni To: arun Cc: R help Sent: Wednesday, October 24, 2012 9:51 AM Subject: RE: [R] Recode f

Re: [R] Recode function car package erases previous values

2012-10-24 Thread Pancho Mulongeni
-Original Message- From: arun [mailto:smartpink...@yahoo.com] Sent: Wednesday, October 24, 2012 3:31 PM To: Pancho Mulongeni Cc: R help Subject: Re: [R] Recode function car package erases previous values Hi, May be this helps: set.seed(1) dat1<-data.frame(B20_C1=c(NA,sample(0:

Re: [R] Recode function car package erases previous values

2012-10-24 Thread arun
NA    table(dat2$pharm) # No no response Yes  # 3   1   1 A.K. - Original Message - From: Pancho Mulongeni To: "r-help@r-project.org" Cc: Sent: Wednesday, October 24, 2012 5:17 AM Subject: [R] Recode function car package

[R] Recode function car package erases previous values

2012-10-24 Thread Pancho Mulongeni
Hi all, I am attempting to create a new variable based on values of other variables. The variable is called pharm. It basically takes the numeric code of 1 as yes and 0 to be No from the variable B20_C1 (a question on a survey). However, I would also like to have a level for non-respondents and

Re: [R] Recode Variable

2012-04-12 Thread S Ellison
> myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" try myData[!is.na(myData$var1) & myData$var1==5;"var1"]<-NA or, more simply, myData$var1[myData$var1==5]<-NA *** This email and any attachments are confid

Re: [R] Recode Variable

2012-04-12 Thread Rainer Schuermann
1. Some data structured the way you are using would have been helpful. I used Tal Galil's play data and set up a dataframe with the variable names you are using: structure(list(var1 = c(1, NA, NA, 4, 5, 6, 7, 8, 9, 10, 5), var2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5)), .Names = c("var1", "var2

Re: [R] Recode Variable

2012-04-12 Thread Milan Bouchet-Valat
Le jeudi 12 avril 2012 à 11:08 +0200, David Studer a écrit : > Hello everybody, > > I know this is pretty basic stuff, but could anyone explain me how to > recode a single value of a variable > into a missing value? > > I used to do it like this: > > myData[myData$var1==5;"var1"]<-NA

Re: [R] Recode Variable

2012-04-12 Thread Milan Bouchet-Valat
Le jeudi 12 avril 2012 à 12:29 +0300, Tal Galili a écrit : > Hi David, > You bring up a good question. I am not sure what is the "right" way to > solve it. But here is a simple solution I put together: > > x = c(1:10,5) > y = x > x[c(2,3)] <- NA > > # reproducing the problem: > y[x==5] > > na2

Re: [R] Recode Variable

2012-04-12 Thread Tal Galili
Hi David, You bring up a good question. I am not sure what is the "right" way to solve it. But here is a simple solution I put together: x = c(1:10,5) y = x x[c(2,3)] <- NA # reproducing the problem: y[x==5] na2F <- function(x) { x2 <- x x2[is.na(x)] <- F x2 } na2F(x==5) # "solved

[R] Recode Variable

2012-04-12 Thread David Studer
Hello everybody, I know this is pretty basic stuff, but could anyone explain me how to recode a single value of a variable into a missing value? I used to do it like this: myData[myData$var1==5;"var1"]<-NA # recode value "5" into "NA" But the column "var1" already contains NAs, whic

Re: [R] recode Variable in dependence of values of two other variables

2011-08-12 Thread Dennis Murphy
Hi: Here are several equivalent ways to produce your desired output: # Base package: transform() df <- transform(df, mean = ave(x, id, FUN = mean)) # plyr package library('plyr') ddply(df, .(id), transform, mean = mean(x)) # data.table package library('data.table') dt <- data.table(df, key = '

Re: [R] recode Variable in dependence of values of two other variables

2011-08-12 Thread Mikhail Titov
?aggregate aggregate(X~ID, your.data.frame.goes.here, "mean") Mikhail > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Julia Moeller > Sent: Friday, August 12, 2011 10:10 AM > To: r-help@r-project.

[R] recode Variable in dependence of values of two other variables

2011-08-12 Thread Julia Moeller
Hi, as an R-beginner, I have a recoding problem and hope you can help me: I am working on a SPSS dataset, which I loaded into R (load("C:/...) I have 2 existing Variables: "ID" and "X" , and one variable to be computed: meanX.dependID (=mean of X for all rows in which ID has the same value)

Re: [R] Recode numbers

2011-06-01 Thread Dennis Murphy
Hi: Here's another option: rep(b, rle(a)$lengths) > identical(a1, rep(b, rle(a)$lengths)) [1] TRUE rle(a)$lengths computes a table of the number of consecutive repeats of a number (or run lengths). It will have the same length as b in this case. Using rep() with the table of lengths as repetiti

Re: [R] Recode numbers

2011-06-01 Thread William Dunlap
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Lisa > Sent: Wednesday, June 01, 2011 12:28 PM > To: r-help@r-project.org > Subject: Re: [R] Recode

Re: [R] Recode numbers

2011-06-01 Thread Lisa
Thank you, Duncan, Here “a” has the length of 24, and “b” has the length of 20 with numbers from 1 to 20 uniquely. I just want encode “a” from 1 to 20 based on “a” current order using “b”. So, a1[1] = b[1] = 1 a1[2] = b[2] = 5 a1[3] = a1[4] = b[3] = 8 (since third and fourth numbers are the same i

Re: [R] Recode numbers

2011-06-01 Thread Duncan Murdoch
(The attributions are a little messed up here:) I have two sets of numbers that look like a<- c(1, 2, 3, 3, 4, 4, 5, 6, 1, 2, 2, 3, 1, 2, 1, 2, 3, 3, 4, 5, 1, 2, 3, 4) b<- c(1, 5, 8, 9, 14, 20, 3, 10, 12, 6, 16, 7, 11, 13, 17, 18, 2, 4, 15, 19) I just want to use “b” to encode “a” so that “a”

Re: [R] Recode numbers

2011-06-01 Thread Lisa
Thank you for your help, Pete. I tried b[a], but it is not a1. -- View this message in context: http://r.789695.n4.nabble.com/Recode-numbers-tp3566395p3566534.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailin

[R] Recode numbers

2011-06-01 Thread Lisa
Dear all, I have two sets of numbers that look like a <- c(1, 2, 3, 3, 4, 4, 5, 6, 1, 2, 2, 3, 1, 2, 1, 2, 3, 3, 4, 5, 1, 2, 3, 4) b <- c(1, 5, 8, 9, 14, 20, 3, 10, 12, 6, 16, 7, 11, 13, 17, 18, 2, 4, 15, 19) I just want to use “b” to encode “a” so that “a” looks like a1<- c(1, 5, 8, 8, 9, 9,

Re: [R] Recode numbers

2011-06-01 Thread Pete Brecknock
Lisa wrote: > > Dear all, > > I have two sets of numbers that look like > > a <- c(1, 2, 3, 3, 4, 4, 5, 6, 1, 2, 2, 3, 1, 2, 1, 2, 3, 3, 4, 5, 1, 2, > 3, 4) > > b <- c(1, 5, 8, 9, 14, 20, 3, 10, 12, 6, 16, 7, 11, 13, 17, 18, 2, 4, 15, > 19) > > I just want to use “b” to encode “a” so that “a”

Re: [R] recode according to specific sequence of characters within a string variable

2011-02-04 Thread Greg Snow
Healthcare greg.s...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of D. Alain > Sent: Friday, February 04, 2011 5:33 AM > To: r-help@r-project.org > Subject: [R] recode according to sp

Re: [R] recode according to specific sequence of characters within a string variable

2011-02-04 Thread Greg Snow
...@imail.org 801.408.8111 > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- > project.org] On Behalf Of Denis Kazakiewicz > Sent: Friday, February 04, 2011 6:26 AM > To: Marc Schwartz > Cc: R-help > Subject: Re: [R] recode according to

Re: [R] recode according to specific sequence of characters within a string variable

2011-02-04 Thread David Winsemius
On Feb 4, 2011, at 8:26 AM, Denis Kazakiewicz wrote: Dear R people Could you please help I have similar but opposite question How to reshape data from DF.new to DF from example, Mark kindly provided? Well, I don't think you want a random order, right? If what you are asking is for a singl

Re: [R] recode according to specific sequence of characters within a string variable

2011-02-04 Thread Denis Kazakiewicz
Dear R people Could you please help I have similar but opposite question How to reshape data from DF.new to DF from example, Mark kindly provided? Thank you Denis On Пят, 2011-02-04 at 07:09 -0600, Marc Schwartz wrote: > On Feb 4, 2011, at 6:32 AM, D. Alain wrote: > > > Dear R-List, > > > >

Re: [R] recode according to specific sequence of characters within a string variable

2011-02-04 Thread Marc Schwartz
Do you mean something like: > with(DF.new, paste(person, year, paste("team", team, sep = ""), sep = "_")) [1] "jeff_2001_teamx" "jeff_2002_teamy" "robert_2002_teamz" [4] "mary_2002_teamz" "mary_2003_teamy" ? See ?paste and ?with for more information, if so. HTH, Marc On Feb 4, 2011, a

Re: [R] recode according to specific sequence of characters within a string variable

2011-02-04 Thread Marc Schwartz
On Feb 4, 2011, at 6:32 AM, D. Alain wrote: > Dear R-List, > > I have a dataframe with one column "name.of.report" containing character > values, e.g. > > >> df$name.of.report > > "jeff_2001_teamx" > "teamy_jeff_2002" > "robert_2002_teamz" > "mary_2002_teamz" > "2003_mary_teamy" > ... > (i.

[R] recode according to specific sequence of characters within a string variable

2011-02-04 Thread D. Alain
Dear R-List, I have a dataframe with one column "name.of.report" containing character values, e.g. >df$name.of.report "jeff_2001_teamx" "teamy_jeff_2002" "robert_2002_teamz" "mary_2002_teamz" "2003_mary_teamy" ... (i.e. the bit of interest is not always at same position) Now I want to recode

Re: [R] recode letters to numbers

2010-12-03 Thread Jim Lemon
On 12/04/2010 01:25 AM, Katharina Noussi wrote: Hello, I have a dataframe assigning various scores on around 20 variables to a list of countries. The scores are rated on a scale of (D, C, B, A) and there are also some not rated ones (NR) and others are left blank (NA). I now wanted to transfer t

Re: [R] recode letters to numbers

2010-12-03 Thread Phil Spector
Katharina - I think something like this may be helpful: z = data.frame(matrix(sample(c(LETTERS[1:4],'NR',NA),100,replace=TRUE),20,5)) codes = c(A=100,B=27,C=50,D=25,NR=0) newz = sapply(z,function(x)codes[x]) - Phil Spector

[R] recode letters to numbers

2010-12-03 Thread Katharina Noussi
Hello, I have a dataframe assigning various scores on around 20 variables to a list of countries. The scores are rated on a scale of (D, C, B, A) and there are also some not rated ones (NR) and others are left blank (NA). I now wanted to transfer the scores into numeric values (such as NR=0, D=

Re: [R] recode according to old levels

2009-11-18 Thread Peter Ehlers
Andreas Wittmann wrote: Dear R-users, i try to recode a factor according to old levels F <- factor(sample(c(rep("A", 4), rep("B",2), rep("C",5 recode(F, "levels(F)[c(1,3)]='X'; else='Y'") i tried to work with eval or expression around levels(F)[c(1,3)], but nothing seems to work. I a

[R] recode according to old levels

2009-11-18 Thread Andreas Wittmann
Dear R-users, i try to recode a factor according to old levels F <- factor(sample(c(rep("A", 4), rep("B",2), rep("C",5 recode(F, "levels(F)[c(1,3)]='X'; else='Y'") i tried to work with eval or expression around levels(F)[c(1,3)], but nothing seems to work. Many thanks if anyone could t

Re: [R] Recode issue

2009-10-22 Thread Ista Zahn
Hi Nikhil, The problem is that your initial "as.factor(0)" causes x to have values of "1" : "20" instead of 1 : 20. There are two possible solutions: 1) > library(car) > x <- 1:20 > y <- as.factor(recode(x, " 1:5='A'; 6:10='B'; 11:15='C'; 16:20='D' ")) > y [1] A A A A A B B B B B C C C C C D D D

[R] Recode issue

2009-10-22 Thread Nikhil Kaza
I am having trouble with the recode function that is provided in the CAR package. I trying to create a new factors based on existing factors. E.g. >x <- as.factor(1:20) >y <- recode(x, " 1:5='A'; 6:10='B'; 11:15='C'; 16:20='D' ") >y [1] A A A A A 6 7 8 9 A A A A A A A A A A A Levels: 6 7 8 9

Re: [R] Recode of text variables

2009-04-01 Thread Gabor Grothendieck
doBy and memisc packages have recoding functions as well, e.g. > library(doBy) > x<-c("A","B","C","D","E","A") > out <- recodevar(x, list(c("A", "B"), c("C", "D", "E")), list("Treat 1", > "Treat 2")) > recodevar(x, list(c("A", "B"), c("C", "D", "E")), list("Treat 1", "Treat 2")) [1] "Treat 1" "Tr

Re: [R] Recode of text variables

2009-04-01 Thread John Fox
sure what recode(x, "1:2='A'; 3='B'") is supposed to do, inasmuch as x is a character, not numeric, vector. Thanks for bringing the bug to my attention. John > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.or

Re: [R] Recode of text variables

2009-04-01 Thread Bill.Venables
Original Message- From: Rolf Turner [mailto:r.tur...@auckland.ac.nz] Sent: Thursday, 2 April 2009 9:49 AM To: Venables, Bill (CMIS, Cleveland) Cc: andrew.mcfad...@maf.govt.nz; r-help@r-project.org Subject: Re: [R] Recode of text variables Uhhh, Bill, he wanted E to be recoded as ``Treat 3&

Re: [R] Recode of text variables

2009-04-01 Thread Rolf Turner
x27;s fascinating about Microsoft Access, but, uh... never mind. Yeah. ***Absolutely fascinating***. :-) Remind me to tell you sometime the story about the girl who went to finishing school in Switzerland R. Bill Venables http://www.cmis.csiro.au/bill.venables/ -Or

Re: [R] Recode of text variables

2009-04-01 Thread Rolf Turner
On 2/04/2009, at 12:22 PM, Andrew McFadden wrote: Hi all I am trying to do a simple recode which I am stumbling on. I figure there must be any easy way but haven't come across it. Given data of A","B","C","D","E","A" it would be nice to recode this into say three categories ie A and B becomes

Re: [R] Recode of text variables

2009-04-01 Thread Bill.Venables
ascinating about Microsoft Access, but, uh... never mind. Bill Venables http://www.cmis.csiro.au/bill.venables/ -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Andrew McFadden Sent: Thursday, 2 April 2009 9:23 AM To: r-help@r-proje

[R] Recode of text variables

2009-04-01 Thread Andrew McFadden
Hi all I am trying to do a simple recode which I am stumbling on. I figure there must be any easy way but haven't come across it. Given data of A","B","C","D","E","A" it would be nice to recode this into say three categories ie A and B becomes "Treat1", C becomes "Treat 2" and E becomes "Treat 3"

Re: [R] Recode factor into binary factor-level vars

2009-03-07 Thread David Winsemius
Sören; You need to somehow add back to the information that is in "l" that fact that it was sampled from a set with 4 elements. Since you didn't sample from a factor the level information was lost. Otherwise, you coud create that list with unique(l) which in this case only returns 3 eleme

Re: [R] Recode factor into binary factor-level vars

2009-03-07 Thread Dimitris Rizopoulos
one way is: set.seed(20) l <- sample(rep.int(c("locA", "locB", "locC", "locD"), 100), 10, replace=T) f <- factor(l, levels = paste("loc", LETTERS[1:4], sep = "")) m <- as.data.frame(model.matrix(~ f - 1)) names(m) <- levels(f) m I hope it helps. Best, Dimitris soeren.vo...@eawag.ch wrote: H

[R] Recode factor into binary factor-level vars

2009-03-07 Thread soeren . vogel
How to I "recode" a factor into a binary data frame according to the factor levels: ### example:start set.seed(20) l <- sample(rep.int(c("locA", "locB", "locC", "locD"), 100), 10, replace=T) # [1] "locD" "locD" "locD" "locD" "locB" "locA" "locA" "locA" "locD" "locA" ### example:end What I

Re: [R] Recode

2008-05-17 Thread John Fox
Dear Raphael, This is a bug in recode(): The problem is that recode() tries to figure out whether it can convert the result to numeric, and the approach that it uses is faulty when there are both numerals and other characters in the recode target. I should say, as well, that I can't precisely du

[R] Recode

2008-05-17 Thread Raphael Saldanha
Hi! Using recode in cars package, I tryed to use the following: recode(data$nrcomp, "lo:5='0 to 5'; 5:hi='bigger than 5'") I got: Erro em parse(text = strsplit(term, "=")[[1]][2]) : unexpected end of input in "'0 to 5" When I try only numbers, or only text, it's ok, but when I try to combine

Re: [R] recode involving a count rule

2008-05-03 Thread jim holtman
This should do what you want -- not sure what happens after the 4th less than 5. > a <- rep(c(3,5,7), 4) > b <- rep(c(NA,1,2), 4) > df <- data.frame(a,b) > # determine which ones are < 5 and count the occurances > less.5 <- cumsum(df$a < 5) > # create new value > df$c <- ifelse(df$a < 5, (less.5 +

[R] recode involving a count rule

2008-05-03 Thread Greg Blevins
Hello, R-helpers. I have a data frame below called df. I want to add a variable c, based on the following logic: if df$a < 5 then the first two times this condition is met place a 1 in c, the next two times this condition is met place a 2 in c and when the condition is not met let c equal df$b.

Re: [R] Recode factors

2008-03-27 Thread Abhijit Dasgupta
; > >> -Original Message- >> From: Henrique Dallazuanna [mailto:[EMAIL PROTECTED] >> Sent: Thursday, March 27, 2008 12:50 PM >> To: Doran, Harold >> Cc: r-help@r-project.org >> Subject: Re: [R] Recode factors >> >> If I understand, y

Re: [R] Recode factors

2008-03-27 Thread Doran, Harold
Perfect. My headache is gone. Thanks. > -Original Message- > From: Henrique Dallazuanna [mailto:[EMAIL PROTECTED] > Sent: Thursday, March 27, 2008 12:50 PM > To: Doran, Harold > Cc: r-help@r-project.org > Subject: Re: [R] Recode factors > > If I unde

Re: [R] Recode factors

2008-03-27 Thread Henrique Dallazuanna
If I understand, you can try this: levels(x)[is.na(as.numeric(levels(x)))] <- 0 On 27/03/2008, Doran, Harold <[EMAIL PROTECTED]> wrote: > I know this comes up, but I didn't see my exact issue in the archives. I > have variables in a dataframe that need to be recoded. Here is what I'm > dealing

[R] Recode factors

2008-03-27 Thread Doran, Harold
I know this comes up, but I didn't see my exact issue in the archives. I have variables in a dataframe that need to be recoded. Here is what I'm dealing with I have a factor called aa > class(aa) [1] "factor" > table(aa) aa *0123ABCDLNT 0

Re: [R] recode() function results in logical output, not factor output

2008-01-08 Thread John Fox
c: r-help@r-project.org > Subject: Re: [R] recode() function results in logical output, not > factor output > > On 1/7/2008 3:32 PM, Thomas MacFarland wrote: > > Dear R Users: > > > > I have race-ethnicity groups identified in the factor variable > Ethnic_G. > >

Re: [R] recode() function results in logical output, not factor output

2008-01-08 Thread Chuck Cleland
On 1/7/2008 3:32 PM, Thomas MacFarland wrote: > Dear R Users: > > I have race-ethnicity groups identified in the factor variable Ethnic_G. > > I need to collapse Ethnic_G into a new variable with only two factors, 1 > (White, non-Hispanic) and 2 (Minority). > > As seen in the code and output

[R] recode() function results in logical output, not factor output

2008-01-08 Thread Thomas MacFarland
Dear R Users: I have race-ethnicity groups identified in the factor variable Ethnic_G. I need to collapse Ethnic_G into a new variable with only two factors, 1 (White, non-Hispanic) and 2 (Minority). As seen in the code and output below, the recoded race-ethnicity variable is put into logi

Re: [R] recode based on filter

2007-12-19 Thread Donatas G.
On Wednesday 19 December 2007 14:12:16 rašėte: > sapply(levels(DATA$know1), function(x)subset(DATA, (know1==x & > know2==x)), simplify=F) Hey, thanks, that seems to work! -- Donatas Glodenis http://dg.lapas.info __ R-help@r-project.org mailing list ht

Re: [R] recode based on filter

2007-12-19 Thread Henrique Dallazuanna
Perhaps you can try subset the data: sapply(levels(DATA$know1), function(x)subset(DATA, (know1==x & know2==x)), simplify=F) On 19/12/2007, Donatas G. <[EMAIL PROTECTED]> wrote: > Hi, I have a data frame DATA, which (simplified of course) looks like this: > > know1 = c("Y","N","N","Y","N","N","Y",

[R] recode based on filter

2007-12-19 Thread Donatas G.
Hi, I have a data frame DATA, which (simplified of course) looks like this: know1 = c("Y","N","N","Y","N","N","Y","Y","N") par1=c(1,4,5,3,3,2,3,3,5) know2 = c("Y","Y","N","Y","N","N","N","Y","Y") par2=c(3,4,4,3,5,2,4,3,2) DATA=data.frame(know1,par1,know2,par2) it represents answers in a questionn