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 *(i.e. for V1 the code will be 0=a, 1=b, 2=c, for V2 0=z, 1=y, 2=x). My new codes are stored in another data frame (call it Y). colnames(X) and rownames(Y) matches. How can I solve this situation? Thanks a lot in advance, Pasquale -- View this message in context: http://r.789695.n4.nabble.com/Recode-values-tp4680959.html Sent from the R help mailing list archive at Nabble.com.
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 dat2[] <-lapply(seq_len(ncol(dat2)),function(i) as.character(factor(dat2[,i],labels=lst1[[i]]))) ?identical(dat1,dat2) #[1] TRUE A.K. 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 (i.e. for V1 the code will be 0=a, 1=b, 2=c, for V2 0=z, 1=y, 2=x). My new codes are stored in another data frame (call it Y). colnames(X) and rownames(Y) matches. How can I solve this situation? Thanks a lot in advance, Pasquale
On 22 Nov 2013, at 11:13 , lillosdos <lillo_dicarlo at live.it> 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 is that for each columns the recoding system is different *(i.e. > for V1 the code will be 0=a, 1=b, 2=c, for V2 0=z, 1=y, 2=x). > My new codes are stored in another data frame (call it Y). colnames(X) and > rownames(Y) matches. > How can I solve this situation?Why rownames(Y) (typo?) I?d expect the ticket to be f <- function(x,names) factor(x, levels=0:2, labels=names) and then mapply(FUN=f, X, listofnamevectors) where listofnamevectors could be just Y or as.data.frame(t(Y)) depending on whether rownames(Y) was really colnames(Y) or not.> > Thanks a lot in advance, > Pasquale > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Recode-values-tp4680959.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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com