[R] using lapply with recode
arun
smartpink111 at yahoo.com
Fri Nov 9 04:28:03 CET 2012
HI,
Try this:
lapply(mylist,function(x) {recode(x[,2],"'QUE'='QC';'Quebec'='QC'")
return(x)})
#$df1
# a prov
#1 1 QUE
#2 2 QUE
#3 3 QUE
#4 4 QUE
#5 5 QUE
#6 6 BC
#7 7 BC
#8 8 BC
#9 9 BC
#10 10 BC
#$df2
# a prov
#1 1 Quebec
#2 2 Quebec
#3 3 Quebec
#4 4 Quebec
#5 5 Quebec
#6 6 AB
#7 7 AB
#8 8 AB
#9 9 AB
#10 10 AB
A.K.
----- Original Message -----
From: Simon Kiss <sjkiss at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Thursday, November 8, 2012 10:06 PM
Subject: [R] using lapply with recode
Hello:
Forgive me, this is surely a simple question but I can't figure it out, having consulted the help archives and "Data Manipulation With R" (Spector).
I have a list of 11 data frames with one common variable in each (prov). I'd like to use lapply to go through and recode one particular level of that common variable.
I can get the recode to work, but it only returns the variable that has been recoded. I need the whole data frame with the recoded variable.
Thank you for your help. Reproducible data and my current code are below.
####Sample Data
mylist<-list(df1=data.frame(a=seq(1,10,1), prov=c(rep('QUE', 5), rep('BC', 5))), df2=data.frame(a=seq(1,10,1), prov=c(rep('Quebec', 5), rep('AB', 5))))
str(mylist)
###My current code
lapply(mylist, function(x) {
recode(x$prov, "'QUE'='QC' ; 'Quebec'='QC'")
}
)
*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
______________________________________________
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