I want to replace "BO" by "BOB", "C" by "CR", "CL" by "CLO", and the list is more long.
I can do that for each element:
df[df=="BO"]<-"BOB"
But my df is bigger indeed with other elements.
I was thinking using replace(), but can't get any clean result ( NA or all elements replaced with only one), neither with sapply().
TY for any help, and sorry for the n00b question.
Arnaud Gaboury
?
A2CT2 Ltd.
I did indeed have a look at recode(), and was able to replace, but an error warning :
recode(names,"BO","BOO",df)
Warning message:
In recode.default(names, "BO", "BOO", df) :
Name(s) of vars duplicates with an object outside the dataFrame.
df
names price
1 BOO 10
2 C 25
3 CL 20
As you can see, "BO" has been replaced by "BOO", but with a warning!
Arnaud Gaboury
?
A2CT2 Ltd.
Trade: +41 22 849 88 63
Fax:?? +41 22 849 88 66
arnaud.gaboury at a2ct2.com
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. Access to this email by anyone else is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution or any action taken or omitted to be taken in reliance on it, is prohibited and may be unlawful. If you have received this email in error please notify the sender.
From: Jorge I Velez [mailto:jorgeivanvelez at gmail.com]
Sent: mardi 7 f?vrier 2012 19:55
To: Arnaud Gaboury
Cc: r-help at r-project.org
Subject: Re: [R] replace elements of a data frame
Hi Arnaud,
Take a look at
require(car)
?recode
HTH,
Jorge.-
On Tue, Feb 7, 2012 at 1:05 PM, Arnaud Gaboury <> wrote:
Hello,
I am getting mad at finding a simple way to replace elements of a df.
Here is a short df :
I want to replace "BO" by "BOB", "C" by "CR", "CL" by "CLO", and the list is more long.
I can do that for each element:
df[df=="BO"]<-"BOB"
But my df is bigger indeed with other elements.
I was thinking using replace(), but can't get any clean result ( NA or all elements replaced with only one), neither with sapply().
TY for any help, and sorry for the n00b question.
Arnaud Gaboury
?
A2CT2 Ltd.
______________________________________________
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.
I used in fact recode() from epilcac package, not the one you mentioned!
Arnaud Gaboury
?
A2CT2 Ltd.
-----Original Message-----
From: Arnaud Gaboury
Sent: mardi 7 f?vrier 2012 20:25
To: Jorge I Velez
Cc: r-help at r-project.org; Arnaud Gaboury
Subject: RE: [R] replace elements of a data frame
I did indeed have a look at recode(), and was able to replace, but an error warning :
recode(names,"BO","BOO",df)
Warning message:
In recode.default(names, "BO", "BOO", df) :
Name(s) of vars duplicates with an object outside the dataFrame.
df
names price
1 BOO 10
2 C 25
3 CL 20
As you can see, "BO" has been replaced by "BOO", but with a warning!
Arnaud Gaboury
?
A2CT2 Ltd.
From: Jorge I Velez [mailto:jorgeivanvelez at gmail.com]
Sent: mardi 7 f?vrier 2012 19:55
To: Arnaud Gaboury
Cc: r-help at r-project.org
Subject: Re: [R] replace elements of a data frame
Hi Arnaud,
Take a look at
require(car)
?recode
HTH,
Jorge.-
On Tue, Feb 7, 2012 at 1:05 PM, Arnaud Gaboury <> wrote:
Hello,
I am getting mad at finding a simple way to replace elements of a df.
Here is a short df :
I want to replace "BO" by "BOB", "C" by "CR", "CL" by "CLO", and the list is more long.
I can do that for each element:
df[df=="BO"]<-"BOB"
But my df is bigger indeed with other elements.
I was thinking using replace(), but can't get any clean result ( NA or all elements replaced with only one), neither with sapply().
TY for any help, and sorry for the n00b question.
Arnaud Gaboury
?
A2CT2 Ltd.
______________________________________________
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.
names1 price
1 BOO 10
2 CC 25
3 CLR 20
TY for the tip.
Any possibility to write this in one single line?
Arnaud Gaboury
?
A2CT2 Ltd.
-----Original Message-----
From: Berend Hasselman [mailto:bhh at xs4all.nl]
Sent: mardi 7 f?vrier 2012 20:46
To: Arnaud Gaboury
Cc: Jorge I Velez; r-help at r-project.org
Subject: Re: [R] replace elements of a data frame
On 07-02-2012, at 20:24, Arnaud Gaboury wrote:
I did indeed have a look at recode(), and was able to replace, but an error warning :
recode(names,"BO","BOO",df)
Warning message:
In recode.default(names, "BO", "BOO", df) :
Name(s) of vars duplicates with an object outside the dataFrame.
df
names price
1 BOO 10
2 C 25
3 CL 20
As you can see, "BO" has been replaced by "BOO", but with a warning!
library(car)
names<-c("BO","C","CL")
price<-c("10","25","20")
df<-data.frame(names,price)
recode(df$names,"'BO'='BOO'; 'CL'='CLO'; 'C'='CR'")
results in
[1] BOO CR CLO
Levels: BOO CLO CR
Note the single quotes.
Berend