Skip to content

replace funcion

5 messages · Dominic Roye, arun, Rui Barradas +1 more

#
HI,

Try this:
set.seed(1)
?df1<-data.frame(colA=sample(1:100,n,replace=TRUE)) 

numbers1<-c(1,2,3,4,11,12)
?numbers2<-c(5,6,7,8,9,10)
?df1$colA[df1$colA%in%numbers1]<-"invierno"
?df1$colA[df1$colA%in%numbers2]<-"verano"
?head(df1,10)
???? colA
1????? 27
2????? 38
3????? 58
4????? 91
5????? 21
6????? 90
7????? 95
8????? 67
9????? 63
10 verano

A.K.



----- Original Message -----
From: Dominic Roye <dominic.roye at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, August 11, 2012 3:10 PM
Subject: [R] replace funcion

Hello everybody,


I would like to replace or recode a list of numbers between 1 and 12 (total
100). I have tried to make it with recode, but i have two types of
replacements. For 1,2,3,4,11,12 => invierno and for 5,6,7,8,9 and 10 =>
verano.

recode(datos.mx1[,7], "1='invierno'; 2='invierno';
3='invierno';4='invierno';11='invierno';12='invierno'")? #with this command
it works perfectly, but i have only one part

recode(datos.mx1[,7], "'5'='verano'; '6'='verano';
'7'='verano';'8'='verano';'9'='verano';'10'='verano'") #when i run this
command it works also fine, but it clears the first change above.

I had previously tried it with the if-command, but it doesn't work. I hope
someone can help me. Thank you very much.

Domi

??? [[alternative HTML version deleted]]

______________________________________________
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,

I guess this should also work.
set.seed(1)
?df1<-data.frame(colA=sample(1:100,n,replace=TRUE))
numbers1<-c(1,2,3,4,11,12)
?numbers2<-c(5,6,7,8,9,10)

df1[df1$colA%in%numbers1,]<-"invierno"
df1[df1$colA%in%numbers2,]<-"verano"
#or
set.seed(1)
?df1<-data.frame(colA=sample(1:100,n,replace=TRUE))
within(df1,{colA[colA%in%numbers1]<-"invierno";colA[colA%in%numbers2]<-"verano"})

A.K.




----- Original Message -----
From: Dominic Roye <dominic.roye at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, August 11, 2012 3:10 PM
Subject: [R] replace funcion

Hello everybody,


I would like to replace or recode a list of numbers between 1 and 12 (total
100). I have tried to make it with recode, but i have two types of
replacements. For 1,2,3,4,11,12 => invierno and for 5,6,7,8,9 and 10 =>
verano.

recode(datos.mx1[,7], "1='invierno'; 2='invierno';
3='invierno';4='invierno';11='invierno';12='invierno'")? #with this command
it works perfectly, but i have only one part

recode(datos.mx1[,7], "'5'='verano'; '6'='verano';
'7'='verano';'8'='verano';'9'='verano';'10'='verano'") #when i run this
command it works also fine, but it clears the first change above.

I had previously tried it with the if-command, but it doesn't work. I hope
someone can help me. Thank you very much.

Domi

??? [[alternative HTML version deleted]]

______________________________________________
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.
#
Hello,

Try the following, using index vectors, not recode().

inx <- datos.mx1[, 7] %in% 5:10
datos.mx1[inx, 7] <- "verano"
datos.mx1[!inx, 7] <- "invierno"

Hope this helps,

Rui Barradas

Em 11-08-2012 20:10, Dominic Roye escreveu:
#
Or if you want to use recode (which you forgot to mention is in package
car):

set.seed(1)
df1<- c(sample(1:12, 50, replace=TRUE)) # sample data

library(car)
recode(df1, "c(1, 2, 3, 4, 11, 12)='invierno'; 5:10='verano'")

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352