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
you need to return 'x' as the last statement of the lapply. Sent from my iPad On Nov 8, 2012, at 22:06, Simon Kiss <sjkiss at gmail.com> wrote:> 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'")x> } > ) > > ********************************* > 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.
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.
HI,
Sorry, I gave the wrong answer.??res<-lapply(mylist,function(x)
data.frame(a=x[,1],prov=recode(x[,2],"'QUE'='QC';'Quebec'='QC'")))
res[[1]][1:6,]
#? a prov
#1 1?? QC
#2 2?? QC
#3 3?? QC
#4 4?? QC
#5 5?? QC
#6 6?? BC
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.
HI,
Sorry, I didn't know that "prov" exists in different columns.
Try this:
#changed your example dataset to include one more column and reordered the
columns in list elements
mylist<-list(df1=data.frame(a=seq(1,10,1),
b=sample(1:15,10,replace=FALSE),prov=c(rep('QUE', 5), rep('BC',
5))), df2=data.frame(prov=c(rep('Quebec', 5), rep('AB',
5)),a=seq(1,10,1),b=sample(1:25,10,replace=FALSE)))
res<-lapply(mylist,function(x)
data.frame(x[colnames(x)!="prov"],prov=recode(x$prov,"'QUE'='QC';'Quebec'='QC'")))
?res
#$df1
?# ? a? b prov
#1?? 1? 9?? QC
#2?? 2? 8?? QC
#3?? 3? 1?? QC
#4?? 4? 7?? QC
#5?? 5 15?? QC
#6?? 6 10?? BC
#7?? 7? 6?? BC
#8?? 8 11?? BC
#9?? 9? 5?? BC
#10 10 14?? BC
#$df2
?# ? a? b prov
#1?? 1 23?? QC
#2?? 2 16?? QC
#3?? 3 12?? QC
#4?? 4 14?? QC
#5?? 5 17?? QC
#6?? 6? 7?? AB
#7?? 7? 6?? AB
#8?? 8 21?? AB
#9?? 9? 5?? AB
#10 10? 1?? AB
A.K.
----- Original Message -----
From: Simon Kiss <sjkiss at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc:
Sent: Friday, November 9, 2012 9:39 AM
Subject: Re: [R] using lapply with recode
Hi there:
None of these suggestions do the work. I tried Jim's suggestion and it
returns the following:
####
$df1
? ? a prov
1? 1? QUE
2? 2? QUE
#####
$df2
? ? a? prov
1? 1 Quebec
2? 2 Quebec
So it didn't actually do the recode.? Arun's first suggestion returned
the same problem. And his second suggestion won't work because in the
original data set the variable "prov" exists in different columns; I
can't use the same column subscript to access each column.
Simon Kiss
On 2012-11-08, at 10:43 PM, arun wrote:
> HI,
>
> Sorry, I gave the wrong answer.? res<-lapply(mylist,function(x)
data.frame(a=x[,1],prov=recode(x[,2],"'QUE'='QC';'Quebec'='QC'")))
> res[[1]][1:6,]
> #? a prov
> #1 1? QC
> #2 2? QC
> #3 3? QC
> #4 4? QC
> #5 5? QC
> #6 6? BC
> 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.
>
*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
Cell: +1 905 746 7606
Please avoid sending me Word, PowerPoint or Excel attachments. Sending these
documents puts pressure on many people to use Microsoft software and helps to
deny them any other choice. In effect, you become a buttress of the Microsoft
monopoly.
To convert to plain text choose Text Only or Text Document as the Save As Type.?
Your computer may also have a program to convert to PDF format. Select File,
then Print. Scroll through available printers and select the PDF converter.
Click on the Print button and enter a name for the PDF file when requested.
HI,
I found the reason why my first suggestion didn't work out:
mylist<-list(df1=data.frame(a=seq(1,10,1),
b=sample(1:15,10,replace=FALSE),prov=c(rep('QUE', 5), rep('BC',
5))),
df2=data.frame(prov=c(rep('Quebec', 5), rep('AB',
5)),a=seq(1,10,1),b=sample(1:25,10,replace=FALSE)))?
?lapply(mylist,function(x)
{x$prov<-recode(x$prov,"'QUE'='QC';'Quebec'='QC'")?????
?return(x)})???????????????????????
#$df1?
?# ? a? b prov
#1?? 1? 4?? QC
#2?? 2 13?? QC
#3?? 3 14?? QC
#4?? 4 12?? QC
#5?? 5? 3?? QC
#6?? 6 11?? BC
#7?? 7 15?? BC
#8?? 8 10?? BC
#9?? 9? 6?? BC
#10 10? 9?? BC
#$df2
?#? prov? a? b
#1??? QC? 1 21
#2??? QC? 2? 7
#3??? QC? 3 15
#4??? QC? 4? 5
#5??? QC? 5? 9
#6??? AB? 6 24
#7??? AB? 7? 3
#8??? AB? 8 23
#9??? AB? 9 19
#10?? AB 10 20
#or you can use:
res<-lapply(mylist,function(x)
data.frame(x[colnames(x)!="prov"],prov=recode(x$prov,"'QUE'='QC';'Quebec'='QC'")))
A.K.
----- Original Message -----
From: Simon Kiss <sjkiss at gmail.com>
To: arun <smartpink111 at yahoo.com>
Cc:
Sent: Friday, November 9, 2012 9:39 AM
Subject: Re: [R] using lapply with recode
Hi there:
None of these suggestions do the work. I tried Jim's suggestion and it
returns the following:
####
$df1
? ? a prov
1? 1? QUE
2? 2? QUE
#####
$df2
? ? a? prov
1? 1 Quebec
2? 2 Quebec
So it didn't actually do the recode.? Arun's first suggestion returned
the same problem. And his second suggestion won't work because in the
original data set the variable "prov" exists in different columns; I
can't use the same column subscript to access each column.
Simon Kiss
On 2012-11-08, at 10:43 PM, arun wrote:
> HI,
>
> Sorry, I gave the wrong answer.? res<-lapply(mylist,function(x)
data.frame(a=x[,1],prov=recode(x[,2],"'QUE'='QC';'Quebec'='QC'")))
> res[[1]][1:6,]
> #? a prov
> #1 1? QC
> #2 2? QC
> #3 3? QC
> #4 4? QC
> #5 5? QC
> #6 6? BC
> 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.
>
*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
Cell: +1 905 746 7606
Please avoid sending me Word, PowerPoint or Excel attachments. Sending these
documents puts pressure on many people to use Microsoft software and helps to
deny them any other choice. In effect, you become a buttress of the Microsoft
monopoly.
To convert to plain text choose Text Only or Text Document as the Save As Type.?
Your computer may also have a program to convert to PDF format. Select File,
then Print. Scroll through available printers and select the PDF converter.
Click on the Print button and enter a name for the PDF file when requested.
Apparently Analagous Threads
- Create single vector after looping through multiple data frames with GREP
- separate elements of a character vector
- cycling from x11 window in RCommander to graphics device window: Mac Os 10.6.8
- select different variables from a list of data frames
- Changing line length in Sweave output works for numeric, but not for character vectors